开发者

PyGame code doesn't execute properly when event is executed

开发者 https://www.devze.com 2023-01-29 20:16 出处:网络
I am trying to make the most simple pythong code that will respond whe开发者_开发问答n a button is pressed on a joystick. I used code from several different examples and I still cannot get it to work.

I am trying to make the most simple pythong code that will respond whe开发者_开发问答n a button is pressed on a joystick. I used code from several different examples and I still cannot get it to work. The following code will not dispatch the event when I press the trigger (or any button for that matter)

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


I assume you've tried leaving off the if and just printing str? Your joystick might also not be working properly. Does it work in other programs?

If you are using linux you might need to install a joystick driver. For Windows, check the device manager.

0

精彩评论

暂无评论...
验证码 换一张
取 消