我正在编写一个 Pygame 程序,它需要用户输入来写音乐。它等待特定事件(即键盘或鼠标输入)并立即分析输入,而无需用户对每个条目按 Enter。
while (running == 1):
for event in pygame.event.get():
# If event is a keypress
if(event.type == pygame.KEYDOWN):
key = pygame.key.get_pressed() # get_pressed returns an boolean array
# of every key showing pressed or not
# Find which key is pressed
for x in range(len(key)):
if(key[x] == 1):
break
# If a number key is pressed (0-9)
if(x >= 48 and x <=57):
# Set the octave to keypress
gui.setOctave(x-48) # gui is an instance of a class
# that controlls pygame display
我知道这样做的唯一方法是使用无限的while循环。但是,这几乎占用了所有 CPU 功率来运行。有没有另一种有效的方法来解决这个问题而不使用循环?