Joystick Test Programme

import pygame

# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)

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

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

size = [500, 800]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")

#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 done==False:
 # EVENT PROCESSING STEP
 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

screen.fill(WHITE)
 textPrint.reset()

joystick = pygame.joystick.Joystick(0)
 joystick.init()
 
 axes = 4 
 for i in range( axes ):
 axis = joystick.get_axis( i )
 aaa = axis + 1
 textPrint.prt(screen, "Axis {} value: {:>6.4f}".format(i, aaa) )
 textPrint.unindent()
 
 buttons = joystick.get_numbuttons()
 for i in range( buttons ):
 button = joystick.get_button( i )
 textPrint.prt(screen, "Button {:>2} value: {}".format(i,button) )
 textPrint.unindent()
 
 # Go ahead and update the screen with what we've drawn.
 pygame.display.flip()

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