Pygame Joystick Bizarre Behavior

Started by
0 comments, last by ChocolatePancakes 4 years, 7 months ago

I have this weird glitch: whenever I am pressing the D-pad on the 360 controller, the A-button also lights up. The A-button returns a 1 for positive and 0 for negative. It returns a 1 whenever I am pressing the 4 directions of the D-pad. Does anyone know how to solve this? By the way, I am using Linux Mint, Python 3.6.8, and Pygame 1.9.6

The source code for the class is below:



# Xbox 360 Controller class
class xpad:
    def __init__(self, pygameJoystick, deadzone):

        self.ID = pygameJoystick.get_id()

        pygameJoystick.init()

        self.deadzone = deadzone

        # Xbox 360 Controller Name
        self.name = pygameJoystick.get_name()

        # Thumbsticks
        self.left_thumb  = (
            self.dead( pygameJoystick.get_axis(0) ),
            self.dead( pygameJoystick.get_axis(1) )
            )
        self.right_thumb = (
            self.dead( pygameJoystick.get_axis(3) ),
            self.dead( pygameJoystick.get_axis(4) )
            )

        # Triggers
        self.left_trig   = pygameJoystick.get_axis(2)
        self.right_trig  = pygameJoystick.get_axis(5)

        # Buttons
        self.A = pygameJoystick.get_button(0)
        self.B = pygameJoystick.get_button(1)
        self.X = pygameJoystick.get_button(2)
        self.Y = pygameJoystick.get_button(3)

        self.LB = pygameJoystick.get_button(4)
        self.RB = pygameJoystick.get_button(5)

        self.Back = pygameJoystick.get_button(6)
        self.Start = pygameJoystick.get_button(7)

        # The center button
        self.Guide = pygameJoystick.get_button(8)
        
        # When you click the thumbsticks
        self.left_stick = pygameJoystick.get_button(9)
        self.right_stick = pygameJoystick.get_button(10)

        # The directional pad on the 360 controller
        self.dpad = pygameJoystick.get_hat(0)

    # Limits a particular axis
    # by returning its value if it is
    # within the set deadzone. Returns a
    # zero otherwise.
    def dead(self, axis):
        if ( abs(axis) >= self.deadzone ):
            return axis
        
        return 0

    # Returns a string that can be sent over the network
    # and be easily interpreted by an Arduino.
    def serialized(self):
        packet = ("<%i,%s,%s,%s,%s>")%(
            self.ID,
            hex( int(256*( (self.left_thumb[0]+1)/2  )) ),
            hex( int(256*( (self.left_thumb[1]+1)/2  )) ),
            hex( int(256*( (self.right_thumb[0]+1)/2 )) ),
            hex( int(256*( (self.right_thumb[1]+1)/2 )) )
        )

        return packet

Here is how you can visualize the problem:


import pygame
import xinput


# Define some colors.
BLACK = pygame.Color('black')
WHITE = pygame.Color('white')


# This is a simple class that will help us print to the screen.
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint(object):
    def __init__(self):
        self.reset()
        self.font = pygame.font.Font(None, 20)

    def tprint(self, screen, textString):
        textBitmap = self.font.render(textString, True, BLACK)
        screen.blit(textBitmap, (self.x, self.y))
        self.y += self.line_height

    def reset(self):
        self.x = 10
        self.y = 10
        self.line_height = 15

    def indent(self):
        self.x += 10

    def unindent(self):
        self.x -= 10


pygame.init()

# Set the width and height of the screen (width, height).
screen = pygame.display.set_mode((500, 700))

pygame.display.set_caption("ECU Robotics Xbox 360 Controller Layout")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates.
clock = pygame.time.Clock()

# Initialize the joysticks.
pygame.joystick.init()

# Get ready to print.
textPrint = TextPrint()

# -------- Main Program Loop -----------
while not done:
    #
    # EVENT PROCESSING STEP
    #
    # Possible joystick actions: JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONDOWN,
    # JOYBUTTONUP, JOYHATMOTION
    for event in pygame.event.get(): # User did something.
        if event.type == pygame.QUIT: # If user clicked close.
            done = True # Flag that we are done so we exit this loop.
        elif event.type == pygame.JOYBUTTONDOWN:
            print("Joystick button pressed.")
        elif event.type == pygame.JOYBUTTONUP:
            print("Joystick button released.")

    #
    # DRAWING STEP
    #
    # First, clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.
    screen.fill(WHITE)
    textPrint.reset()

    # Initialize the connected Xbox controller
    xpad = xinput.xpad(pygame.joystick.Joystick(0), 0.25)

    # Print the controller's name
    textPrint.tprint(
        screen,
        xpad.name
    )
    textPrint.tprint(screen, '')

    # Begin main body...
    textPrint.indent()

    # Thumbsticks...
    textPrint.tprint(
        screen,
        ("Left Thumbstick (x, y): %.3f, %.3f")%(
            xpad.left_thumb[0],
            xpad.left_thumb[1]
        )
    )

    textPrint.tprint(
        screen,
        ("Right Thumbstick (x, y): %.3f, %.3f")%(
            xpad.right_thumb[0],
            xpad.right_thumb[1]
        )
    )

    textPrint.tprint(screen, '')

    # Triggers...
    textPrint.tprint(
        screen,
        ("Left Trigger: %.3f")%(
            xpad.left_trig
        )
    )

    textPrint.tprint(
        screen,
        ("Right Trigger: %.3f")%(
            xpad.right_trig
        )
    )

    textPrint.tprint(screen, '')

    # Buttons...
    textPrint.tprint(screen, ("A Button: %i")%(xpad.A))
    textPrint.tprint(screen, ("B Button: %i")%(xpad.B))
    textPrint.tprint(screen, ("X Button: %i")%(xpad.X))
    textPrint.tprint(screen, ("Y Button: %i")%(xpad.Y))

    textPrint.tprint(screen, ("Left Bumper: %i")%(xpad.LB))
    textPrint.tprint(screen, ("Right Bumper: %i")%(xpad.RB))

    textPrint.tprint(screen, ("Start Button: %i")%(xpad.Start))
    textPrint.tprint(screen, ("Back Button: %i")%(xpad.Back))

    textPrint.tprint(screen, ("Guide Button: %i")%(xpad.Guide))

    textPrint.tprint(screen, ("Left Stick Click: %i")%(xpad.left_stick))
    textPrint.tprint(screen, ("Right Stick Click: %i")%(xpad.right_stick))

    textPrint.tprint(screen, ("D-Pad: %i, %i")%(xpad.dpad[0], xpad.dpad[1]))

    # Serialized packet...
    textPrint.tprint(screen, '')

    textPrint.tprint(screen, 'Format: <ID, J1.x, J1.y, J2.x, J2.y>')
    textPrint.tprint(screen, ("%s")%(xpad.serialized()))

    #
    # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
    #

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # Limit to 20 frames per second.
    clock.tick(60)

# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()

 

Aiming high is easier than flying high.

This topic is closed to new replies.

Advertisement