Python/Pygame:尝试使用一个变量(可能包含一个字符串)来表示Pygame条件

2024-10-01 02:23:05 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一篇博文,对任何礼仪上的失误提前表示歉意。你知道吗

我目前正在使用Python中的pygame来允许用户自定义什么键做什么。下面的代码是我目前的状况,但它不能解决我的问题。你知道吗

 if keys[pygame.K_UP]:

我真正想要的是这样的东西:

if keys["pygame.customisable_key"]:

据我所知,pygame序言必须包括在内,因为我设计导入这个类的方式。你知道吗

谢谢


Tags: key代码用户if方式keyspygameup
1条回答
网友
1楼 · 发布于 2024-10-01 02:23:05

^{}常量实际上只是数字(整数),您可以将其作为值放入keys字典中。你知道吗

我将定义一个字典,其中actions(字符串)作为dict键,pygame键constants作为值,例如:keys = {'move_left': pg.K_a, 'move_right': pg.K_d}。你知道吗

然后可以检入事件循环if event.key == keys['move_left']:,并根据需要处理该事件。你知道吗

要更改一个键,首先需要确定一个新键应该分配给哪个操作,当按下下一个键盘键时,插入event.key属性作为新值:keys[selected_action] = event.key。你知道吗

这只是一个简单的例子,仍然可以改进,但就我所知,它是完整的,工作正常(按Esc键切换分配场景,按前面的一个键选择一个动作,然后按另一个键为这个动作分配一个新的键):

import sys
import pygame as pg


def key_assignment_scene(keys, screen, clock):
    selected_action = None

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    return keys  # Return the keys to the main scene.
                elif selected_action:
                    # Assign a new key to the action.
                    keys[selected_action] = event.key
                    print(keys, 'new key:', event.unicode)
                    selected_action = None
                else:  # Select an action (a key from the `keys` dict).
                    for action, pygame_key in keys.items():
                        if event.key == pygame_key:
                            selected_action = action
                            print('Selected action:', selected_action)

        screen.fill((20, 40, 60))
        pg.display.flip()
        clock.tick(30)


def main():
    pg.init()
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    BG_COLOR = pg.Color('gray12')
    BLUE = pg.Color('dodgerblue1')
    rect = pg.Rect(300, 220, 20, 20)
    speed_x = 0
    keys = {'move_left': pg.K_a, 'move_right': pg.K_d}

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                return
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    print(keys)
                    # Switch to the key assigment scene.
                    keys = key_assignment_scene(keys, screen, clock)
                elif event.key == keys['move_left']:
                    speed_x = -3
                elif event.key == keys['move_right']:
                    speed_x = 3
            elif event.type == pg.KEYUP:
                if event.key == keys['move_left'] and speed_x < 0:
                    speed_x = 0
                elif event.key == keys['move_right'] and speed_x > 0:
                    speed_x = 0

        rect.x += speed_x

        screen.fill(BG_COLOR)
        pg.draw.rect(screen, BLUE, rect)
        pg.display.flip()
        clock.tick(30)

main()
pg.quit()

相关问题 更多 >