サンプルコードをよく見れば気付いているべきだったのですが……
JoystickにもJOYAXISMOTION等のイベントが割付られているもの、同じ角度にしたままだとイベントとして登録されないことがあります。
動作範囲制限をつけようと試行錯誤していたところ……そもそも、軸の値はイベントによらず取得していることに気づき少し実験してみました。
実際のところ
第一軸(左右方向)の傾き値を10fps毎に合算し、目標値に到達したら終了するようなサンプルスクリプトは以下の通り。
import pygame from pygame.locals import * import datetime pygame.init() screen = pygame.display.set_mode((100, 100)) pygame.display.set_caption("Joystick example") joystick = pygame.joystick.Joystick(0) # create a joystick instance joystick.init() # init instance # Used to manage how fast the screen updates. clock = pygame.time.Clock() done = False distination = 0.0 while not done: for event in pygame.event.get(): if event.type == pygame.JOYBUTTONDOWN: print("\nJoystick button pressed.") quit() elif event.type == KEYDOWN and event.key == K_ESCAPE: print("\nEsc pressed.") quit() now = datetime.datetime.now() dt = now.strftime('%H:%M:%S.%f')[:-3] axis = joystick.get_axis(0) print(f"\r{dt:>12} {axis:>6.3f} => {distination:>6.3f}",end='') distination += axis if (distination > 100 ): print("done") done = True # Go ahead and update the screen with what we've drawn. pygame.display.flip() # Limit to 30 frames per second. clock.tick(10)
実行して少しジョイコン一軸目をうごかし、放置しボタンて止めるとこんな感じ。
ジョイコンの値はゼロにはならない。
$ python3 fot.py pygame 2.1.2 (SDL 2.0.16, Python 3.8.10) Hello from the pygame community. https://www.pygame.org/contribute.html 22:18:07.125 0.004 => 28.488 Joystick button pressed.