forked from keithtanner037/merlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree_shells.py
More file actions
440 lines (376 loc) · 13.9 KB
/
Copy paththree_shells.py
File metadata and controls
440 lines (376 loc) · 13.9 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# three_shells.py — Master Merlin "Three Shells" for Adafruit MacroPad
# Written by Iain Bennett — 2025
# Inspired by Keith Tanner's Merlin for the Macropad
#
# License:
# Released under the CC0 1.0 Universal (Public Domain Dedication).
# You can copy, modify, distribute, and perform the work, even for commercial purposes,
# all without asking permission. Attribution is appreciated but not required.
#
# "Three Shells" is a digital version of the classic shell game, adapted for the MacroPad.
# The ball is hidden under one of three shells (K3, K4, K5). After shuffling, the player
# must guess where the ball is. The shuffling speed and number of swaps are determined by
# the selected skill level.
#
# Gameplay:
# • At the start of a game, choose a skill level (1–9) using K0..K8. Keys display a
# rainbow gradient and pulse effect to indicate selection.
# • Once a skill is chosen, the ball briefly appears under one shell, then the shells
# are shuffled based on skill level.
# • When the shuffle ends, guess the ball’s location by pressing K3, K4, or K5.
# • Correct guesses score a point; incorrect guesses reveal the ball in blue while
# wrong shells fade out in red.
# • The next round starts automatically after a brief pause.
#
# Controls:
# • K0..K8 — Select skill level (1–9) during skill select mode
# • K3, K4, K5 — Guess ball location during guessing phase
# • K9 — New Game (return to skill select)
#
# Features:
# • Skill-based speed and shuffle complexity
# • Smooth LED animations for fades, pulses, and swaps
# • Auto-start next round after each guess
# • Persistent score display during gameplay
import time, math, random
import displayio, terminalio
from adafruit_display_text import label
class three_shells:
def __init__(self, macropad, tones):
self.mac = macropad
self.tones = tones
# Batch LED writes to prevent flicker
try:
self.mac.pixels.auto_write = False
except AttributeError:
pass
# Keys
self.K_NEW = 9
self.SHELL_KEYS = (3, 4, 5) # left, middle, right
self.SKILL_KEYS = tuple(range(0, 9)) # K0..K8 -> skill 1..9
# Colors
self.BRIGHT = 0.30
self.C_WHITE = 0xFFFFFF
self.C_RED = 0xFF0000
self.C_BLUE = 0x0000FF
self.C_DIM = 0x050505
self.fade_outs = []
# Timings
self.SHOW_BALL_TIME = 0.8
self.SWAP_FLASH_TIME = 0.13
self.PAUSE_AFTER_ROUND = 0.6
# Guess-phase pulse
self.GUESS_PULSE_HZ = 0.9
self.GUESS_MIN_SCALE = 0.18
self.GUESS_MAX_SCALE = 0.34
# State
self.mode = "skill"
self.skill = 3
self.ball_index = 1
self.swap_plan = []
self.swap_i = 0
self.phase_until = 0.0
self.score = 0
self.rounds = 0
# Display
self._build_display()
self._show()
self._update_score_text()
self._show_drawn = False
# ---------- Cleanup ----------
def cleanup(self):
# Make future ticks a no-op
self.mode = "idle" # not used by tick() -> nothing renders
self.fade_outs = []
self.swap_plan = []
self.phase_until = 0.0
# Best-effort: stop any sound
try:
if hasattr(self.mac, "stop_tone"):
self.mac.stop_tone()
except Exception:
pass
# LEDs: hard clear and hand pixel control back
try:
self.mac.pixels.fill(0x000000)
self.mac.pixels.show()
self.mac.pixels.auto_write = True
except Exception:
pass
# Display: clear text and detach our group so launcher can draw immediately
try:
self.title.text = ""
self.status.text = ""
except Exception:
pass
try:
blank = displayio.Group()
try:
self.mac.display.root_group = blank # CP 9.x
except AttributeError:
self.mac.display.show(blank) # CP 8.x
except Exception:
pass
# ---------- Public API ----------
def new_game(self):
self._enter_skill_select()
self._show()
def button(self, key):
now = time.monotonic()
if self.mode == "skill":
if key in self.SKILL_KEYS:
self.skill = key + 1 # K0=1 .. K8=9
self._beep(659, 0.04)
self._start_round()
elif key == self.K_NEW:
self._start_round()
return
if key == self.K_NEW:
self._enter_skill_select()
return
if self.mode != "guess":
return
if key in self.SHELL_KEYS:
guess_idx = self.SHELL_KEYS.index(key)
self._handle_guess(guess_idx, now)
def encoderChange(self, *_):
return
def tick(self):
if self.mode == "idle":
return
now = time.monotonic()
# Reveal-mode red fades (non-blocking)
if self.mode == "reveal" and self.fade_outs:
to_remove = []
for (idx, start, dur) in list(self.fade_outs):
t = (now - start) / dur
if t >= 1.0:
self.mac.pixels[idx] = 0x000000
to_remove.append((idx, start, dur))
else:
# cosine ease-out for smoothness
s = 0.25 * (0.5 + 0.5 * math.cos(t * math.pi))
self.mac.pixels[idx] = self._scale(self.C_RED, max(0.0, s))
for item in to_remove:
self.fade_outs.remove(item)
self._led_show()
if self.mode == "skill":
self._render_skill(now)
return
if self.mode == "show":
if not getattr(self, "_show_drawn", False): # <-- only once
self._render_board(high_ball=True)
self._show_drawn = True
if now >= self.phase_until:
self._enter_shuffle(now)
return
if self.mode == "shuffle":
if now >= self.phase_until:
self._next_swap(now)
return
if self.mode == "guess":
self._render_guess_pulse(now)
return
if self.mode == "reveal":
if now >= self.phase_until:
self._start_round()
return
# ---------- Skill select ----------
def _enter_skill_select(self):
self.mode = "skill"
self.score = 0
self.rounds = 0
self._show_drawn = False
self._all_off()
self.status.text = f"Skill: {self.skill}"
self._render_skill(time.monotonic())
self._show()
def _skill_color(self, idx):
# idx: 0..8 (K0..K8)
if idx <= 2:
t = idx / 2.0
r, g, b = 0, int(255*t), int(255*(1-t))
elif idx <= 5:
t = (idx - 3) / 2.0
r, g, b = int(255*t), 255, 0
else:
t = (idx - 6) / 2.0
r, g, b = 255, int(255*(1 - t)), 0
return (r << 16) | (g << 8) | b
def _render_skill(self, now):
self.mac.pixels.brightness = self.BRIGHT
# gradient across K0..K8
for i in range(9):
self.mac.pixels[i] = self._skill_color(i)
# blank K9..K11 during skill select
for k in (9, 10, 11):
self.mac.pixels[k] = 0x000000
# pulse speed increases with skill
if self.skill is not None:
sel = self.skill - 1
min_hz, max_hz = 0.6, 1.5
freq = min_hz + (self.skill - 1) * (max_hz - min_hz) / 8.0
pulse = 0.5 + 0.5 * math.cos(now * 2 * math.pi * freq)
base = self._skill_color(sel)
self.mac.pixels[sel] = self._scale(base, 0.5 + 0.5 * pulse)
self._led_show()
# ---------- Round flow ----------
def _start_round(self):
self.fade_outs = []
self.rounds += 1
self._all_off()
self.ball_index = random.randrange(3)
self.mode = "show"
self._show_drawn = False # <-- and reset here
self.phase_until = time.monotonic() + self.SHOW_BALL_TIME
self._update_score_text()
# (tick() will render the high_ball frame once)
def _enter_shuffle(self, now):
self.mode = "shuffle"
swap_count = 4 + self.skill * 2 # 6..22 swaps for skill 1..9
self.swap_plan = self._make_swaps(swap_count)
self.swap_i = 0
self._next_swap(now)
def _next_swap(self, now):
if self.swap_i >= len(self.swap_plan):
self.mode = "guess"
self._render_board()
return
a, b = self.swap_plan[self.swap_i]
self._swap_ball_if_needed(a, b)
self._flash_swap(a, b, now)
self.swap_i += 1
def _handle_guess(self, guess_idx, now):
self._all_off()
correct = (guess_idx == self.ball_index)
# Prepare fades for the two wrong shells
self.fade_outs = []
for i, shell_key in enumerate(self.SHELL_KEYS):
if i != self.ball_index:
self.mac.pixels[shell_key] = self._scale(self.C_RED, 0.25)
self.fade_outs.append((shell_key, now, 0.4))
if correct:
self.score += 1
self.mac.pixels[self.SHELL_KEYS[guess_idx]] = 0x00FF00
self._beep(784, 0.06)
else:
self.mac.pixels[self.SHELL_KEYS[self.ball_index]] = self.C_BLUE
self._beep(220, 0.07)
self._update_score_text()
self._led_show()
self.mode = "reveal"
self.phase_until = now + self.PAUSE_AFTER_ROUND
# ---------- Visuals ----------
def _render_board(self, high_ball=False):
self.mac.pixels.brightness = self.BRIGHT
# Everything off first
for k in range(12):
self.mac.pixels[k] = 0x000000
# Reset key dim hint
self.mac.pixels[self.K_NEW] = self._scale(self.C_WHITE, 0.10)
# Shell keys dim gray
for k in self.SHELL_KEYS:
self.mac.pixels[k] = self.C_DIM
# Highlight the ball if requested (batched with the above)
if high_ball:
self.mac.pixels[self.SHELL_KEYS[self.ball_index]] = self.C_WHITE
self._led_show()
def _render_guess_pulse(self, now):
self.mac.pixels.brightness = self.BRIGHT
# non-shell keys off, except K9 dim hint
for k in range(12):
if k == self.K_NEW:
self.mac.pixels[k] = self._scale(self.C_WHITE, 0.10)
elif k not in self.SHELL_KEYS:
self.mac.pixels[k] = 0x000000
# cosine pulse between MIN and MAX on shells
u = 0.5 + 0.5 * math.cos(now * 2 * math.pi * self.GUESS_PULSE_HZ)
s = self.GUESS_MIN_SCALE + (self.GUESS_MAX_SCALE - self.GUESS_MIN_SCALE) * u
for k in self.SHELL_KEYS:
self.mac.pixels[k] = self._scale(self.C_WHITE, s)
self._led_show()
def _flash_swap(self, a, b, now):
keys = (self.SHELL_KEYS[a], self.SHELL_KEYS[b])
self._render_board(high_ball=False)
self.mac.pixels[keys[0]] = self.C_WHITE
self.mac.pixels[keys[1]] = self.C_WHITE
self._led_show()
self.phase_until = now + self.SWAP_FLASH_TIME
# ---------- Swap plan ----------
def _make_swaps(self, n):
pairs = [(0,1), (1,2), (0,2)]
last = None
plan = []
for _ in range(n):
a, b = random.choice(pairs)
if last and (a,b) in (last, (last[1], last[0])):
choices = [p for p in pairs if p not in (last, (last[1], last[0]))]
a, b = random.choice(choices)
plan.append((a, b))
last = (a, b)
return plan
def _swap_ball_if_needed(self, a, b):
if self.ball_index == a:
self.ball_index = b
elif self.ball_index == b:
self.ball_index = a
# ---------- Display ----------
def _build_display(self):
W, H = self.mac.display.width, self.mac.display.height
self.group = displayio.Group()
y = 0
self.logo_tile = None
try:
logo_bmp = displayio.OnDiskBitmap("MerlinChrome.bmp")
self.logo_tile = displayio.TileGrid(
logo_bmp,
pixel_shader=logo_bmp.pixel_shader,
x=(W - logo_bmp.width)//2,
y=y
)
self.group.append(self.logo_tile)
y += logo_bmp.height + 2
except Exception as e:
print("Logo load failed:", e)
self.title = label.Label(
terminalio.FONT, text="Three Shells",
color=0xFFFFFF, anchor_point=(0.5, 0.0),
anchored_position=(W//2, y)
)
self.group.append(self.title)
y += 12 + 2
self.status = label.Label(
terminalio.FONT, text="",
color=0xAAAAAA, anchor_point=(0.5, 0.0),
anchored_position=(W//2, y)
)
self.group.append(self.status)
def _show(self):
try:
self.mac.display.root_group = self.group
except AttributeError:
self.mac.display.show(self.group)
def _update_score_text(self):
self.status.text = f"Score: {self.score}"
# ---------- LED helpers ----------
def _all_off(self):
self.mac.pixels.brightness = self.BRIGHT
for i in range(12):
self.mac.pixels[i] = 0x000000
self._led_show()
def _scale(self, color, s):
r = (color >> 16) & 0xFF
g = (color >> 8) & 0xFF
b = color & 0xFF
r = int(r * s); g = int(g * s); b = int(b * s)
return (r << 16) | (g << 8) | b
def _led_show(self):
try:
self.mac.pixels.show()
except AttributeError:
pass
def _beep(self, f, d):
try:
self.mac.play_tone(f, d)
except Exception:
pass