My goal is a 2 player pygame played on one keyboard where each player can move up, down, left and right.
I wonder if it is preferable to use only the event module or also the key module for this. Most examples I've seen only use the key module, like so:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
The problem with this: When left and right are pressed at the same time and then one key is lifted, x_change is set to zero.
I tried fixing this with a variable that tracks how many keys are pressed, but no success. So I am wondering if the above method is just not the proper way to do it. I have hope that the pygame.key module solves my problem but have problems understanding the documentation.
Anyone with pygame experience here who can recommend when to use which module?