Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (47 loc) · 1.66 KB
/
Copy pathmain.py
File metadata and controls
54 lines (47 loc) · 1.66 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
import pygame
import sys
from constants import *
from player import *
from asteroid import *
from asteroidfield import *
def main():
pygame.init()
clock = pygame.time.Clock()
dt = 0
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
shots = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group()
Shot.containers = (shots, updatable, drawable)
AsteroidField.containers = (updatable,)
Asteroid.containers = (asteroids, updatable, drawable)
Player.containers = (updatable, drawable)
player = Player(x = SCREEN_WIDTH / 2, y = SCREEN_HEIGHT / 2)
asteroidfield = AsteroidField()
#use an infinite while loop for the game loop. At each iteration, it should:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
dt = clock.tick(60)/1000
pygame.Surface.fill(screen, (0, 0, 0))
updatable.update(dt)
for object in drawable:
object.draw(screen)
for asteroid in asteroids:
if player.collision(asteroid):
print("Game over!")
sys.exit()
for asteroid in asteroids:
for shot in shots:
if shot.collision(asteroid):
asteroid.split()
shot.kill()
#2. use pygames's display.flip() method to refresh the screen. This should be called last.
pygame.display.flip()
print("Starting Asteroids!")
print(f"Screen width: {SCREEN_WIDTH}")
print(f"Screen height: {SCREEN_HEIGHT}")
if __name__ == "__main__":
main()