5

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

しかし、正しく再接続しません。正確に何をしているのかはわかりませんが、間違いなく間違っています。これに関する方向性は役に立ちます

4

2 に答える 2

2

古い xbox パッドを起動する必要がありましたが、切断をチェックして正常に動作するように見える関数を作成しました。

discon = False
def check_pad():
    global discon
    pygame.joystick.quit()
    pygame.joystick.init()
    joystick_count = pygame.joystick.get_count()
    for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()
    if not joystick_count: 
        if not discon:
           print "reconnect you meat bag"
           discon = True
        clock.tick(20)
        check_pad()
    else:
        discon = False

したがって、この関数をメイン ループで実行すると、ジョイスティックの接続が回復するまで実行し続けます。私が見つけた小さなテストコードで動作します:

http://programarcadegames.com/python_examples/show_file.php?file=joystick_calls.py

また見つけた:

http://demolishun.net/?p=21

私がアイデアを盗んだところ、彼は不自由なコード例を持っていませんでした

そして最後に、常にドキュメントをチェックする必要があるため:

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

于 2013-05-12T17:12:37.490 に答える