简体   繁体   中英

Don't print result after calling get_axis() in pygame

I'm writing a code that reads the value of a joystick axis and then sends this value to an arduino. It works, however I do not want it constantly print 0 to the LXTerminal when nothing is being pressed.

The code prints: SDL_JoystickGetAxis value:0: repeatably when the axis is not being pressed.

It then prints:

SDL_JoystickGetAxis value:-32768:
SDL_JoystickGetAxis value:0:
SDL_JoystickGetAxis value:-32768:
The left drive is
-1.0

Repeatably when the axis is being pulled all the way down, or a number between -1 and 1 depending where the joystick is.

I only want it to print The left drive is <leftDrive> when the left axis is being pressed, and then nothing when it is not. How do I achieve this?

Here is my code:

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

Note: The code doesn't work without pygame.event.pump() . This is needed so that "get_axis()" actually returns the value of the axis.

这与此问题有关。 禁用Pygame控制台输出和线程: http : //archives.seul.org/pygame/users/Aug-2009/msg00110.html这似乎已在bitbucket源https://中修复。 bitbucket.org/pygame/pygame/downloads,您可以尝试从此处下载版本。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM