2026-05-21 15:07:53 -05:00
|
|
|
"""Entrypoint for asteroids game"""
|
|
|
|
|
|
2026-05-22 09:34:56 -05:00
|
|
|
import sys
|
|
|
|
|
|
2026-05-21 15:07:53 -05:00
|
|
|
import pygame
|
|
|
|
|
|
2026-05-22 09:34:56 -05:00
|
|
|
from asteroid import Asteroid
|
|
|
|
|
from asteroidfield import AsteroidField
|
2026-05-21 15:10:25 -05:00
|
|
|
from constants import SCREEN_HEIGHT, SCREEN_WIDTH
|
2026-05-22 09:34:56 -05:00
|
|
|
from logger import log_event, log_state
|
|
|
|
|
from player import Player
|
|
|
|
|
from shot import Shot
|
|
|
|
|
|
2026-05-21 15:10:25 -05:00
|
|
|
|
2026-05-21 15:07:53 -05:00
|
|
|
def main():
|
|
|
|
|
"""Main method for asteroids game"""
|
2026-05-22 09:34:56 -05:00
|
|
|
|
2026-05-21 15:07:53 -05:00
|
|
|
print(f"Starting Asteroids with pygame version: {pygame.version.ver}")
|
2026-05-21 15:10:25 -05:00
|
|
|
print(f"Screen width: {SCREEN_WIDTH}")
|
|
|
|
|
print(f"Screen height: {SCREEN_HEIGHT}")
|
2026-05-22 09:34:56 -05:00
|
|
|
|
2026-05-21 15:29:17 -05:00
|
|
|
pygame.init()
|
2026-05-22 09:34:56 -05:00
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
dt = 0.0
|
|
|
|
|
|
2026-05-21 15:29:17 -05:00
|
|
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
|
|
|
|
2026-05-22 09:34:56 -05:00
|
|
|
asteroids = pygame.sprite.Group()
|
|
|
|
|
updatable = pygame.sprite.Group()
|
|
|
|
|
drawable = 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_WIDTH / 2, SCREEN_HEIGHT / 2)
|
|
|
|
|
AsteroidField()
|
|
|
|
|
|
2026-05-21 15:29:17 -05:00
|
|
|
while True:
|
|
|
|
|
log_state()
|
|
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
|
return
|
|
|
|
|
|
2026-05-22 09:34:56 -05:00
|
|
|
for u in updatable:
|
|
|
|
|
u.update(dt)
|
|
|
|
|
|
|
|
|
|
for a in asteroids:
|
|
|
|
|
if a.collides_with(player):
|
|
|
|
|
log_event("player_hit")
|
|
|
|
|
print("Game over!")
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
for s in shots:
|
|
|
|
|
if a.collides_with(s):
|
|
|
|
|
log_event("asteroid_shot")
|
|
|
|
|
s.kill()
|
|
|
|
|
a.split()
|
|
|
|
|
|
2026-05-21 15:29:17 -05:00
|
|
|
screen.fill("black")
|
2026-05-22 09:34:56 -05:00
|
|
|
|
|
|
|
|
for d in drawable:
|
|
|
|
|
d.draw(screen)
|
|
|
|
|
|
2026-05-21 15:29:17 -05:00
|
|
|
pygame.display.flip()
|
2026-05-22 09:34:56 -05:00
|
|
|
dt = clock.tick(60) / 1000
|
|
|
|
|
|
2026-05-21 15:07:53 -05:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|