I have created a Frogger Game Prototype and I'm wanting to not allow two keys to be pressed at the same time for movement. currently in my event function I have the following:
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.player.speedx = -48
if event.key == pygame.K_RIGHT:
self.player.speedx = 48
if event.key == pygame.K_UP:
self.player.speedy = -48
if event.key == pygame.K_DOWN:
self.player.speedy = 48
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
self.player.speedx = 0
self.player.speedy = 0
if event.key == pygame.K_RIGHT:
self.player.speedx = 0
self.player.speedy = 0
if event.key == pygame.K_UP:
self.player.speedx = 0
self.player.speedy = 0
if event.key == pygame.K_DOWN:
self.player.speedx = 0
self.player.speedy = 0
self.player.rect.x += self.player.speedx
self.player.rect.y += self.player.speedy
This allows the user to move left, right, up, and down. This is functioning properly although I don't want the user to be able to press two keys together ex: left, up or right, up. Also want to exclude the other direction as well ex: left, down or right, down. Basically I want the user only to be able to travel in one direction at a time. Also, is there anyway to put a time limit in between keypresses? For example you cannot press a key again before 1000 miliseconds have passed.