如何将 pygame 键盘输入值存储在变量中

How do I store a pygame keyboard input value in a varibale

如何将 pygame 键盘输入值存储在变量中。 在 pygame.K_r 的地方,我如何使用我的键变量分配给键输入。

import pygame
import random
from sys import exit

pygame.init()


screen = pygame.display.set_mode((300, 500))
clock = pygame.time.Clock()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        **key = "r"**
        key_input = pygame.key.get_pressed()

        if key_input[pygame.**K_r**]:
            pass


    pygame.display.update()
    clock.tick(60)

只需将枚举常量存储在一个变量中:

key = pygame.K_r

以及用于订阅的变量并从序列中获取状态:

key_input = pygame.key.get_pressed()
if key_input[key]:

试试看,

import random
from sys import exit
import pygame

pygame.init()


screen = pygame.display.set_mode((300, 500))
clock = pygame.time.Clock()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        
        R = pygame.K_r    # variable R is used to assign the pygame keypress K_r
        key_input = pygame.key.get_pressed()

        if key_input[R]:   #use varibale R to check if r is pressed
            print('Pressed')


    pygame.display.update()
    clock.tick(60)