rate limited shooting
This commit is contained in:
parent
cf21f5ff0a
commit
f49a105d21
2 changed files with 16 additions and 8 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
21
player.py
21
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)
|
||||
|
|
@ -43,17 +44,23 @@ class Player(circleshape.CircleShape):
|
|||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue