From 6d45b7245fdaaa0d35e429b69c9ac14ee940a841 Mon Sep 17 00:00:00 2001 From: yuzu-eva Date: Fri, 20 Sep 2024 18:43:29 +0200 Subject: add collision for bullets --- constants.py | 1 + main.py | 4 ++++ player.py | 12 +++++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/constants.py b/constants.py index 87070cd..119909f 100644 --- a/constants.py +++ b/constants.py @@ -10,5 +10,6 @@ PLAYER_RADIUS = 20 PLAYER_TURN_SPEED = 300 PLAYER_SPEED = 200 PLAYER_SHOOT_SPEED = 500 +PLAYER_SHOOT_COOLDOWN = 0.3 SHOT_RADIUS = 5 diff --git a/main.py b/main.py index 6081f2a..2a2328d 100644 --- a/main.py +++ b/main.py @@ -36,6 +36,10 @@ def main(): for obj in updatable: obj.update(dt) for asteroid in asteroids: + for bullet in shots: + if bullet.collision_check(asteroid): + bullet.kill() + asteroid.kill() if asteroid.collision_check(player): print("Game over!") return diff --git a/player.py b/player.py index 9b7f4bc..8fbcdc3 100644 --- a/player.py +++ b/player.py @@ -7,6 +7,7 @@ class Player(CircleShape): def __init__(self, x, y): super().__init__(x, y, PLAYER_RADIUS) self.rotation = 0 + self.timer = 0 # in the player class def triangle(self): @@ -25,6 +26,7 @@ class Player(CircleShape): def update(self, dt): keys = pygame.key.get_pressed() + self.timer -= dt if keys[pygame.K_LEFT]: self.rotate(-dt) if keys[pygame.K_RIGHT]: @@ -34,12 +36,16 @@ class Player(CircleShape): if keys[pygame.K_DOWN]: self.move(-dt) if keys[pygame.K_SPACE]: - self.shoot(self.position) + self.shoot() def move(self, dt): forward = pygame.Vector2(0, 1).rotate(self.rotation) self.position += forward * PLAYER_SPEED * dt - def shoot(self, position): - shot = Shot(position.x, position.y, SHOT_RADIUS) + def shoot(self): + if self.timer > 0: + return + shot = Shot(self.position.x, self.position.y, SHOT_RADIUS) shot.velocity = pygame.Vector2(0, 1).rotate(self.rotation) * PLAYER_SHOOT_SPEED + self.timer = PLAYER_SHOOT_COOLDOWN + -- cgit v1.2.3