-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (62 loc) · 1.94 KB
/
main.py
File metadata and controls
78 lines (62 loc) · 1.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# this allows us to use code from
# the open-source pygame library
# throughout this file
import pygame
from constants import *
from player import Player
from shot import Shot
from asteroid import Asteroid
from asteroidfield import AsteroidField
def main():
# init game
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# turn off text input (mac compatibility)
pygame.key.stop_text_input()
# game clock
clock = pygame.time.Clock()
dt = 0
# set up object groups
updatables = pygame.sprite.Group()
drawables = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
# create player
Player.containers = (updatables, drawables)
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
# set up shots
Shot.containers = (shots, updatables, drawables)
# create asteroids
Asteroid.containers = (asteroids, updatables, drawables)
AsteroidField.containers = (updatables)
AsteroidField()
while True:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
# set background
screen.fill(pygame.Color('black'))
# do updates on objects
for obj in updatables:
obj.update(dt)
# check player collisions
for asteroid in asteroids:
if player.collision(asteroid):
print("Game over!")
return
# check asteroid shot collision
for asteroid in asteroids:
for shot in shots:
if asteroid.collision(shot):
asteroid.split()
shot.kill()
# draw objects
for obj in drawables:
obj.draw(screen)
# draw screen
pygame.display.flip()
# limit framerate
dt = clock.tick(MAX_FRAMERATE) / 1000
if __name__ == "__main__":
main()