-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (47 loc) · 1.57 KB
/
main.py
File metadata and controls
57 lines (47 loc) · 1.57 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
import pygame
import sys
from constants import *
from player import Player, Shot
from asteroid import Asteroid
from asteroidfield import AsteroidField
def main():
print("Starting Asteroids!")
print(f"Screen width: {SCREEN_WIDTH}")
print(f"Screen height: {SCREEN_HEIGHT}")
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
x = int(SCREEN_WIDTH / 2)
y = int(SCREEN_HEIGHT / 2)
updatables = pygame.sprite.Group()
drawables = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
Player.containers = (updatables, drawables)
Asteroid.containers = (updatables, drawables, asteroids)
AsteroidField.containers = (updatables)
Shot.containers = (updatables, drawables, shots)
player = Player(x, y)
asteroid_field = AsteroidField()
clock = pygame.time.Clock()
dt = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
updatables.update(dt)
screen.fill("black")
for drawable in drawables:
drawable.draw(screen)
for asteroid in asteroids:
for shot in shots:
if asteroid.collision(shot):
asteroid.split()
shot.kill()
for asteroid in asteroids:
if asteroid.collision(player):
print("Game over!")
sys.exit()
pygame.display.flip()
dt = clock.tick(60) / 1000 # convert milliseconds to seconds
if __name__ == "__main__":
main()