From 220e57f42fca7bd241041e5c808f62a3fa7cfa2a Mon Sep 17 00:00:00 2001 From: yuzu-eva Date: Fri, 20 Sep 2024 17:55:28 +0200 Subject: add asteroids --- asteroid.py | 11 +++++++++++ asteroidfield.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 23 ++++++++++++++++++++--- 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 asteroid.py create mode 100644 asteroidfield.py diff --git a/asteroid.py b/asteroid.py new file mode 100644 index 0000000..c77618c --- /dev/null +++ b/asteroid.py @@ -0,0 +1,11 @@ +from circleshape import * + +class Asteroid(CircleShape): + def __init__(self, x, y, radius): + super().__init__(x, y, radius) + + def draw(self, screen): + pygame.draw.circle(screen, "white", self.position, 2) + + def update(self, dt): + self.position += self.velocity * dt diff --git a/asteroidfield.py b/asteroidfield.py new file mode 100644 index 0000000..da82d41 --- /dev/null +++ b/asteroidfield.py @@ -0,0 +1,51 @@ +import pygame +import random +from asteroid import Asteroid +from constants import * + + +class AsteroidField(pygame.sprite.Sprite): + edges = [ + [ + pygame.Vector2(1, 0), + lambda y: pygame.Vector2(-ASTEROID_MAX_RADIUS, y * SCREEN_HEIGHT), + ], + [ + pygame.Vector2(-1, 0), + lambda y: pygame.Vector2( + SCREEN_WIDTH + ASTEROID_MAX_RADIUS, y * SCREEN_HEIGHT + ), + ], + [ + pygame.Vector2(0, 1), + lambda x: pygame.Vector2(x * SCREEN_WIDTH, -ASTEROID_MAX_RADIUS), + ], + [ + pygame.Vector2(0, -1), + lambda x: pygame.Vector2( + x * SCREEN_WIDTH, SCREEN_HEIGHT + ASTEROID_MAX_RADIUS + ), + ], + ] + + def __init__(self): + pygame.sprite.Sprite.__init__(self, self.containers) + self.spawn_timer = 0.0 + + def spawn(self, radius, position, velocity): + asteroid = Asteroid(position.x, position.y, radius) + asteroid.velocity = velocity + + def update(self, dt): + self.spawn_timer += dt + if self.spawn_timer > ASTEROID_SPAWN_RATE: + self.spawn_timer = 0 + + # spawn a new asteroid at a random edge + edge = random.choice(self.edges) + speed = random.randint(40, 100) + velocity = edge[0] * speed + velocity = velocity.rotate(random.randint(-30, 30)) + position = edge[1](random.uniform(0, 1)) + kind = random.randint(1, ASTEROID_KINDS) + self.spawn(ASTEROID_MIN_RADIUS * kind, position, velocity) diff --git a/main.py b/main.py index f62150f..43645dc 100644 --- a/main.py +++ b/main.py @@ -2,20 +2,37 @@ import pygame from constants import * from circleshape import * from player import * +from asteroid import * +from asteroidfield import * def main(): 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() + + Asteroid.containers = (asteroids, updatable, drawable) + AsteroidField.containers = (updatable) + + asteroidfield = AsteroidField() + + Player.containers = (updatable, drawable) player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) + + dt = 0 + while True: for event in pygame.event.get(): if event.type == pygame.QUIT or pygame.key.get_pressed()[pygame.K_q]: return screen.fill("black") - player.update(dt) - player.draw(screen) + for obj in updatable: + obj.update(dt) + for obj in drawable: + obj.draw(screen) pygame.display.flip() dt = clock.tick(60) / 1000 -- cgit v1.2.3