在pygame中调用get_axis()之后不要打印结果

时间:2014-11-17 15:40:31

标签: python pygame

我正在编写一个读取操纵杆轴值的代码,然后将该值发送给arduino。它可以工作,但我不希望它在没有任何按下的情况下不断向LXTerminal打印0。

当未按下轴时,代码可重复打印: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(),代码就无法运行。这是必要的,以便&#34; get_axis()&#34;实际上返回轴的值。

1 个答案:

答案 0 :(得分:1)

这与此问题Disable the Pygame console Output和主题:http://archives.seul.org/pygame/users/Aug-2009/msg00110.html有关。这似乎已在bitbucket源https://bitbucket.org/pygame/pygame/downloads中修复,您可以尝试从那里下载该版本。