How to use pygame.KEYDOWN?

后端 未结 4 1427
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 12:59

So, my question was most likely already asked, but I didn\'t know what to search for, and didnt find much. So, my problem is, I made 2 functions, which would check for an e

4条回答
  •  不知归路
    2020-11-29 13:35

    I would advice you to stick with the event-driven approach rather than using a polling mechanism.

    You should let the key events alter some internal state to reflect a pressed key imo.

    Example: You are controlling a spaceship with your keyboard. You want the propulsion rockets to fire when you press one of 'w', 's', 'a' or 'd' to make the ship accelerate in a certain direction:

    • On pygame.KEYDOWN event, set an appropriate acceleration vector for the object if event.key in [K_w, K_s, K_a, K_d].
    • On pygame.KEYUP event, set the acceleration vector to the zero vector if event.key in [K_w, K_s, K_a, K_d].

    This will effectively make the object accelerate while a movement key is pressed, and stop accelerating when the key is released.

提交回复
热议问题