-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (50 loc) · 1.69 KB
/
main.py
File metadata and controls
60 lines (50 loc) · 1.69 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
import pygame
import sys
from constants import SCREEN_WIDTH, SCREEN_HEIGHT, PLAYER_RADIUS, LINE_WIDTH
from logger import log_state, log_event
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from shot import Shot
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
py_clock = pygame.time.Clock()
dt = 0
## Creating game groups
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)
Shot.containers = (shots, drawable, updatable)
AsteroidField.containers = (updatable,)
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
asteroid_field = AsteroidField()
## Main game loop
while True:
log_state()
screen.fill("black")
for object in drawable:
object.draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
pygame.display.flip()
dt = py_clock.tick(60) / 1000
updatable.update(dt)
# Collision detection
for asteroid in asteroids:
if player.collides_with(asteroid):
log_event("player_hit")
print("Game over!")
sys.exit()
for asteroid in asteroids:
for shot in shots:
if asteroid.collides_with(shot):
log_event("asteroid_shot")
asteroid.split()
shot.kill()
if __name__ == "__main__":
main()