asteroids/main.py

65 lines
1.6 KiB
Python
Raw Permalink Normal View History

2024-10-09 14:12:32 +01:00
import sys
import pygame
from constants import SCREEN_WIDTH, SCREEN_HEIGHT
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from shot import Shot
def main():
print("Starting asteroids!")
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 = (shots, updatable, drawable)
playerObj = Player(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
asteroidFieldObj = AsteroidField()
# this is the game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
for obj in updatable:
obj.update(dt)
for asteroid in asteroids:
if playerObj.is_colliding_with(asteroid):
print("Game over!")
sys.exit(0)
for shot in shots:
if shot.is_colliding_with(asteroid):
asteroid.split()
shot.kill()
screen.fill("black")
for obj in drawable:
obj.draw(screen)
pygame.display.flip()
dt = clock.tick(60) / 1000
if __name__ == "__main__":
main()