rate limited shooting

This commit is contained in:
specCon18 2025-01-21 03:03:38 -05:00
parent cf21f5ff0a
commit f49a105d21
2 changed files with 16 additions and 8 deletions

View file

@ -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

View file

@ -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