[英]PyGame code doesn't execute properly when event is executed
我正在嘗試制作最簡單的pythong代碼,當在操縱桿上按下按鈕時會響應。 我使用了來自幾個不同示例的代碼,但仍然無法正常工作。 當我按下觸發器(或與此有關的任何按鈕)時,以下代碼將不會調度事件
import pygame
joy = []
def handleJoyEvent(e):
if e.type == pygame.JOYBUTTONDOWN:
str = "Button: %d" % (e.dict['button'])
if (e.dict['button'] == 0):
print ("Pressed!\n")
else:
pass
def joystickControl():
while True:
e = pygame.event.wait()
if (e.type == pygame.JOYBUTTONDOWN):
handleJoyEvent(e)
# main method
def main():
pygame.joystick.init()
pygame.display.init()
for i in range(pygame.joystick.get_count()):
myjoy = pygame.joystick.Joystick(i)
myjoy.init()
joy.append(myjoy)
# run joystick listener loop
joystickControl()
# allow use as a module or standalone script
if __name__ == "__main__":
main()
我認為您已經嘗試過放棄if並且只打印str嗎? 您的游戲桿可能也無法正常工作。 它可以在其他程序中工作嗎?
如果您使用的是Linux,則可能需要安裝操縱桿驅動程序。 對於Windows,請檢查設備管理器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.