Pygame-如何允许我的用户更改其输入键? (自定义键绑定) [英] Pygame - How can I allow my users to change their input keys? (Custom Keybinding)
问题描述
我正在尝试制作游戏,希望用户更改其输入键,例如他们按下A键,并将MoveUp
变量更改为A键,以便在游戏中按下A时它们会向上移动.任何帮助或建议,将不胜感激.
I'm trying to make a game and want the user to change their input keys, e.g. they press the A key and that changes the MoveUp
variable to the A key so that when they press A in the game they move upwards. Any help or advice would be greatly appreciated.
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
移动我的精灵.
The current problem with this code is that it gives me a list that corresponds to the key pressed, I need a key identifier so that I can use MoveUp
to move my sprite later on.
推荐答案
当您收到事件KEYDOWN
时,请按event.key
When you get event KEYDOWN
then you have pressed key in event.key
while not Fin:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN
MoveUp = event.key
顺便说一句:每个事件可能都有不同的字段.您可以在文档的黄色列表中看到所有内容:事件
这篇关于Pygame-如何允许我的用户更改其输入键? (自定义键绑定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!