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
<小时>
顺便说一句:每个事件可能有不同的字段.您可以在文档中查看黄名单上的所有内容:event
这篇关于Pygame - 如何允许我的用户更改他们的输入键?(自定义键绑定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!