-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpiano-sprite.py
48 lines (36 loc) · 1.1 KB
/
piano-sprite.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
import time
from PIL import Image
import pianohat
import unicornhathd
SPRITE_W = 16
SPRITE_H = 16
# Map the white piano keys to the 8 sprites
map = [0,2,4,5,7,9,11,12]
img = Image.open('unicorn-sprite-switch.png')
def display_sprite(source, sprite, frame):
image_width, image_height = source.size
num_sprites = image_width / SPRITE_W
num_frames = image_height / SPRITE_H
offset_x = (sprite % num_sprites) * SPRITE_W
offset_y = (frame % num_frames) * SPRITE_H
width, height = unicornhathd.get_shape()
for x in range(width):
for y in range(height):
pixel = source.getpixel((offset_x + x, offset_y + y))
r, g, b, alpha = [int(p) for p in pixel]
if alpha > 0:
unicornhathd.set_pixel(x, y, r, g, b)
else:
unicornhathd.set_pixel(x, y, 0, 0, 0)
unicornhathd.show()
def handle_touch(ch, evt):
global sprite
if ch in map:
sprite = map.index(ch)
pianohat.on_note(handle_touch)
frame = 0
sprite = 0
while True:
frame += 1
display_sprite(img, sprite, frame)
time.sleep(1.0/5)