diff options
| -rw-r--r-- | sunrise.py | 64 |
1 files changed, 29 insertions, 35 deletions
@@ -9,38 +9,40 @@ from show import Show class Sun(object): """Object displayed on the bridge, probably a fish.""" - def __init__(self, x, y, width, height, c, step): + def __init__(self): """Create fish with default parameters.""" - self.x = x - self.y = y - self.x = 75 - self.y = -2 - self.width = width - self.width = 75 - self.height = height - self.height = 2 - self.color = c - self.color = color.RGB(1, 0.5, 0) - self.speed = step - self.step = step + self.center_x = 140 + self.center_y = -5 + self.width = 30 + self.height = 5 self.wait = 0 def update(self): """Swim gently to the other side of the bridge.""" - #self.x += self.step - #if self.x > 200: - # self.x = 0 - #self.y = 0.25 + 0.25 * math.sin(math.pi * self.x / 10) - #theta = random.random() * math.pi / 2 - math.pi / 4 - #self.y += 0.01 * math.sin(theta) - #print(self.y) - self.y += 0.005 - if self.y >= 1: - self.y = 1 + self.center_y += 0.005 + if self.center_y >= 1: + self.center_y = 1 self.wait += 1 if self.wait >= 200: self.wait = 0 - self.y = -2 + self.center_y = -5 + + def color_at(self, x, y): + ball_width = 15 + if abs(x - self.center_x) < ball_width: + return color.RGB(1, 1, 1) + distance = abs(self.center_x - x) + start_hue = 58 / 360 + end_hue = 41 / 360 + start_sat = 0 + end_sat = 0.92 + sat = scale_with_clamp(distance, ball_width, 60, start_sat, end_sat) + hue = scale_with_clamp(distance, ball_width, 100, start_hue, end_hue) + return color.HSV(hue, sat, 1).to_RGB() + + +def scale_with_clamp(inp, min_inp, max_inp, min_out, max_out): + return (clamp(min_inp, inp, max_inp) - min_inp) / (max_inp - min_inp) * (max_out - min_out) + min_out class Light(object): @@ -54,17 +56,9 @@ class Light(object): self.x = x self.y = y - def update(self, fish): + def update(self, sun): """Change color depending on where the fish is.""" - if self.y == 1: - new_color = color.RGB(0.5, 0.5, 0.5) - else: - new_color = color.RGB(0.5, 0.5, 0) - coverage = compute_coverage(fish, self.x, self.y) - ccolor = new_color * (1 - coverage) + fish.color * coverage - self.light.setRGBRaw( - *map(lambda x: clamp(0, x, 1), ccolor.components) - ) + self.light.setRGBRaw(*sun.color_at(self.x, self.y).components) class SunriseShow(Show): @@ -72,7 +66,7 @@ class SunriseShow(Show): self.lights = [Light(x, y, self.bridge.get_light(x, y)) for y in xrange(self.bridge.HEIGHT) for x in xrange(self.bridge.WIDTH)] - self.sun = Sun(0, 0, 23, 1.5, color.black, 1) + self.sun = Sun() def update(self): |
