From f49a105d21df9776fbd83e70c6791b29fe053a75 Mon Sep 17 00:00:00 2001 From: specCon18 Date: Tue, 21 Jan 2025 03:03:38 -0500 Subject: [PATCH] rate limited shooting --- constants.py | 1 + player.py | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/constants.py b/constants.py index 445461a..c73d816 100644 --- a/constants.py +++ b/constants.py @@ -5,6 +5,7 @@ PLAYER_RADIUS = 20 PLAYER_ROTATION_SPEED = 300 PLAYER_SPEED = 200 PLAYER_SHOOT_SPEED = 500 +PLAYER_SHOOT_COOLDOWN = 0.3 SHOT_RADIUS = 5 diff --git a/player.py b/player.py index a15efa0..0faed81 100644 --- a/player.py +++ b/player.py @@ -6,6 +6,7 @@ from shot import Shot class Player(circleshape.CircleShape): containers = [] + cooldown = 0 def __init__(self,x,y): super().__init__(x,y,constants.PLAYER_RADIUS) @@ -42,18 +43,24 @@ class Player(circleshape.CircleShape): self.move(-dt) if keys[pygame.K_SPACE]: self.shoot() - + + if self.cooldown > 0: + self.cooldown = self.cooldown - dt + else: + self.cooldown = 0 def move(self,dt): forward = pygame.Vector2(0, 1).rotate(self.rotation) self.position += forward * constants.PLAYER_SPEED * dt def shoot(self): - # Create new shot at player position - shot = Shot(self.position.x, self.position.y) + if self.cooldown == 0: + # Create new shot at player position + shot = Shot(self.position.x, self.position.y) - # Calculate velocity (similar to your move method) - forward = pygame.Vector2(0, 1).rotate(self.rotation) - shot.velocity = forward * constants.PLAYER_SHOOT_SPEED + # Calculate velocity (similar to your move method) + forward = pygame.Vector2(0, 1).rotate(self.rotation) + shot.velocity = forward * constants.PLAYER_SHOOT_SPEED - # Add to container - Shot.containers[0].add(shot) + # Add to container + Shot.containers[0].add(shot) + self.cooldown = constants.PLAYER_SHOOT_COOLDOWN