-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (52 loc) · 1.67 KB
/
main.py
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
63
64
65
66
67
import sys
import pygame
import constants as c
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from shot import Shot
def main():
pygame.init()
print("Starting asteroids!")
print(f"Screen width: {c.SCREEN_WIDTH}")
print(f"Screen height: {c.SCREEN_HEIGHT}")
# Containers
updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
# asign the containers to classes
Player.containers = (updatable, drawable)
Asteroid.containers = (updatable, drawable, asteroids)
AsteroidField.containers = updatable
Shot.containers = (updatable, drawable, shots)
screen = pygame.display.set_mode((c.SCREEN_WIDTH, c.SCREEN_HEIGHT))
clock = pygame.time.Clock()
dt = 0
run = True
player = Player(c.SCREEN_WIDTH / 2, c.SCREEN_HEIGHT / 2)
asteroidsfield = AsteroidField()
# Game LOOP
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
screen.fill("#000000")
for ug in updatable:
ug.update(dt)
for asteroid in asteroids:
if asteroid.collides_with(player):
print("Game Over!")
sys.exit()
for shot in shots:
if asteroid.collides_with(shot):
shot.kill()
asteroid.split()
for dg in drawable:
dg.draw(screen)
pygame.display.flip()
delta = clock.tick(60) # 60 FPS
print(f"FPS {clock.get_fps()}")
dt = delta / 1000
if __name__ == "__main__":
main()