Guest User

Untitled

a guest
May 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import pygame
  2. from pymouse import PyMouse
  3. from time import sleep
  4.  
  5. # edit this to reflect your joystick axis and buttons
  6. action = {'x':0, 'y':1, 'multiplier':3, 'left':0, 'right':1}
  7.  
  8. pygame.init()
  9. j = pygame.joystick.Joystick(0) # first joystick
  10. j.init()
  11. m = PyMouse()
  12. print 'Initialized Joystick : %s' % j.get_name()
  13. state = [0, 0]
  14. try:
  15. while True:
  16. pygame.event.pump()
  17. sleep(0.1)
  18. # check if any button state has changed and change mouse state accordingly
  19. if j.get_button(action['left']) and not state[0]:
  20. state[0] = 1
  21. print "left press"
  22. m.press(*m.position())
  23. elif not j.get_button(action['left']) and state[0]:
  24. state[0] = 0
  25. print "left release"
  26. m.release(*m.position())
  27. elif j.get_button(action['right']) and not state[1]:
  28. state[1] = 1
  29. print "right press"
  30. m.press(*m.position(), button=2)
  31. elif not j.get_button(action['right']) and state[1]:
  32. state[1] = 0
  33. print "right release"
  34. m.release(*m.position(), button=2)
  35.  
  36. x, y = m.position()
  37. m.move(
  38. # get_axis returns a value between -1 and 1
  39. # fumble a bit here to reverse axis
  40. x + (j.get_axis(action['x']) * 50 * abs(j.get_axis(action['multiplier']) - 1)),
  41. y + (j.get_axis(action['y']) * 50 * abs(j.get_axis(action['multiplier']) - 1))
  42. )
  43. except KeyboardInterrupt:
  44. j.quit()
Add Comment
Please, Sign In to add comment