1
我正在寫一個代碼讀取操縱桿軸的值,然後將此值發送給arduino。它可以工作,但是我不希望它在沒有任何東西被按下時不斷地向LXTerminal輸出0。在pygame中調用get_axis()後不要打印結果
代碼打印時:SDL_JoystickGetAxis value:0:
當軸未被按下時可重複。
它然後打印:
SDL_JoystickGetAxis value:-32768:
SDL_JoystickGetAxis value:0:
SDL_JoystickGetAxis value:-32768:
The left drive is
-1.0
重複當軸被拉動一路向下,或-1和1之間的數取決於其中操縱桿。
我只希望打印The left drive is <leftDrive>
當左邊的軸被按下,然後什麼也不是。我如何實現這一目標?
這裏是我的代碼:
import pygame
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
leftDrive = 0
rightDrive = 0
while True:
try:
pygame.event.pump()
joyCheckLeft = joystick.get_axis(1)
joyCheckRight = joystick.get_axis(2)
if joyCheckLeft != 0:
pygame.event.pump()
leftDrive = joystick.get_axis(axisLeftDrive)
print 'The left drive is'
print leftDrive
writeNumber(int(2+(leftDrive*-10)))
pygame.event.pump()
time.sleep(0.1)
except KeyboardInterrupt:
quit()
注意:代碼不無pygame.event.pump()
工作。這是需要的,以便「get_axis()」實際返回軸的值。