Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoystick.py
More file actions
Latest commit
97 lines (78 loc) · 2.41 KB
/
Copy pathJoystick.py
File metadata and controls
97 lines (78 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
importpygame
classJoystick(object):
def__init__(self):
self.joystick_names= []
self.doMove=-1
self.joyButtonDown=False
self.myJoystick=None
# Enumerate joysticks
foriinrange(0, pygame.joystick.get_count()):
self.joystick_names.append(pygame.joystick.Joystick(i).get_name())
# By default, load the first available joystick.
iflen(self.joystick_names) >0:
self.myJoystick=pygame.joystick.Joystick(0)
self.myJoystick.init()
defjoystickAvailable(self):
ifself.myJoystickisnotNone:
returnTrue
else:
returnFalse
defgetAction(self):
action=""
ifself.myJoystickisNone:
returnaction
try:
buttonCount=self.myJoystick.get_numbuttons()
ifself.myJoystick.get_button(0) andself.joyButtonDownisFalse: # speed up
action="speedUp"
elifself.myJoystick.get_button(1) andself.joyButtonDownisFalse: # speed down
action="speedDown"
elif (buttonCount>9andself.myJoystick.get_button(9) andself.joyButtonDownisFalse)\
or (buttonCount>3andself.myJoystick.get_button(3) andself.joyButtonDownisFalse): # (re)start
action="restart"
elifbuttonCount>8andself.myJoystick.get_button(8) andself.joyButtonDownisFalse:
action='quit'
# make sure NO button is down for reset
someJoyButtonDown=False
foriinrange(0, self.myJoystick.get_numbuttons()):
ifself.myJoystick.get_button(i):
someJoyButtonDown=True
self.joyButtonDown=someJoyButtonDown
ifself.doMove!=-1andnotaction:
action="move"
exceptExceptionasex:
print('JOYSTICK ERROR, DEACTIVATING:'+str(ex))
self.myJoystick=None
returnaction
defprocessAxis(self):
ifself.myJoystickisNone:
return
try:
xAx=0
yAx=0
# sometimes 2 axis, sometimes 6, wtf?! (X-Box controller)
ifself.myJoystick.get_numaxes() ==2:
xAx=0
yAx=1
elifself.myJoystick.get_numaxes() ==3:
xAx=0
yAx=1
else:
xAx=3
yAx=4
ifself.myJoystick.get_axis(xAx) >0:
self.doMove=2
elifself.myJoystick.get_axis(xAx) <0:
self.doMove=4
elifself.myJoystick.get_axis(yAx) >0:
self.doMove=3
elifself.myJoystick.get_axis(yAx) <0:
self.doMove=1
exceptExceptionasex:
print('JOYSTICK ERROR, DEACTIVATING:'+str(ex))
self.myJoystick=None
defgetMoveAction(self):
tmp=self.doMove
ifself.doMove!=-1:
self.doMove=-1
returntmp