如何在pygame中使用键盘?

时间:2015-01-17 01:31:46

标签: keyboard pygame

我尝试了下面的简单脚本,但我没有得到回应。也许我不明白pygame能做什么。我只想将某个功能分配给某个键。就像我按下字母'h'我想要“再见”在屏幕上打印或者如果我按'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

1 个答案:

答案 0 :(得分:1)

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"

对于我至少有什么理由,似乎事件命令不能用于屏幕的使用,即使它没有任何目的,这将得到你想要的结果。

相关问题