2010-12-11 87 views
1

我正在尝试制作最简单的pythong代码,它会在按下游戏杆上的按钮时作出响应。我使用了几个不同示例的代码,但仍然无法实现。当我按下触发(或与此有关的任何按钮)当执行事件时,PyGame代码无法正确执行

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() 
+0

我试过你的代码,它为我工作。因此,您的电脑可能会识别您的操纵杆或其他问题。您是否已经成功地使用其他应用程序试用过游戏杆 – 2010-12-11 15:16:51

回答

1

我想你已经尝试过离开的,如果,只是打印海峡下面的代码将不会分派事件? 您的操纵杆可能无法正常工作。它在其他程序中工作吗?

如果您使用的是linux,则可能需要安装游戏杆驱动程序。对于Windows,请检查设备管理器。