mirror of
https://github.com/polyfloyd/ledcat-nyancat.git
synced 2025-07-04 16:20:09 +02:00
s/tabs/spaces/
This commit is contained in:
parent
23178cb121
commit
21f581a057
1 changed files with 59 additions and 59 deletions
118
nyan/__main__.py
118
nyan/__main__.py
|
@ -15,80 +15,80 @@ DISP_HEIGHT = int(_geometry[1])
|
||||||
|
|
||||||
|
|
||||||
tail_colors = [
|
tail_colors = [
|
||||||
(0xff, 0x00, 0x00),
|
(0xff, 0x00, 0x00),
|
||||||
(0xff, 0x99, 0x00),
|
(0xff, 0x99, 0x00),
|
||||||
(0xff, 0xff, 0x00),
|
(0xff, 0xff, 0x00),
|
||||||
(0x00, 0xff, 0x00),
|
(0x00, 0xff, 0x00),
|
||||||
(0x00, 0x99, 0xff),
|
(0x00, 0x99, 0xff),
|
||||||
(0x66, 0x33, 0xff),
|
(0x66, 0x33, 0xff),
|
||||||
]
|
]
|
||||||
|
|
||||||
def read_image(filename):
|
def read_image(filename):
|
||||||
img = Image.open(filename)
|
img = Image.open(filename)
|
||||||
return img
|
return img
|
||||||
|
|
||||||
anim_cat = []
|
anim_cat = []
|
||||||
for i in range(0, 6):
|
for i in range(0, 6):
|
||||||
frame = read_image('%s/cat/%d.png' % (path.dirname(__file__), i))
|
frame = read_image('%s/cat/%d.png' % (path.dirname(__file__), i))
|
||||||
assert (32, 16) == frame.size
|
assert (32, 16) == frame.size
|
||||||
anim_cat.append(frame)
|
anim_cat.append(frame)
|
||||||
anim_sparkle = []
|
anim_sparkle = []
|
||||||
for i in range(0, 5):
|
for i in range(0, 5):
|
||||||
frame = read_image('%s/sparkle/%d.png' % (path.dirname(__file__), i))
|
frame = read_image('%s/sparkle/%d.png' % (path.dirname(__file__), i))
|
||||||
anim_sparkle.append(frame)
|
anim_sparkle.append(frame)
|
||||||
|
|
||||||
class Sparkle(object):
|
class Sparkle(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
half_h = anim_sparkle[0].size[1] // 2
|
half_h = anim_sparkle[0].size[1] // 2
|
||||||
self.x = random.randint(0, DISP_WIDTH)
|
self.x = random.randint(0, DISP_WIDTH)
|
||||||
self.y = random.randint(half_h, DISP_HEIGHT - half_h)
|
self.y = random.randint(half_h, DISP_HEIGHT - half_h)
|
||||||
self.frame_index = 0
|
self.frame_index = 0
|
||||||
|
|
||||||
sparkles = []
|
sparkles = []
|
||||||
while True:
|
while True:
|
||||||
for anim_frame in anim_cat:
|
for anim_frame in anim_cat:
|
||||||
if len(sparkles) < 32:
|
if len(sparkles) < 32:
|
||||||
sparkles.append(Sparkle())
|
sparkles.append(Sparkle())
|
||||||
|
|
||||||
frame = bytearray(DISP_WIDTH * DISP_HEIGHT * 3)
|
frame = bytearray(DISP_WIDTH * DISP_HEIGHT * 3)
|
||||||
|
|
||||||
# Render the tail
|
# Render the tail
|
||||||
for x in range(DISP_WIDTH):
|
for x in range(DISP_WIDTH):
|
||||||
for y in range(DISP_HEIGHT):
|
for y in range(DISP_HEIGHT):
|
||||||
i = y * DISP_WIDTH + x
|
i = y * DISP_WIDTH + x
|
||||||
t = time.time()
|
t = time.time()
|
||||||
col_y = y - (DISP_HEIGHT // 2 - len(tail_colors) // 2) + int(math.sin(x / 6 + t * math.pi) * 4 * math.sin(t * 8))
|
col_y = y - (DISP_HEIGHT // 2 - len(tail_colors) // 2) + int(math.sin(x / 6 + t * math.pi) * 4 * math.sin(t * 8))
|
||||||
if x < (DISP_WIDTH - 10) and 0 <= col_y < len(tail_colors):
|
if x < (DISP_WIDTH - 10) and 0 <= col_y < len(tail_colors):
|
||||||
color = tail_colors[col_y]
|
color = tail_colors[col_y]
|
||||||
else:
|
else:
|
||||||
color = (0x0f, 0x4d, 0x8f)
|
color = (0x0f, 0x4d, 0x8f)
|
||||||
frame[i*3:i*3+3] = color
|
frame[i*3:i*3+3] = color
|
||||||
|
|
||||||
# Copy animated frame
|
# Copy animated frame
|
||||||
for anim_x in range(anim_frame.size[0]):
|
for anim_x in range(anim_frame.size[0]):
|
||||||
for anim_y in range(anim_frame.size[1]):
|
for anim_y in range(anim_frame.size[1]):
|
||||||
pix = anim_frame.getpixel((anim_x, anim_y))
|
pix = anim_frame.getpixel((anim_x, anim_y))
|
||||||
if pix[3] != 0: # Test for alpha
|
if pix[3] != 0: # Test for alpha
|
||||||
x = (DISP_WIDTH - anim_frame.size[0]) + anim_x
|
x = (DISP_WIDTH - anim_frame.size[0]) + anim_x
|
||||||
y = (DISP_HEIGHT // 2 - anim_frame.size[1] // 2) + anim_y
|
y = (DISP_HEIGHT // 2 - anim_frame.size[1] // 2) + anim_y
|
||||||
i = ((y * DISP_WIDTH) + x) * 3
|
i = ((y * DISP_WIDTH) + x) * 3
|
||||||
frame[i:i+3] = pix[:3]
|
frame[i:i+3] = pix[:3]
|
||||||
|
|
||||||
# Render and update the sparkles
|
# Render and update the sparkles
|
||||||
for sp in sparkles:
|
for sp in sparkles:
|
||||||
for x in range(anim_sparkle[0].size[0]):
|
for x in range(anim_sparkle[0].size[0]):
|
||||||
for y in range(anim_sparkle[0].size[1]):
|
for y in range(anim_sparkle[0].size[1]):
|
||||||
pix_x = sp.x + x - anim_sparkle[0].size[0] // 2
|
pix_x = sp.x + x - anim_sparkle[0].size[0] // 2
|
||||||
pix_y = sp.y + y - anim_sparkle[0].size[1] // 2
|
pix_y = sp.y + y - anim_sparkle[0].size[1] // 2
|
||||||
if 0 <= pix_x < DISP_WIDTH and 0 <= pix_y < DISP_HEIGHT:
|
if 0 <= pix_x < DISP_WIDTH and 0 <= pix_y < DISP_HEIGHT:
|
||||||
i = pix_y * DISP_WIDTH + pix_x
|
i = pix_y * DISP_WIDTH + pix_x
|
||||||
pix = anim_sparkle[sp.frame_index].getpixel((x, y))
|
pix = anim_sparkle[sp.frame_index].getpixel((x, y))
|
||||||
if pix[3] != 0:
|
if pix[3] != 0:
|
||||||
frame[i*3:i*3+3] = pix[:3]
|
frame[i*3:i*3+3] = pix[:3]
|
||||||
sp.x -= 3
|
sp.x -= 3
|
||||||
sp.frame_index += 1
|
sp.frame_index += 1
|
||||||
# Remove expired sparkles
|
# Remove expired sparkles
|
||||||
sparkles = list(filter(lambda sp: sp.frame_index < len(anim_sparkle), sparkles))
|
sparkles = list(filter(lambda sp: sp.frame_index < len(anim_sparkle), sparkles))
|
||||||
|
|
||||||
sys.stdout.buffer.write(frame)
|
sys.stdout.buffer.write(frame)
|
||||||
time.sleep(1 / 30)
|
time.sleep(1 / 30)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue