I am making a game that is similar to Google’s Dino game & I am currently working on the player’s controls. I want to make it so that the space key for jumping couldn’t be spammed because it would make the game pointless otherwise if people can just spam the space key forever to avoid obstacles. I’m trying to code it in a way where the space key can only be pressed two times consecutively (to make the player double jump) & set an interval so that it couldn’t be pressed more than two times in a certain amount of time, but so far, to no avail. How can I achieve this?
Here are snippets of my code:
class Player():
def __init__(self, x, y):
self.image = aru
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.vel_y = 0
self.jumped = False
def update(self):
δx = 0
δy = 0
# Controls
kei = pygame.key.get_pressed()
if kei[pygame.K_SPACE] and self.jumped == False:
self.vel_y = -15
self.jumped = True
if kei[pygame.K_SPACE] == False:
self.jumped = False
if kei[pygame.K_a]:
δx -= 10
if kei[pygame.K_d]:
δx += 10