Can constant key input move a sprite constantly?

后端 未结 3 529
深忆病人
深忆病人 2021-01-16 05:31

If I want to move a sprite based on key input in pygame, is there anyway to write code so if I hold down a key, the sprite will keep moving in that direction until I release

3条回答
  •  青春惊慌失措
    2021-01-16 05:43

    You normally use have a moving function in your update method that looks like this:

    this.pos_x = direction * speed * delta
    

    Now on your KEYDOWN events you set your direction to 1 or -1 and on KEYUP you set it back to 0. That way you have a nice constant movement.

提交回复
热议问题