-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
284 lines (248 loc) · 8.52 KB
/
__init__.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from color import Color
import display
import utime
import buttons
import bhi160
import leds
import utime
import interrupt
import vibra
# HCOLORS = {
# 1: Color.from_hex(0x010101),
# 2: Color.from_hex(0xa9a9a9),
# 3: Color.from_hex(0x575757),
# 4: Color.from_hex(0xffbbd3),
# }
# HPIXELS = [
# [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 1, 2, 1, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 1, 2, 3, 1, 1, 1, 1, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
# [0, 1, 2, 2, 2, 3, 3, 2, 3, 2, 2, 3,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 0],
# ]
HCOLORS = {
1: Color.from_hex(0x010101),
2: Color.from_hex(0xa9a9a9),
3: Color.from_hex(0xffffff),
4: Color.from_hex(0xde4c8a),
}
HPIXELS = [
[0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,],
[0,1,2,1,0,0,0,0,0,0,0,1,2,2,1,0,],
[0,1,2,2,1,0,0,0,0,0,1,2,2,2,1,0,],
[0,1,2,2,2,1,1,1,1,1,2,2,2,2,1,0,],
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0,],
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0,],
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,],
[1,2,2,2,3,1,2,2,2,2,2,3,1,2,2,1,],
[1,2,2,2,1,1,2,2,2,1,2,1,1,2,2,1,],
[1,2,4,4,2,2,2,2,2,2,2,2,2,4,4,1,],
[1,2,4,4,2,1,2,2,1,2,2,1,2,4,4,1,],
[0,1,2,2,2,1,1,1,1,1,1,1,2,2,1,0,],
[0,0,1,2,2,2,2,2,2,2,2,2,2,1,0,0,],
[0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,],
]
displayWidth = 160
displayHeight = 80
size = 1
catWidth = len(HPIXELS[0]) * size
catHeight = len(HPIXELS) * size
midpointX = int(displayWidth/2) - int(catWidth/2)
midpointY = int(displayHeight/2) - int(catHeight/2)
right = True
samples = []
def bhiData(_samples):
global samples
samples = _samples
bhi = bhi160.BHI160Orientation(sample_rate=10, callback=bhiData)
DispPosition = [midpointX,midpointY] # x,y
Orientation = [0,0,0] #x,y,z?
counter = 0
game = True
def scale(size):
result = []
for row in HPIXELS:
cl = []
for p in row:
for i in range(size):
cl.append(p)
for i in range(size):
result.append(cl)
return result
SIZER = scale(size)
def draw_cat(X, Y):
with display.open() as disp:
disp.clear()
for y, row in enumerate(SIZER):
if (right == True):
for x, p in enumerate(row):
if p:
disp.pixel(X+x, Y+y, col=HCOLORS[p])
else:
for x, p in enumerate(row[::-1]):
if p:
disp.pixel(X+x, Y+y, col=HCOLORS[p])
disp.update()
def run():
global right
i = 0
while True:
draw_cat(i, 10)
if (i == (displayWidth - catWidth)):
while (i != 0):
right = False
draw_cat(i, 0)
i-= 1
right = True
i += 1
def runbutton():
i = 10
while True:
while True:
pressed = buttons.read(
buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT
)
if pressed != 0:
break
while True:
if pressed & buttons.BOTTOM_LEFT != 0:
i-=1
draw_cat(i, 10)
break
if pressed & buttons.BOTTOM_RIGHT != 0:
i+=1
draw_cat(i, 10)
break
def set_X(shift):
global DispPosition
realshift = Orientation[0] + shift
new_position = DispPosition[0] + realshift
if (new_position < 0):
DispPosition[0] = 0
elif (new_position + catWidth > 159):
DispPosition[0] = 159 - catWidth
else:
DispPosition[0] = new_position
def set_Y(shift):
global DispPosition
new_position = DispPosition[1] + shift
if (new_position < 0):
DispPosition[1] = 0
elif (new_position + catHeight > 79):
DispPosition[1] = 79 - catHeight
else:
DispPosition[1] = new_position
def check_Border():
# Q = [0,0]
# Q[0] = DispPosition[0]/6 # Berrechnung des X Quadranten
# Q[1] = DispPosition[1]/6 # Berrechnung des Y Quadranten
# ColorValues = [[255,186,255], [222,135,255], [164,85,255], [104,29,255], [16,0,202], [0,0,152]]
# if (Q[0] >= 0 and Q[0] <= 3.34):
# leds.prep(14, [])
if DispPosition[0] > 120:
leds.prep(12,[255,0,0])
leds.prep(13,[255,0,0])
elif DispPosition[0] < 10:
leds.prep(11,[255,0,0])
leds.prep(14,[255,0,0])
else:
leds.prep(12,[0,0,0])
leds.prep(13,[0,0,0])
if DispPosition[1] < 10:
leds.prep(13,[255,0,0])
leds.prep(14,[255,0,0])
elif DispPosition[1] > 60:
leds.prep(11,[255,0,0])
leds.prep(12,[255,0,0])
else:
leds.prep(12,[0,0,0])
leds.prep(14,[0,0,0])
leds.update()
def calibrate():
global Orientation
# samples = sensors[sensor]["sensor"].read()
if len(samples) > 0:
vibra.vibrate(40)
sample = samples[0]
Orientation[2] = sample.z
Orientation[1] = sample.y
with display.open() as disp:
disp.clear()
disp.print("Calibration successful", posy=20)
disp.update()
utime.sleep(1)
def runorientation():
global right
global DispPosition
global Orientation
global samples
while True:
pressed = buttons.read(
buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT
)
if pressed & buttons.BOTTOM_LEFT != 0:
DispPosition[0] = midpointX
DispPosition[1] = midpointY
if pressed & buttons.BOTTOM_RIGHT != 0:
calibrate()
vibra.vibrate(10)
if len(samples) > 0:
Value = [0,0,0]
sample = samples[0] # take the first sample
Value[1] = -(Orientation[1]) + sample.y # + (int(counter/10))# y
Value[2] = -(Orientation[2]) + sample.z # + (int(counter/10))# x
if Value[2] < -2: # Check if orientation is right and normalize x data
set_X(int(-Value[2]))
right = True
if Value[2] > 2: # Check if orientation is left and normalize x data
set_X(int(-Value[2]))
right = False
if (sample.y < -2) or (sample.y > 2): # normalize y data
set_Y(int(-Value[1]))
draw_cat(DispPosition[0], DispPosition[1])
# check_Border()
play()
def play():
global counter
global game
global DispPosition
if ((DispPosition[0] + catWidth) == 159 or (DispPosition[0] == 0) or
(DispPosition[1] == 0) or (DispPosition[1] + catHeight == 79)):
print(counter)
game = False
while not game:
with display.open() as disp:
disp.clear()
disp.print("Game Over", fg=[255,0,0], posy=0, posx=20)
disp.print("Your Score:%s" % str(counter), posy=20, font=2)
disp.print("<-New Game", posy = 60, font=2)
disp.update()
utime.sleep(1)
if buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) & buttons.BOTTOM_LEFT != 0:
counter = 0
game = True
DispPosition[0] = midpointX
DispPosition[1] = midpointY
else:
if game:
counter = counter + 1
# def orientation_interrupt():
# interrupt.set_callback(interrupt.RTC_ALARM, Testing)
# interrupt.enable_callback(interrupt.RTC_ALARM)
# sensors = [{"sensor": bhi160.BHI160Orientation(sample_rate=10), "name": "Orientation"}]
# orientation_interrupt()
timer = utime.monotonic_ms()
runorientation()