I am struggling how I can make it so that when a key is pressed in Pygame, you aren't able to keep on holding the key. I am working on player jumps, but it doesn't work unless the key is only pressed once. Here is my keypress code:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and not self.jumping:
self.jumping = True
And here is my jumping method (which also includes gravity)
def gravity(self):
# if the player isn't jumping
if not self.jumping:
# set acceleration constant and accelerate
self.accel_y = PLAYER_ACCELERATION
self.y_change += self.accel_y
# terminal velocity
if abs(self.y_change) >= PLAYER_MAX_SPEED_Y:
# set the y change to the max speed
self.y_change = PLAYER_MAX_SPEED_Y
self.y += self.y_change
else: # jumping
self.accel_y = -PLAYER_ACCELERATION_JUMPING
self.y_change += self.accel_y
if abs(self.y_change) >= PLAYER_MAX_SPEED_JUMPING:
self.jumping = False
self.y += self.y_change
print(self.y_change)
The player can just keep on holding the up arrow key, and the player will fly - which is not good. I would like it so that you can only press once, and then self.jumping
is set to False
when it has reached it's top speed (which I have already implemented)