Skip to content

Commit aa69401

Browse files
authored
Merge pull request adafruit#964 from caternuson/dreidel
Add dreidel code
2 parents 0aae1a4 + 594392e commit aa69401

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

TFT_Gizmo_Dreidel/dreidel.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import time
2+
import random
3+
import displayio
4+
import adafruit_imageload
5+
from adafruit_circuitplayground.bluefruit import cpb
6+
from adafruit_gizmo import tft_gizmo
7+
8+
#pylint: disable=bad-continuation
9+
# define melody to play while spinning (freq, duration)
10+
melody = (
11+
# oh drei del drei del
12+
(330, 8), (392, 8), (330, 8), (392, 8), (330, 8),
13+
# drei del I made it
14+
(392, 8), (330, 16), (330, 8), (392, 8), (392, 8),
15+
# out of clay
16+
(349, 8), (330, 8), (294, 16), (0, 8),
17+
# oh drei del drei del
18+
(294, 8), (349, 8), (294, 8), (349, 8), (294, 8),
19+
# drei del then drei del
20+
(349, 8), (294, 16), (294, 8), (392, 8), (349, 8),
21+
# I shall play
22+
(330, 8), (294, 8), (262, 16),
23+
)
24+
melody_tempo = 0.02
25+
#pylint: enable=bad-continuation
26+
27+
# setup TFT Gizmo and main display group (splash)
28+
display = tft_gizmo.TFT_Gizmo()
29+
splash = displayio.Group()
30+
display.show(splash)
31+
32+
# load dreidel background image
33+
dreidel_bmp, dreidel_pal = adafruit_imageload.load("/dreidel_background.bmp",
34+
bitmap=displayio.Bitmap,
35+
palette=displayio.Palette)
36+
dreidel_tg = displayio.TileGrid(dreidel_bmp, pixel_shader=dreidel_pal)
37+
splash.append(dreidel_tg)
38+
39+
# load dreidel symbols (sprite sheet)
40+
symbols_bmp, symbols_pal = adafruit_imageload.load("/dreidel_sheet.bmp",
41+
bitmap=displayio.Bitmap,
42+
palette=displayio.Palette)
43+
for index, color in enumerate(symbols_pal):
44+
if color == 0xFFFF01:
45+
symbols_pal.make_transparent(index)
46+
symbols_tg = displayio.TileGrid(symbols_bmp, pixel_shader=symbols_pal,
47+
width = 1,
48+
height = 1,
49+
tile_width = 60,
50+
tile_height = 60)
51+
symbols_group = displayio.Group(scale=2, x=60, y=70)
52+
symbols_group.append(symbols_tg)
53+
splash.append(symbols_group)
54+
55+
# dreidel time!
56+
tile = 0
57+
while True:
58+
# wait for shake
59+
while not cpb.shake():
60+
pass
61+
# play melody while "spinning" the symbols
62+
for note, duration in melody:
63+
symbols_tg[0] = tile % 4
64+
tile += 1
65+
cpb.play_tone(note, duration * melody_tempo)
66+
# land on a random symbol
67+
symbols_tg[0] = random.randint(0, 3)
68+
# prevent immediate re-shake
69+
time.sleep(2)
57.4 KB
Binary file not shown.

TFT_Gizmo_Dreidel/dreidel_sheet.bmp

7.17 KB
Binary file not shown.

0 commit comments

Comments
 (0)