-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrollers.py
executable file
·139 lines (114 loc) · 4.69 KB
/
controllers.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
#-*- coding: utf8 -*-
from locals import *
from pygame.locals import *
class Gamepad (object):
def __init__(self, joystick):
self.joystick = joystick
self.moving_on_axis = None
def process_events(self, events):
event_queue = []
for event in events:
if event.type not in [JOYBUTTONDOWN, JOYHATMOTION, JOYAXISMOTION]:
continue
if event.joy != self.joystick.get_id():
continue
if event.type == JOYBUTTONDOWN and event.button in [0, 1, 2, 3]:
event_queue.append(EVENT_FIRE)
continue
# The dead zone for analog inputs. Everything below this value would
# be considered a random noise.
dead_zone = 0.5
if event.type == JOYHATMOTION:
if event.value == JOY_CENTERED:
event_queue.append(EVENT_STOP)
elif event.value == JOY_RIGHT:
event_queue.append(EVENT_MOVE_RIGHT)
elif event.value == JOY_LEFT:
event_queue.append(EVENT_MOVE_LEFT)
elif event.value == JOY_UP:
event_queue.append(EVENT_MOVE_UP)
elif event.value == JOY_DOWN:
event_queue.append(EVENT_MOVE_DOWN)
elif event.type == JOYAXISMOTION:
if event.value > -dead_zone and event.value < dead_zone and \
event.axis == self.moving_on_axis:
event_queue.append(EVENT_STOP)
self.moving_on_axis = None
elif event.axis == 1 and event.value < -dead_zone:
event_queue.append(EVENT_MOVE_UP)
self.moving_on_axis = event.axis
elif event.axis == 1 and event.value > dead_zone:
event_queue.append(EVENT_MOVE_DOWN)
self.moving_on_axis = event.axis
elif event.axis == 0 and event.value < -dead_zone:
event_queue.append(EVENT_MOVE_LEFT)
self.moving_on_axis = event.axis
elif event.axis == 0 and event.value > dead_zone:
event_queue.append(EVENT_MOVE_RIGHT)
self.moving_on_axis = event.axis
return event_queue
class KeyboardScheme (object):
def __init__(self, scheme):
self.left = scheme['left']
self.right = scheme['right']
self.up = scheme['up']
self.down = scheme['down']
self.fire = scheme['fire']
class Keyboard (object):
def __init__(self, scheme):
self.direction = None
self.scheme = scheme
def process_events(self, events):
event_queue = []
for event in events:
if not hasattr(event, 'key'):
continue
down = event.type == KEYDOWN
if down:
if event.key == self.scheme.right:
event_queue.append(EVENT_MOVE_RIGHT)
self.direction = EVENT_MOVE_RIGHT
elif event.key == self.scheme.left:
event_queue.append(EVENT_MOVE_LEFT)
self.direction = EVENT_MOVE_LEFT
elif event.key == self.scheme.up:
event_queue.append(EVENT_MOVE_UP)
self.direction = EVENT_MOVE_UP
elif event.key == self.scheme.down:
event_queue.append(EVENT_MOVE_DOWN)
self.direction = EVENT_MOVE_DOWN
elif event.key == self.scheme.fire:
event_queue.append(EVENT_FIRE)
else:
if event.key == self.scheme.right and self.direction == EVENT_MOVE_RIGHT:
event_queue.append(EVENT_STOP)
elif event.key == self.scheme.left and self.direction == EVENT_MOVE_LEFT:
event_queue.append(EVENT_STOP)
elif event.key == self.scheme.up and self.direction == EVENT_MOVE_UP:
event_queue.append(EVENT_STOP)
elif event.key == self.scheme.down and self.direction == EVENT_MOVE_DOWN:
event_queue.append(EVENT_STOP)
return event_queue
class Network (object):
def __init__(self):
pass
def keyboard_controls():
schemas = [
KeyboardScheme({
'left': K_LEFT,
'right': K_RIGHT,
'up': K_UP,
'down': K_DOWN,
'fire': K_SPACE,
}),
KeyboardScheme({
'left': K_a,
'right': K_d,
'up': K_w,
'down': K_s,
'fire': K_t,
}),
]
for scheme in schemas:
yield Keyboard(scheme)