cookertron

Pygame Joystick XBOX 360

Sep 25th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. import sys
  2. import pygame
  3.  
  4.  
  5. W, H = 1366, 768
  6. HW, HH = int(W / 2), int(H / 2)
  7.  
  8. BRICK_SIZE = 80
  9. BRICK_SIZE_HALF = int(BRICK_SIZE / 2)
  10. BRICK_COLOR = (255, 0, 0)
  11.  
  12. BLACK = (0, 0, 0)
  13.  
  14. pygame.init()
  15. DS = pygame.display.set_mode((W, H), pygame.FULLSCREEN)
  16. CLOCK = pygame.time.Clock()
  17. FPS = 60
  18.  
  19. pygame.joystick.init()
  20. joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
  21. if not joysticks:
  22.     print "NO JOYSTICKS DETECTED"
  23.     sys.exit()
  24. joysticks[0].init()
  25.  
  26. bx = HW
  27. by = HH
  28.  
  29. while True:
  30.     pygame.event.get()
  31.  
  32.     k = pygame.key.get_pressed()
  33.     if k[pygame.K_ESCAPE]: break
  34.  
  35.     DS.fill(BLACK)
  36.  
  37.     pygame.draw.rect(DS, BRICK_COLOR, (bx - BRICK_SIZE_HALF, by - BRICK_SIZE_HALF, BRICK_SIZE, BRICK_SIZE), 0)
  38.     # code here
  39.  
  40.     bx += round(joysticks[0].get_axis(0), 1) * 3
  41.     by += round(joysticks[0].get_axis(1), 1) * 3
  42.  
  43.     if bx < 0: bx = W
  44.     if bx > W: bx = 0
  45.     if by < 0: by = H
  46.     if by > H: by = 0
  47.  
  48.     pygame.display.update()
  49.     CLOCK.tick(FPS)
  50.  
  51. pygame.joystick.quit()
  52. pygame.quit()
  53. sys.exit()
Add Comment
Please, Sign In to add comment