工作重點投入我一直在到處找這個答案,我似乎無法找到它。不pygame的
我遇到這個問題,使用文本崇高3寫一個簡單的pygame的腳本時,我不能讓它對鍵輸入響應。每當我按下一個按鍵(特別是箭頭鍵)時,只會在編輯器中註冊,而不是在遊戲窗口中註冊。鼠標輸入雖然會被註冊,但更加奇怪。
有沒有什麼建議?
import pygame
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A bit Racey')
black = (0, 0, 0)
white = (255, 255, 255)
clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')
x = (display_width * 0.45)
y = (display_height * 0.8)
def car(x, y):
gameDisplay.blit(carImg, (x, y))
x_change = 0
car_speed = 0
while not crashed:
#print(pygame.event.get_grab())
#pygame.event.set_grab(1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("Hello")
crashed = True
############################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.type == pygame.MOUSEBUTTONDOWN:
print("hello my baby")
######################
##
x += x_change
gameDisplay.fill(black)
car(x, y)
pygame.display.update()
pygame.display.flip()
clock.tick(30)
pygame.quit()
quit()
使用python2.7和pygame的1.9.3(而不是python3.5和pygame的1.9.4.dev0)沒有的伎倆我。 這些討論可以幫助:[bitbucket.org/pygame/pygame/issues/203/](https://bitbucket.org/pygame/pygame/issues/203/window-does-not-get-focus-on-os與-x)和https://stackoverflow.com/questions/42512141/pygame-only-detects-mouse-events-does-not-detect-keydown-events?noredirect=1&lq=1。 –