-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (51 loc) · 1.73 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
68
import pygame
import sys
from constants import TICK_TIME
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from shot import Shot
def main():
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screen_w, screen_h = pygame.display.get_surface().get_size()
clock = pygame.time.Clock()
background = pygame.image.load("static/background.jpg")
# Groups
updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
# Containers
Asteroid.containers = (asteroids, updatable, drawable)
AsteroidField.containers = updatable
Shot.containers = (shots, updatable, drawable)
asteroid_field = AsteroidField(screen_w, screen_h)
Player.containers = (updatable, drawable)
player = Player(screen_w / 2, screen_h / 2)
dt = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
for obj in updatable:
obj.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.kill()
asteroid.split()
screen.fill("black")
screen.blit(background, background.get_rect())
for obj in drawable:
obj.draw(screen)
pygame.display.flip()
# limit the frame rate to 60 FPS
dt = clock.tick(TICK_TIME) / 1000
if __name__ == "__main__":
main()