diff options
| author | yuzu-eva <stevenhu@web.de> | 2024-09-20 18:43:29 +0200 |
|---|---|---|
| committer | yuzu-eva <stevenhu@web.de> | 2024-09-20 18:43:29 +0200 |
| commit | 6d45b7245fdaaa0d35e429b69c9ac14ee940a841 (patch) | |
| tree | c34058d2df152d8e12e45ed0640ca71a948a19d3 | |
| parent | a00f4cf1eb4217483408dde11d9d0c7e763ab0a2 (diff) | |
add collision for bullets
| -rw-r--r-- | constants.py | 1 | ||||
| -rw-r--r-- | main.py | 4 | ||||
| -rw-r--r-- | 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 @@ -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 @@ -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 + |
