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.