0

First of all, this is my first time asking a question on Stack Overflow so apologies if I mess anything up or ask a duplicate question. I usually try to look questions up before asking them, and honestly, this bug is part of a youtube tutorial, so I expect that it has been asked before, but I am having trouble wording it in a way that would make sense in a search query and what I have searched up has not led me to any substantial answer. For this bit of code, I want to set the speed to a certain amount and drop it to 0 when it is released, so that the player can hold down the key to keep moving until released.

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        playerXchange = -0.3
    if event.key == pygame.K_RIGHT:
        playerXchange = 0.3
if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
        playerXchange = 0

Normally, this works, but if you were to click left and then right without releasing the left key first, once you release left, but still have the right key pressed down, it stops moving because technically a KEPUP event has been detected even though the right is still pushed. This, of course, makes sense, and its doing exactly what is it supposed to, but I want to fix it so it keeps moving. I've thought of looking at the events to see if any keydowns are still being run and dropping playerXchange to 0 only if no keydowns are detected, but I couldn't figure out a way to do it. Again, apologies if a question like this has been asked and I would appreciate a link to it.

Klikbait
  • 11
  • 1

2 Answers2

1

Use pygame.key.get_pressed(). This returns a list with a 1 for each key held down, and a 0 for a key not held down, and is indexed using the pygame.K_LEFT, pygame.K_RIGHT constants.

playerXchange = 0

pressed = pygame.key.get_pressed()

if pressed[pygame.K_LEFT]:
    playerXchange = -0.3
elif pressed[pygamse.K_RIGHT]:
    playerXchange = 0.3
marienbad
  • 1,461
  • 1
  • 9
  • 19
  • cool, glad it helped! please click the tick by the answer so it sets it as an answer within the SO system and others can find it as well. – marienbad May 29 '21 at 01:08
0

`When you do

if pressed[pygame.K_LEFT]:
   playerXchange = -0.3
elif pressed[pygamse.K_RIGHT]:
   playerXchange = 0.3

You can only move right when left is released. If you hold down left and press right you are still moving left.

If you want to move depending on the last key pressed, you have to use a different logic in KEYUP. Set playerXchange = 0 only when the direction key of the current direction is released:

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        playerXchange = -0.3
    if event.key == pygame.K_RIGHT:
        playerXchange = 0.3
if event.type == pygame.KEYUP:

    # if move left and K_LEFT released
    if event.key == pygame.K_LEFT and playerXchange < 0:
        playerXchange = 0

    # if move right and K_RIGHT released:
    if event.key == pygame.K_RIGHT and playerXchange > 0:
        playerXchange = 0
Rabbid76
  • 202,892
  • 27
  • 131
  • 174