【发布时间】:2010-12-11 10:00:23
【问题描述】:
我正在尝试制作最简单的 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()
【问题讨论】:
-
我试过你的代码,它对我有用。因此,您的计算机识别您的操纵杆或其他东西可能存在问题。您是否在其他应用程序中成功尝试过操纵杆?