Advertisement
Guest User

Untitled

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