gpt4 book ai didi

python - Pygame 中的键盘输入不起作用

转载 作者:行者123 更新时间:2023-11-28 19:02:43 25 4
gpt4 key购买 nike

我正在尝试使用 pygame 和其他一些库创建一个虚拟合成器。可以通过两种方式播放音符:单击屏幕上的按钮或按下键盘上的键。单击屏幕时键盘输入不起作用。

以下代码是 Pygame 循环,其中指定了两种输入类型。

for event in pygame.event.get():

if event.type == pygame.QUIT:
crashed = True

if event.type == pygame.K_ESCAPE:
crashed = True

if pygame.mouse.get_pos()[0] in range(0, 50) and pygame.mouse.get_pos()[1] in range(400,
450) and event.type == pygame.MOUSEBUTTONDOWN:
button(sineCod, (0, 255, 0))
wave_1 = 0 # sine

elif pygame.mouse.get_pos()[0] in range(100, 150) and pygame.mouse.get_pos()[1] in range(400,
450) and event.type == pygame.MOUSEBUTTONDOWN:
button(sawCod, (0, 255, 0))
wave_1 = 1 # saw
elif pygame.mouse.get_pos()[0] in range(200, 250) and pygame.mouse.get_pos()[1] in range(400,
450) and event.type == pygame.MOUSEBUTTONDOWN:
button(squareCod, (0, 255, 0))
wave_1 = 2 # saw

elif pygame.mouse.get_pos()[0] in range(300, 350) and pygame.mouse.get_pos()[1] in range(400,
450) and event.type == pygame.MOUSEBUTTONDOWN:
button(triangleCod, (0, 255, 0))
wave_1 = 3

elif pygame.key.get_focused() is True:
print("Receiving Keyboard Focus")
if event.type == pygame.locals.KEYDOWN:
if event.key == pygame.locals.K_w: # C
play(frequencies[0])

if event.key == pygame.locals.K_2: # C#
play(frequencies[1])

if event.key == pygame.K_w: # D
play(frequencies[2])

if (pygame.mouse.get_pos()[0] in range(40) and pygame.mouse.get_pos()[1] in range(
190)) or (pygame.mouse.get_pos()[0] in range(75) and pygame.mouse.get_pos()[1] in range(190,
320)) and event.type == pygame.MOUSEBUTTONDOWN: # C
play(frequencies[0])

elif pygame.mouse.get_pos()[0] in range(75, 150) and pygame.mouse.get_pos()[1] in range(
320) and event.type == pygame.MOUSEBUTTONDOWN: # D
play(frequencies[2])

elif pygame.mouse.get_pos()[0] in range(150, 225) and pygame.mouse.get_pos()[1] in range(
320) and event.type == pygame.MOUSEBUTTONDOWN: # E
play(frequencies[4])

最佳答案

event.type 没有指定按下的键。这是通过 event.key

完成的

取而代之的是:

if event.type == pygame.K_ESCAPE:

你应该这样做:

if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:

或者

pressed = pygame.key.get_pressed()
if pressed[pygame.K_ESCAPE]:

关于python - Pygame 中的键盘输入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50780111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com