Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (52 loc) · 1.77 KB
/
Copy pathmain.py
File metadata and controls
69 lines (52 loc) · 1.77 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
import pygame
import sys
from constants import SCREEN_HEIGHT, SCREEN_WIDTH
from logger import log_event, log_state
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from shot import Shot
import shot
def main():
print(f"Starting Asteroids with pygame version: {pygame.version.ver}")
print(f"Screen width: {SCREEN_WIDTH}")
print(f"Screen height: {SCREEN_HEIGHT}")
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
dt = 0
updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
Player.containers = (updatable, drawable)
Asteroid.containers = (asteroids, updatable, drawable)
AsteroidField.containers = (updatable)
Shot.containers = (updatable, drawable, shots)
player = Player(screen.get_width()/2, screen.get_height()/2)
asteroidField = AsteroidField()
while True:
log_state()
screen.fill("Black")
for thing in updatable:
thing.update(dt)
for thing in drawable:
thing.draw(screen)
for thing in asteroids:
if player.collides_with(thing):
log_event("player_hit")
print("Game over!")
sys.exit()
for thing in asteroids:
for shot in shots:
if shot.collides_with(thing):
log_event("asteroid_shot")
shot.kill()
thing.split()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
pygame.display.flip()
dt = clock.tick(60) / 1000
if __name__ == "__main__":
main()