Skip to content

Instantly share code, notes, and snippets.

@Gadgetoid
Last active January 4, 2018 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gadgetoid/5c1f52b8a1478f79af772b30a76590bc to your computer and use it in GitHub Desktop.
Save Gadgetoid/5c1f52b8a1478f79af772b30a76590bc to your computer and use it in GitHub Desktop.
Piano HAT Sprite Switcher
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment