Advertisement
Guest User

Joystick Module

a guest
Sep 22nd, 2018
2,814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. import pygame
  2.  
  3. # Define some colors
  4. darkgrey = (40, 40, 40)
  5. lightgrey = (150, 150, 150)
  6.  
  7. # This is a simple class that will help us print to the screen
  8. # It has nothing to do with the joysticks, just outputting the
  9. # information.
  10. class TextPrint:
  11.     def __init__(self):
  12.         self.reset()
  13.         self.font = pygame.font.Font(None, 20)
  14.  
  15.     def print(self, screen, textString):
  16.         textBitmap = self.font.render(textString, True, lightgrey)
  17.         screen.blit(textBitmap, [self.x, self.y])
  18.         self.y += self.line_height
  19.        
  20.     def reset(self):
  21.         self.x = 10
  22.         self.y = 10
  23.         self.line_height = 15
  24.        
  25.     def indent(self):
  26.         self.x += 10
  27.        
  28.     def unindent(self):
  29.         self.x -= 10
  30.  
  31. pygame.init()
  32.  
  33. # Set the width and height of the screen [width,height]
  34. size = [500, 980]
  35. screen = pygame.display.set_mode(size)
  36.  
  37. pygame.display.set_caption("Joystick")
  38.  
  39. #Loop until the user clicks the close button.
  40. done = False
  41.  
  42. # Used to manage how fast the screen updates
  43. clock = pygame.time.Clock()
  44.  
  45. # Initialize the joysticks
  46. pygame.joystick.init()
  47.    
  48. # Get ready to print
  49. textPrint = TextPrint()
  50.  
  51. # -------- Main Program Loop -----------
  52. while done==False:
  53.     # EVENT PROCESSING STEP
  54.     for event in pygame.event.get(): # User did something
  55.         if event.type == pygame.QUIT: # If user clicked close
  56.             done=True # Flag that we are done so we exit this loop
  57.        
  58.         # Possible joystick actions: JOYAXISMOTION JOYBALLMOTION JOYBUTTONDOWN JOYBUTTONUP JOYHATMOTION
  59.         if event.type == pygame.JOYBUTTONDOWN:
  60.             print("Joystick button pressed.")
  61.         if event.type == pygame.JOYBUTTONUP:
  62.             print("Joystick button released.")
  63.            
  64.  
  65.     # DRAWING STEP
  66.     # First, clear the screen to white. Don't put other drawing commands
  67.     # above this, or they will be erased with this command.
  68.     screen.fill(darkgrey)
  69.     textPrint.reset()
  70.  
  71.     # Get count of joysticks
  72.     joystick_count = pygame.joystick.get_count()
  73.  
  74.     textPrint.print(screen, "Number of joysticks: {}".format(joystick_count) )
  75.     textPrint.indent()
  76.    
  77.     # For each joystick:
  78.     for i in range(joystick_count):
  79.         joystick = pygame.joystick.Joystick(i)
  80.         joystick.init()
  81.    
  82.         textPrint.print(screen, "Joystick {}".format(i) )
  83.         textPrint.indent()
  84.    
  85.         # Get the name from the OS for the controller/joystick
  86.         name = joystick.get_name()
  87.         textPrint.print(screen, "Joystick name: {}".format(name) )
  88.        
  89.         # Usually axis run in pairs, up/down for one, and left/right for
  90.         # the other.
  91.         axes = joystick.get_numaxes()
  92.         textPrint.print(screen, "Number of axes: {}".format(axes) )
  93.         textPrint.indent()
  94.        
  95.         for i in range( axes ):
  96.             axis = joystick.get_axis( i )
  97.             textPrint.print(screen, "Axis {} value: {:>6.0f}".format(i, axis) )
  98.         textPrint.unindent()
  99.            
  100.         buttons = joystick.get_numbuttons()
  101.         textPrint.print(screen, "Number of buttons: {}".format(buttons) )
  102.         textPrint.indent()
  103.  
  104.         for i in range( buttons ):
  105.             button = joystick.get_button( i )
  106.             textPrint.print(screen, "Button {:>2} value: {}".format(i,button) )
  107.         textPrint.unindent()
  108.            
  109.         # Hat switch. All or nothing for direction, not like joysticks.
  110.         # Value comes back in an array.
  111.         hats = joystick.get_numhats()
  112.         textPrint.print(screen, "Number of hats: {}".format(hats) )
  113.         textPrint.indent()
  114.  
  115.         for i in range( hats ):
  116.             hat = joystick.get_hat( i )
  117.             textPrint.print(screen, "Hat {} value: {}".format(i, str(hat)) )
  118.         textPrint.unindent()
  119.        
  120.         textPrint.unindent()
  121.  
  122.    
  123.     # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
  124.    
  125.     # Go ahead and update the screen with what we've drawn.
  126.     pygame.display.flip()
  127.  
  128.     # Limit to 20 frames per second
  129.     clock.tick(20)
  130.    
  131. # Close the window and quit.
  132. # If you forget this line, the program will 'hang'
  133. # on exit if running from IDLE.
  134. pygame.quit ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement