如何在 pygame 中使用键盘?

How to use keyboard in pygame?

我尝试了下面的简单脚本,但没有得到任何响应。也许我不明白 pygame 能做什么。我只是想将某个功能分配给某个键。例如,如果我按下字母 'h' 我希望 "Bye" 打印在屏幕上,或者如果我按下 'g' 然后播放某首歌曲。等等等等...

import pygame
from pygame.locals import *

pygame.init()
print K_h #It prints 104
while True:
    for event in pygame.event.get():
        if event.type==KEYDOWN:
            if event.key==K_h:
                print "Bye" #Nothing is printed when I press h key
import pygame
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((640,480))
clock = pygame.time.Clock()

while True:
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == KEYDOWN:
            if event.key == K_h:
                print "bye"

不管出于什么原因,至少对我来说,事件命令似乎在不使用屏幕的情况下无法工作,即使它没有任何目的,这也会让你得到你想要的结果。