pygame:检测摇杆断开连接,并等待它重新连接

Rya*_*ing 5 python pygame joystick gamepad

我正在使用一个pygame.joystick.Joystick对象并希望能够打印一条消息,要求用户在拔下 USB 游戏杆后重新连接它。

现在我有(大致):

js = pygame.joystick.Joystick(0)
#... some game code and stuff
pygame.joystick.quit()
pygame.joystick.init()
while pygame.joystick.get_count() == 0:
    print 'please reconnect joystick'
    pygame.joystick.quit()
    pygame.joystick.init()

js = pygame.joystick.Joystick(0)
js.init()
Run Code Online (Sandbox Code Playgroud)

但它没有正确重新连接,不知道它到底在做什么,但这绝对是错误的。这方面的任何方向都会有所帮助

小智 3

这是设备添加和删除的 pygame 事件。

joystick = pygame.joystick.Joystick(0)

while True:
    event = pygame.event.wait()

    if event.type == pygame.JOYDEVICEREMOVED:
        joystick.quit()

    elif event.type == pygame.JOYDEVICEADDED:
        joystick.init()
Run Code Online (Sandbox Code Playgroud)

https://www.pygame.org/docs/ref/joystick.html