forked from stlucasgarcia/connect-four
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
181 lines (143 loc) · 6.47 KB
/
controller.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import pygame
from pygame.locals import *
class Controller:
def __init__(self):
self.joysticks = []
self.type = []
self.x_hd = 596 # initial position of the chip
for i in range(pygame.joystick.get_count()):
self.joysticks.append(pygame.joystick.Joystick(i))
self.joysticks[-1].init()
self.type.append(self.joysticks[i].get_name())
axes = self.joysticks[i].get_numaxes()
def checkController(self):
if self.joysticks:
return True
else:
return False
def isControllerEvent(self, event):
return (
event.type == JOYBUTTONDOWN
or event.type == JOYAXISMOTION
or event.type == JOYHATMOTION
)
def isControllerDropEvent(self, event):
if len(self.type) > 0:
if self.type[0] == "PS4 Controller":
if event.type == JOYBUTTONDOWN:
return event.button == 12 or event.button == 0
if event.type == JOYAXISMOTION:
return abs(event.axis) == 5 and event.value == 1
else:
if event.type == JOYBUTTONDOWN:
return event.button == 0
if event.type == JOYHATMOTION:
return event.value[1] == -1
if event.type == JOYAXISMOTION:
return abs(event.axis) == 5 and event.value == 1
def isControllerEscEvent(self, event):
if len(self.type) > 0:
if self.type[0] == "PS4 Controller":
if event.type == JOYBUTTONDOWN:
return event.button == 1 or event.button == 6
if event.type == JOYAXISMOTION:
return abs(event.axis) == 4 and event.value == 1
else:
if event.type == JOYBUTTONDOWN:
return event.button == 1 or event.button == 7
if event.type == JOYAXISMOTION:
return abs(event.axis) == 2 and event.value == 1
def get_x_pos(self, event):
px_diff_hd = 109 # how much the chip will move each time
max_px_hd = 931 # max x position of the chip
min_px_hd = 269 # min x position of the chip
if self.type[0] == "PS4 Controller":
if event.type == JOYBUTTONDOWN:
if event.button == 13 or event.button == 9:
self.x_hd -= (
px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
)
if event.button == 14 or event.button == 10:
self.x_hd += px_diff_hd if self.x_hd + px_diff_hd < max_px_hd else 0
if event.type == JOYAXISMOTION:
if abs(event.axis) == 0:
if event.value > 0.7:
self.x_hd += 5 if self.x_hd + 5 < max_px_hd else 0
if event.value < -0.7:
self.x_hd -= 5 if self.x_hd - 5 >= min_px_hd else 0
else:
if event.type == JOYBUTTONDOWN:
if event.button == 4:
self.x_hd -= (
px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
)
if event.button == 5:
self.x_hd += px_diff_hd if self.x_hd + px_diff_hd < max_px_hd else 0
if event.type == JOYHATMOTION:
if event.value[0] == 1:
self.x_hd += px_diff_hd if self.x_hd + px_diff_hd < max_px_hd else 0
if event.value[0] == -1:
self.x_hd -= (
px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
)
if event.type == JOYAXISMOTION:
if abs(event.axis) == 0:
if event.value > 0.7:
self.x_hd += 5 if self.x_hd + 5 < max_px_hd else 0
if event.value < -0.7:
self.x_hd -= 5 if self.x_hd - 5 >= min_px_hd else 0
return self.x_hd
def check_event(self, event):
px_diff_hd = 109 # how much the chip will move each time
max_px_hd = 931 # max x position of the chip
min_px_hd = 269 # min x position of the chip
if self.type[0] == "PS4 Controller":
# Press Buttons
if event.type == JOYBUTTONDOWN:
if event.button == 13 or event.button == 9:
self.x_hd -= (
px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
)
return self.x_hd
if event.button == 14 or event.button == 10:
self.x_hd += px_diff_hd if self.x_hd + px_diff_hd < max_px_hd else 0
return self.x_hd
# Sticks PS4
if event.type == JOYAXISMOTION:
if abs(event.axis) == 0:
if event.value > 0.7:
self.x_hd += 1 if self.x_hd + 1 < max_px_hd else 0
return self.x_hd
if event.value < -0.7:
self.x_hd -= 1 if self.x_hd - 1 >= min_px_hd else 0
return self.x_hd
# Xbox and other
else:
# Press buttons
if event.type == JOYBUTTONDOWN:
if event.button == 4:
self.x_hd -= (
px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
)
return self.x_hd
if event.button == 5:
self.x_hd += px_diff_hd if self.x_hd + px_diff_hd < max_px_hd else 0
return self.x_hd
# D-pad Xbox
if event.type == JOYHATMOTION:
if event.value[0] == 1:
self.x_hd += px_diff_hd if self.x_hd + px_diff_hd < max_px_hd else 0
return self.x_hd
if event.value[0] == -1:
self.x_hd -= (
px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
)
return self.x_hd
if event.type == JOYAXISMOTION:
if abs(event.axis) == 0:
if event.value > 0.7:
self.x_hd += 1 if self.x_hd + 1 < max_px_hd else 0
return self.x_hd
if event.value < -0.7:
self.x_hd -= 1 if self.x_hd - 1 >= min_px_hd else 0
return self.x_hd