Search code examples
pythonpygameuser-input

Why is this code for checking if two keys being pressed in pygame not working?


This code below is a portion of code I wrote using pygame just to check if left key AND/OR right key are being pressed and to move the player img accordingly. All lines besides the final one is inside the while loop that runs and updates the game. Pressing left should make left_pressed true and set playerX_change to -0.1 (which is used to update the location of the player). Pressing the right key should do the opposite.

All functionality works with the left key, the img moves and the message prints in the console.

However, the right key does not work. Using print to test some things showed that pressing the right key does set right_pressed to True (at least for a moment) but when right_pressed is checked later on it is now False and will not move the img or print a message.

(Also that final elif statement is so that if left and right are pressed at the same time the player img will not move.)

Any ideas for why the right key is different from the left in this situation or why right_pressed might be switching to False too quickly would be amazing.

Any help is appreciated! Thanks! :) '''

    keys_pressed = pygame.key.get_pressed()
    right_pressed = False
    left_pressed = False

    if keys_pressed[pygame.K_RIGHT]:
        right_pressed = True
    elif keys_pressed[pygame.K_LEFT]:
        left_pressed = True

    if pygame.K_RIGHT not in keys_pressed:
        right_pressed = False
    elif pygame.K_LEFT not in keys_pressed:
        left_pressed = False

    if right_pressed:
        playerX_change = 0.1
        print("Right pressed")
    elif left_pressed:
        playerX_change = -0.1
        print("Left pressed")
    elif right_pressed and left_pressed:
        playerX_change = 0
        print("Right and left pressed")
    else:
        playerX_change = 0

playerX += playerX_change

'''


Solution

  • Because your using if and elif, if the first if is True the code will never process the elif. So in this Case, if keys_pressed[pygame.K_RIGHT] is True, it will never look to check if keys_pressed[pygame.K_LEFT] is also True.

    So make this change:

    if keys_pressed[pygame.K_RIGHT]:
        right_pressed = True
    # elif keys_pressed[pygame.K_LEFT]:
    if keys_pressed[pygame.K_LEFT]:
        left_pressed = True
    

    You set right_pressed and left_pressed to False at the start of the Code. They are only changed if the key is pressed, so there is no need to set them back to False.

    Finally, you should check right_pressed and left_pressed first. Otherwise, due to the if and elif behavior I mentioned earlier, if one of those are True it will never make it to the Code that checks for both.

    I've reduced the code, have a try with this. It will check if Right is pressed and not Left, then if Left is pressed and not Right. Otherwise, playerX_change will be set to 0 (as that is the value for either both pressed or none pressed).

    keys_pressed = pygame.key.get_pressed()
    
    if keys_pressed[pygame.K_RIGHT] and not keys_pressed[pygame.K_LEFT]:
        playerX_change = 0.1
        print("Right pressed")
    elif keys_pressed[pygame.K_LEFT] and not keys_pressed[pygame.K_RIGHT]:
        playerX_change = -0.1
        print("Left pressed")
    else:
        playerX_change = 0
    
    playerX += playerX_change