-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (53 loc) · 2.08 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
from constants import *
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from bullet import Bullet
def main():
printStartInformations()
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
time_clock = pygame.time.Clock()
delta_time_from_last_tick = 0
bullets, updatable_objects, drawable_objects, asteroids = initaiteGameObjects()
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
AsteroidField()
on = True
while(on):
on = check_for_exit()
screen.fill((0, 0, 0))
updatable_objects.update(delta_time_from_last_tick)
for asteroid in asteroids:
if asteroid.check_for_collision(player):
print("Game over!")
on = False
for bullet in bullets:
if bullet.check_for_collision(asteroid):
asteroid.destroy()
bullet.destroy()
for drawable_object in drawable_objects:
drawable_object.draw(screen)
pygame.display.flip()
delta_time_from_last_tick = time_clock.tick(MAX_FRAME_PER_SECOND) / 1000
def initaiteGameObjects():
updatable_objects = pygame.sprite.Group()
drawable_objects = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
bullets = pygame.sprite.Group()
Asteroid.containers = (asteroids, updatable_objects, drawable_objects)
Player.containers = (updatable_objects, drawable_objects)
AsteroidField.containers = (updatable_objects)
Bullet.containers = (bullets, updatable_objects, drawable_objects)
return bullets, updatable_objects, drawable_objects, asteroids
def printStartInformations():
print("Starting Asteroids!")
print(f"Screen width: {SCREEN_WIDTH}")
print(f"Screen height: {SCREEN_HEIGHT}")
def check_for_exit():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
return True
if __name__ == "__main__":
main()