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_ROTATION_SPEED = 300
|
||||||
PLAYER_SPEED = 200
|
PLAYER_SPEED = 200
|
||||||
PLAYER_SHOOT_SPEED = 500
|
PLAYER_SHOOT_SPEED = 500
|
||||||
|
PLAYER_SHOOT_COOLDOWN = 0.3
|
||||||
|
|
||||||
SHOT_RADIUS = 5
|
SHOT_RADIUS = 5
|
||||||
|
|
||||||
|
|
|
||||||
23
player.py
23
player.py
|
|
@ -6,6 +6,7 @@ from shot import Shot
|
||||||
class Player(circleshape.CircleShape):
|
class Player(circleshape.CircleShape):
|
||||||
|
|
||||||
containers = []
|
containers = []
|
||||||
|
cooldown = 0
|
||||||
|
|
||||||
def __init__(self,x,y):
|
def __init__(self,x,y):
|
||||||
super().__init__(x,y,constants.PLAYER_RADIUS)
|
super().__init__(x,y,constants.PLAYER_RADIUS)
|
||||||
|
|
@ -42,18 +43,24 @@ class Player(circleshape.CircleShape):
|
||||||
self.move(-dt)
|
self.move(-dt)
|
||||||
if keys[pygame.K_SPACE]:
|
if keys[pygame.K_SPACE]:
|
||||||
self.shoot()
|
self.shoot()
|
||||||
|
|
||||||
|
if self.cooldown > 0:
|
||||||
|
self.cooldown = self.cooldown - dt
|
||||||
|
else:
|
||||||
|
self.cooldown = 0
|
||||||
def move(self,dt):
|
def move(self,dt):
|
||||||
forward = pygame.Vector2(0, 1).rotate(self.rotation)
|
forward = pygame.Vector2(0, 1).rotate(self.rotation)
|
||||||
self.position += forward * constants.PLAYER_SPEED * dt
|
self.position += forward * constants.PLAYER_SPEED * dt
|
||||||
|
|
||||||
def shoot(self):
|
def shoot(self):
|
||||||
# Create new shot at player position
|
if self.cooldown == 0:
|
||||||
shot = Shot(self.position.x, self.position.y)
|
# Create new shot at player position
|
||||||
|
shot = Shot(self.position.x, self.position.y)
|
||||||
|
|
||||||
# Calculate velocity (similar to your move method)
|
# Calculate velocity (similar to your move method)
|
||||||
forward = pygame.Vector2(0, 1).rotate(self.rotation)
|
forward = pygame.Vector2(0, 1).rotate(self.rotation)
|
||||||
shot.velocity = forward * constants.PLAYER_SHOOT_SPEED
|
shot.velocity = forward * constants.PLAYER_SHOOT_SPEED
|
||||||
|
|
||||||
# Add to container
|
# Add to container
|
||||||
Shot.containers[0].add(shot)
|
Shot.containers[0].add(shot)
|
||||||
|
self.cooldown = constants.PLAYER_SHOOT_COOLDOWN
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue