Pygame-如何允许我的用户更改其输入键? (自定义键绑定) [英] Pygame - How can I allow my users to change their input keys? (Custom Keybinding)

查看:73
本文介绍了Pygame-如何允许我的用户更改其输入键? (自定义键绑定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作游戏,希望用户更改其输入键,例如他们按下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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆