sk6812-client/fireworks.py

57 lines
1.3 KiB
Python
Raw Normal View History

2019-12-01 14:21:01 +01:00
#!/usr/bin/env python3
import sk6812
import time
from random import randint
class Rocket:
def __init__(self, sk, color = [0,0,0,255], alt = 100, size = 10):
self.sk = sk
self.color = color
self.alt = alt
self.size = size
self.pos = 0
self.dist = 0
def draw(self):
if self.pos < self.alt:
# launching
self.sk.add_color(self.pos, 64, 32, 0, 0)
self.sk.add_color(self.pos+1, 64, 32, 0, 0)
self.pos += 2
elif self.dist < self.size:
# exploded
self.sk.set_color(self.alt, 0, 0, 0, 255)
self.sk.add_color(self.alt + self.dist, self.color[0], self.color[1], self.color[2], self.color[3])
self.sk.add_color(self.alt - self.dist, self.color[0], self.color[1], self.color[2], self.color[3])
self.dist += 1
num_modules = 300
interval = 0.05
w = sk6812.SK6812("zybot", 2703)
w.set_fadestep(1.00/interval)
w.set_num_modules(num_modules)
rockets = []
frame = 0
while True:
if frame % 30 == 0:
rockets.append(Rocket(w, [randint(0, 255), randint(0, 255), randint(0, 255), randint(0, 255)], randint(50, 249), randint(10, 50)))
for r in rockets:
r.draw()
for i in range(num_modules):
w.fade_color(i, 0, 0, 0, 0)
w.commit()
frame += 1
time.sleep(interval)