|
| 1 | +""" |
| 2 | +Day 31 - Fireworks (version A) |
| 3 | +
|
| 4 | +My take on the fireworks theme. Colored blobs growing and sparkling. |
| 5 | +This version gives one explosion at a time at controlled intervalls. |
| 6 | +Suits best for smaller nonregular installations, eg a christmas tree. |
| 7 | +Also check the version in day31b. |
| 8 | +""" |
| 9 | + |
| 10 | +from xled_plus.samples.sample_setup import * |
| 11 | + |
| 12 | + |
| 13 | +class Fireball(Shape): |
| 14 | + def __init__(self, cent, minrad, maxrad, speed, huesat): |
| 15 | + super(Fireball, self).__init__(cent, 0.0) |
| 16 | + self.rad = minrad |
| 17 | + self.minrad = minrad |
| 18 | + self.maxrad = maxrad |
| 19 | + self.speed = speed |
| 20 | + self.huesat = huesat |
| 21 | + |
| 22 | + def is_inside(self, coord): |
| 23 | + return ( |
| 24 | + sum(map(lambda x1, x2: (x1 - x2) ** 2, self.cent, coord)) <= self.rad ** 2 |
| 25 | + ) |
| 26 | + |
| 27 | + def get_color(self, coord, ind): |
| 28 | + dist2 = ( |
| 29 | + sum(map(lambda x1, x2: (x1 - x2) ** 2, self.cent, coord)) / self.rad ** 2 |
| 30 | + ) |
| 31 | + if dist2 > 1.0: |
| 32 | + return False |
| 33 | + elif self.rad == self.minrad: |
| 34 | + return hsl_color(0.0, 0.0, 1.0) |
| 35 | + else: |
| 36 | + (hue, sat) = self.huesat |
| 37 | + p = (self.rad - self.minrad) / (self.maxrad - self.minrad) |
| 38 | + rnd = random() |
| 39 | + if rnd < 0.1 * (1.2 - p): |
| 40 | + light = 1.0 |
| 41 | + elif rnd < 0.2 * (1.2 - p): |
| 42 | + light = 0.0 |
| 43 | + else: |
| 44 | + light = 2.0 * (1.0 - p) ** 2 - 1.0 |
| 45 | + return hsl_color(hue, sat, light) |
| 46 | + |
| 47 | + def update(self, step): |
| 48 | + self.rad += self.speed * step |
| 49 | + |
| 50 | + |
| 51 | +class FireworksScene(MovingShapesScene): |
| 52 | + def __init__(self, ctr): |
| 53 | + super(FireworksScene, self).__init__(ctr) |
| 54 | + self.freq = 0.0 |
| 55 | + self.horizon = 100 |
| 56 | + self.preferred_frames = 700 |
| 57 | + self.preferred_fps = 20 |
| 58 | + self.proj2D3D = "halfsphere" |
| 59 | + |
| 60 | + def create(self): |
| 61 | + cent = (0.0, 0.0) |
| 62 | + shape = Fireball(cent, 0.10, 1.0, 0.01, (random(), 1.0 - random()**2)) |
| 63 | + shape.duetime = self.time + 0.9/0.01 |
| 64 | + return shape |
| 65 | + |
| 66 | + def update(self, step): |
| 67 | + for sh in reversed(self.shapes): |
| 68 | + if sh.duetime < self.time: |
| 69 | + self.shapes.remove(sh) |
| 70 | + if self.time >= self.crtime: |
| 71 | + if self.record: |
| 72 | + sh = self.create() |
| 73 | + self.add_shape(copy(sh)) |
| 74 | + sh.duetime += self.preferred_frames |
| 75 | + self.crtime = self.time + 100 |
| 76 | + self.pre_shapes.append((self.crtime + self.preferred_frames, sh)) |
| 77 | + elif self.replay: |
| 78 | + if self.pre_shapes: |
| 79 | + self.add_shape(self.pre_shapes[0][1]) |
| 80 | + self.crtime = self.pre_shapes[0][0] |
| 81 | + self.pre_shapes = self.pre_shapes[1:] |
| 82 | + else: |
| 83 | + self.add_shape(self.create()) |
| 84 | + self.crtime = self.time + 100 |
| 85 | + if self.pre_shapes and self.crtime >= self.pre_shapes[0][0]: |
| 86 | + self.replay = True |
| 87 | + for sh in self.shapes: |
| 88 | + sh.update(step) |
| 89 | + self.time += step |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + ctr = setup_control() |
| 93 | + ctr.adjust_layout_aspect(1.0) # How many times wider than high is the led installation? |
| 94 | + eff = FireworksScene(ctr) |
| 95 | + oldmode = ctr.get_mode()["mode"] |
| 96 | + eff.launch_rt() |
| 97 | + print("Started continuous effect - press Return to stop it") |
| 98 | + input() |
| 99 | + eff.stop_rt() |
| 100 | + ctr.set_mode(oldmode) |
0 commit comments