-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (64 loc) · 2.46 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pygame
from constants import *
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from shot import Shot
def main():
# initializes pygame https://www.pygame.org/docs/ref/pygame.html#pygame.init
pygame.init()
# creates a python screen, sets resolution
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# creates a clock https://www.pygame.org/docs/ref/time.html#pygame.time.Clock
# important so we can track delta time and limit frames per second
clock = pygame.time.Clock()
# delta time
dt = 0
# groups
updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
# add shots to containers
Shot.containers = (updatable, drawable, shots)
# add player to the drawable and updatable sprite groups
Player.containers = (updatable, drawable)
# add asteroids to containers
Asteroid.containers = (asteroids, updatable, drawable)
# add asteroidfield to updatable container
AsteroidField.containers = (updatable)
# create a player object, see player.py
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, PLAYER_RADIUS)
asteroidfield = AsteroidField()
# main game loop
while True:
# capture the quick event from pressing the X on a window
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
# paint the screen background black
screen.fill("black")
# run update on updatable group
for obj in updatable:
obj.update(dt)
# check for collisions with the player
for obj in asteroids:
if obj.is_colliding(player):
print("Game over!")
exit()
# check for collisions between asteroids and shots
for roid in asteroids:
for shot in shots:
if roid.is_colliding(shot):
roid.split()
shot.kill()
# run draw on drawable group
for obj in drawable:
obj.draw(screen)
# runs a frame buffer flip
pygame.display.flip()
# captures deltatime, also limits frames to x frames per second see https://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick
dt = clock.tick(60) / 1000
# Makes only this file execute the main method
if __name__ == "__main__":
main()