-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteors.py
36 lines (32 loc) · 1.01 KB
/
meteors.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
import time
import random
from common import add_color,wheel,random_brightness
class Meteors:
def __init__(self,state):
self.meteors = []
self.cooldown = 0
self.state = state
def add_meteor(self,pos,color):
self.meteors.append((color,pos))
def step(self,wait_ms=5):
self.state.map_all(lambda _, c: tuple([max(x-15,0) for x in c]))
self.cooldown -= 1
if random.random() < 0.05 and self.cooldown <= 0:
self.cooldown = 20
direction = random.choice([-1,1])
shelves = [
[self.state.positions[0],self.state.positions[1]],
[self.state.positions[2],self.state.positions[3]],
]
shelf = random.choice(shelves)
self.add_meteor(shelf[0][::direction],wheel(random.randint(0,255)))
self.add_meteor(shelf[1][::direction],wheel(random.randint(0,255)))
for color,leds in self.meteors:
if not leds:
self.meteors.remove((color,leds))
continue
led = leds.pop()
x,y = led
# set color
self.state[x,y] = add_color(random_brightness(color),self.state[x,y])
time.sleep(wait_ms/1000.0)