Files
asteroids/main.py
2024-10-28 21:39:11 -06:00

57 lines
1.5 KiB
Python

# this allows us to use code from
# the open-source pygame library
# throughout this file
import pygame
from constants import SCREEN_HEIGHT, SCREEN_WIDTH
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from shot import Shot
def main():
pygame.init()
clock = pygame.time.Clock()
dt = 0
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
asteroids = pygame.sprite.Group()
updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group()
shots = pygame.sprite.Group()
Asteroid.containers = (asteroids, updatable, drawable)
AsteroidField.containers = updatable
Shot.containers = (shots, updatable, drawable)
asteroid_field = AsteroidField()
Player.containers = (updatable, drawable)
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
screen.fill("black")
# limit the framerate to 60 FPS
dt = clock.tick(60) / 1000
for object in drawable:
object.draw(screen)
for object in updatable:
object.update(dt)
for asteroid in asteroids:
if asteroid.collision(player):
print("Game over!")
return
for bullet in shots:
if asteroid.collision(bullet):
bullet.kill()
asteroid.split()
pygame.display.flip()
if __name__ == "__main__":
main()