-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (48 loc) · 1.56 KB
/
main.py
File metadata and controls
62 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import random
import pygame
from asteroid import *
from asteroidfield import *
from constants import *
from player import *
from shot import *
def main():
print("Starting Asteroids!")
print(f"Screen width: {SCREEN_WIDTH}")
print(f"Screen height: {SCREEN_HEIGHT}")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
game_clock = pygame.time.Clock()
dt = 0
# Create groups for easier handling within gameloop
asteroids = pygame.sprite.Group()
drawable = pygame.sprite.Group()
shots = pygame.sprite.Group()
updatable = pygame.sprite.Group()
Asteroid.containers = (asteroids, updatable, drawable)
AsteroidField.containers = updatable
Player.containers = (updatable, drawable)
Shot.containers = (updatable, drawable, shots)
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
field = AsteroidField()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
screen.fill("black")
for object in drawable:
object.draw(screen)
pygame.display.flip()
updatable.update(dt)
for asteroid in asteroids:
if asteroid.collision(player):
print("Game over!")
exit()
for asteroid in asteroids:
for shot in shots:
if asteroid.collision(shot):
asteroid.split()
shot.kill()
dt = game_clock.tick(60) / 1000
# Initialize pygame
pygame.init()
if __name__ == "__main__":
main()