From ae27a3c7a0bb54ecf9cc7ce4a5a49f2c52bd13cb Mon Sep 17 00:00:00 2001 From: franky212 Date: Mon, 28 Oct 2024 17:31:31 -0600 Subject: [PATCH] Adding asteroids/fields and collision --- asteroid.py | 11 +++++++++++ asteroidfield.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ circleshape.py | 5 +++++ main.py | 22 +++++++++++++++++---- 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 asteroidfield.py diff --git a/asteroid.py b/asteroid.py index e69de29..44fa349 100644 --- a/asteroid.py +++ b/asteroid.py @@ -0,0 +1,11 @@ +import pygame +from circleshape import CircleShape +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, self.radius, 2) + + def update(self, dt): + self.position += (self.velocity * dt) \ No newline at end of file diff --git a/asteroidfield.py b/asteroidfield.py new file mode 100644 index 0000000..74d9d57 --- /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) \ No newline at end of file diff --git a/circleshape.py b/circleshape.py index a6cb536..e595f1c 100644 --- a/circleshape.py +++ b/circleshape.py @@ -13,6 +13,11 @@ class CircleShape(pygame.sprite.Sprite): self.velocity = pygame.Vector2(0, 0) self.radius = radius + def collision(self, other): + if self.position.distance_to(other.position) <= (self.radius + other.radius): + return True + return False + def draw(self, screen): # sub-classes must override pass diff --git a/main.py b/main.py index e4c52c2..f72b6ea 100644 --- a/main.py +++ b/main.py @@ -4,17 +4,26 @@ import pygame from constants import SCREEN_HEIGHT, SCREEN_WIDTH from player import Player +from asteroid import Asteroid +from asteroidfield import AsteroidField 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() + + Asteroid.containers = (asteroids, updatable, drawable) + AsteroidField.containers = updatable + asteroid_field = AsteroidField() + + Player.containers = (updatable, drawable) + player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) - updatable = [] - drawable = [] - updatable.append(player) - drawable.append(player) while True: for event in pygame.event.get(): @@ -30,6 +39,11 @@ def main(): for object in updatable: object.update(dt) + for asteroid in asteroids: + if asteroid.collision(player): + print("Game over!") + return + pygame.display.flip() if __name__ == "__main__":