我正在尝试制作一个游戏,希望用户更改他们的输入键,例如,他们按a键,然后将MoveUp
变量更改为a键,这样当他们在游戏中按a键时,他们会向上移动。任何帮助或建议都将不胜感激
global MoveUp # MoveUp = pygame.K_UP
while not Fin:
for event in pygame.event.get():
pressed = pygame.key.pressed()
if event.type == pygame.KEYDOWN:
MoveUp = pressed
KeysLoop()
这段代码当前的问题是,它给了我一个与按下的键对应的列表,我需要一个键标识符,以便以后可以使用MoveUp
来移动我的精灵
当您得到事件
KEYDOWN
时,您已经按下了event.key
中的键顺便说一句:每个事件可能有不同的字段。您可以在文档中看到黄色列表中的所有内容:event
您可以创建一个字典,其中动作名称作为dict键,pygame键(
pygame.K_LEFT
等)作为值。例如:允许您为这些操作分配其他pygame键(在分配菜单的事件循环中):
然后,在主
while
循环中,可以使用动作名称检查是否按下了相应的键盘键:在下面的示例中,您可以通过单击
ESCAPE
键来访问assignment_menu
。它是一个单独的函数,有自己的while循环,我在其中创建了一个包含动作和pygame键的表,您可以用鼠标选择这些动作和键。如果选择了一个动作,用户按下一个键,我更新input_map
指令,并在用户再次按下Esc时将其返回到主游戏功能相关问题 更多 >
编程相关推荐