-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (55 loc) · 1.98 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
import pygame
from constants import *
from circleshape import *
from player import *
from asteroid import *
from asteroidfield import *
def init_player():
player_x_pos = SCREEN_WIDTH/2
player_y_pos = SCREEN_HEIGHT/2
return Player(player_x_pos, player_y_pos)
def init_asteroid_field():
return AsteroidField()
def main():
print("Starting asteroids!")
print(f"Screen width: {SCREEN_WIDTH}")
print(f"Screen height: {SCREEN_HEIGHT}")
player = init_player() # initialise the player
asteroid_field = init_asteroid_field() # initialise the asteroid field
# initizalize pygame
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.DOUBLEBUF | pygame.HWSURFACE)
pygame.display.set_caption("boot.dev: Asteroids!")
# main game loop
game_running = True
game_clock = pygame.time.Clock()
delta_time = 0
# run the game
while game_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
# update the delta time:
delta_time = game_clock.tick(60)/1000
# update assets in "updatable" container
for updatable_asset in updatable:
updatable_asset.update(delta_time)
# check for collisions:
for asteroid in asteroids:
for bullet in bullets:
if asteroid.is_colliding_with(bullet):
asteroid.split()
bullet.kill()
pass
if asteroid.is_colliding_with(player):
raise SystemExit("Game over!")
# refresh screen
black = pygame.Color((0,0,0))
view = screen.fill(color=black)
# render assets in the "drawable" container
for drawable_asset in drawable:
drawable_asset.draw(screen)
# flip the buffers
pygame.display.flip()
if __name__ == "__main__":
main()