简体   繁体   English

Pygame中的字符移动归零

[英]Character movement in Pygame Zero

As of right now, my character moves whilst I'm holding my key down.截至目前,我的角色在我按住键的同时移动。

eg例如

if keyboard.left:
   actor.x -= 5

However, I would like my actor to move in a sort of block-ish fashion eg I press my key once and it moves to the left by 5 pixels, regardless of whether I hold it down or not.但是,我希望我的演员以一种块状的方式移动,例如,我按一次键,无论我是否按住它,它都会向左移动 5 个像素。

How would I do this?我该怎么做?

First第一的

if keyboard.left: 
    move_direction = 'left'

and later然后

if move_direction == 'left':
    actor.x -= 5

and don't reset move_direction .并且不要重置move_direction


Eventually it may need to keep only left or right to stop moving left only when you press right but not when you press top or bottom .最终它可能只需要保持leftright来停止向左移动,只有当你按下right而不是按下topbottom时。

First第一的

if keyboard.left: 
    move_horizontally = 'left'

if keyboard.right: 
    move_horizontally = 'right'

and later然后

if move_horizontally == 'left':
    actor.x -= 5
if move_horizontally == 'right':
    actor.x += 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM