"""Rendering classes and helper functions.""" from __future__ import division import color import random import constants from constants import * import math from show import Show class ShootingStar(object): """Object displayed on the bridge, probably a fish.""" def __init__(self): """Create fish with default parameters.""" self.x = 0 self.y = random.random() * 1.5 self.width = 5 self.height = 0.5 self.color = color.RGB(255/255, 210/255, 201/255) def update(self): self.x += 0.1 class Light(object): """Represent a bridge light and its state.""" def __init__(self, x, y, light): """Store all facts about a light.""" self.light = light self.color = color.black self.transitioning_to = self.color self.step = 0 self.on = False self.increasing = False self.x = x self.y = y self.fade_step = 0 def update(self): """Change color depending on where the fish is.""" if not self.on and random.random() < 0.0005: self.transitioning_to = color.RGB(1, 1, 1) self.step = abs(self.transitioning_to - self.color) / (FRAME_RATE / 2) self.increasing = True self.on = True self.color = transition(self.color, self.transitioning_to, self.step) elif self.on: if abs(self.transitioning_to - self.color) <= self.step: if self.increasing == True: self.transitioning_to = color.RGB(0, 0, 0) self.step = abs(self.transitioning_to - self.color) / (FRAME_RATE / 2) self.increasing = False else: self.increasing = False self.on = False self.color = transition(self.color, self.transitioning_to, self.step) self.light.setRGBRaw( *map(lambda x: clamp(0, x, 1), self.color.components) ) def fade_to_black(self, steps): hue = self.color.to_HSV().h() sat = self.color.to_HSV().s() value = self.color.to_HSV().v() new_color = color.HSV(hue, sat, value - value / steps * self.fade_step).to_RGB() self.fade_step += 1 self.light.setRGBRaw(*map(lambda x: clamp(0, x, 1), new_color.components)) class TwinkleShow(Show): def init(self): 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() self.tick_count = 0 def update(self): if self.tick_count > 1000: fade_time = 3 # sec if self.tick_count > 1000 + fade_time * FRAME_RATE: self.stop() return for light in self.lights: light.fade_to_black(fade_time * FRAME_RATE) else: for light in self.lights: light.update() self.tick_count += 1 print self.tick_count def compute_coverage(fish, x, y): """Calculate the coverage of a fish over a box.""" middle_of_fish = fish.x + fish.width / 2 middle_of_panel = x + 0.5 diff = abs(middle_of_fish - middle_of_panel) / (fish.width / 2) scale = 1 - diff min_x = min(max(x, fish.x), x + 1) max_x = max(min(x+1, fish.x + fish.width), x) min_y = min(max(y, fish.y), y + 1) max_y = max(min(y+1, fish.y + fish.height), y) coverage = (max_x - min_x) * (max_y - min_y) return coverage * scale def pick_ocean_HSV(sky_brightness): """Return a random color in an appropriate interval.""" min_hue = 236 / 360 max_hue = 250 / 360 min_sat = 0.25 max_sat = 0.6 hue = random.random() * (max_hue - min_hue) + min_hue saturation = random.random() * (max_sat - min_sat) + min_sat return color.HSV(hue, saturation, sky_brightness) def transition(start, end, step): """Return a vector equal to (end - start) * step + start.""" if start == end: return start diff_vector = end - start step_vector = diff_vector / abs(diff_vector) * step result = step_vector + start return result def random_change(): """Return a random amount that a light should change each frame.""" min_time = int(2 * FRAME_RATE) max_time = int(6 * FRAME_RATE) return 1 / random.randint(min_time, max_time) def clamp(low, x, high): """Return v such that low <= v <= high and |x - v| is minimal.""" return max(low, min(x, high))