[在带有python3.7 Windows 10的pygame 1.9.4上运行,按下键时,我没有收到任何KEYDOWN和KEYUP事件。
我编写了许多pygame程序,并认为我了解事件处理,但这让我很困惑。向循环添加event.pump()没什么区别。实际的击键以ctrl-C终止后会显示在shell中。
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
frame = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
if event.type == pygame.KEYDOWN:
print('Keydown')
if event.key == pygame.K_z:
print('z down')
if pygame.key.get_pressed(pygame.K_z):
print('z pressed')
if event.type == pygame.KEYUP:
print('Keyup')
if event.key == pygame.K_z:
print('z up')
pygame.display.flip()
clock.tick(1)
frame +=1
print(frame)
`
尝试用这种方式使您的播放器移动,我想您可以包括print('z down')部分
moving_right = False
moving_left = False
while True:
moving_right == True:
player_location[0] += 4
if moving_left == True:
player_location[0] -= 4
for event in pygame.event.get(): # event loop
if event.type == QUIT: # check for window quit
pygame.quit() # stop pygame
sys.exit() # stop script
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moving_right = True
if event.key == K_LEFT:
moving_left = True
if event.type == KEYUP:
if event.key == K_RIGHT:
moving_right = False
if event.key == K_LEFT:
moving_left = False