私はこれについての答えを探してきましたが、私はそれを見つけることができませんでした。キー入力はPygameで動作しません
私はこの問題は、単純なpygameスクリプトを書くためにSublime Text 3を使用するときにキー入力に応答することができないということです。私がキー(具体的に矢印キー)を押すたびに、これはゲームウィンドウではなくエディタに登録されます。マウス入力は登録されていますが、それはさらに奇妙です。
アドバイスはありますか?
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-with)とhttps://stackoverflow.com/questions/42512141/pygame-only-detects-mouse-events-does-not-detect-keydown-events?noredirect=1&lq=1 –