1

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.

Kingsley
  • 14,398
  • 5
  • 31
  • 53

1 Answers1

1

All you have to do is evaluate that the speed is 0 when a key is pressed:

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 self.player.speedx == 0 and self.player.speedy == 0:
                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
            # [...]

Another option is to reset the speed when the a key is pressed:

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
                self.player.speedy = 0
            if event.key == pygame.K_RIGHT:
                self.player.speedx = 48
                self.player.speedy = 0
            if event.key == pygame.K_UP:
                self.player.speedx = 0
                self.player.speedy = -48
            if event.key == pygame.K_DOWN:
                self.player.speedx = 0
                self.player.speedy = 48

The two implementations behave differently. While the first implementation does not allow you to change direction while a key is pressed, the second implementation changes direction immediately. Try both and choose the one that suits your needs.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174