diff options
| -rwxr-xr-x | twinkle.py | 77 |
1 files changed, 46 insertions, 31 deletions
@@ -11,14 +11,20 @@ class ShootingStar(object): """Object displayed on the bridge, probably a fish.""" def __init__(self): """Create fish with default parameters.""" - self.x = 0 + self.x = random.random() * BRIDGE_WIDTH self.y = random.random() * 1.5 self.width = 5 self.height = 0.5 - self.color = color.RGB(255/255, 210/255, 201/255) + self.color = color.RGB(1, 1, 1) + if random.random() >= 0.5: + self.speed = 8 + else: + self.speed = -8 def update(self): - self.x += 0.1 + self.x += self.speed + self.speed *= 0.9 + return abs(self.speed) > 1 and self.x < BRIDGE_WIDTH class Light(object): @@ -27,6 +33,7 @@ class Light(object): """Store all facts about a light.""" self.light = light self.color = color.black + self.result_color = self.color self.transitioning_to = self.color self.step = 0 self.on = False @@ -35,7 +42,7 @@ class Light(object): self.y = y self.fade_step = 0 - def update(self): + def update_base(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) @@ -54,11 +61,20 @@ class Light(object): self.on = False self.color = transition(self.color, self.transitioning_to, self.step) + self.result_color = self.color + + def update_star(self, star): + coverage = compute_coverage(star, self.x, self.y) + self.result_color = coverage * star.color + (1 - coverage) * self.result_color + + def blit(self): self.light.setRGBRaw( - *map(lambda x: clamp(0, x, 1), self.color.components) + *map(lambda x: clamp(0, x, 1), self.result_color.components) ) + + def fade_to_black(self, steps): hue = self.color.to_HSV().h() sat = self.color.to_HSV().s() @@ -70,10 +86,11 @@ class Light(object): class TwinkleShow(Show): def init(self): - self.lights = [Light(x, y, self.bridge.get_light(x, y)) - for y in xrange(self.bridge.HEIGHT) + 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.stars = set() self.tick_count = 0 @@ -83,11 +100,29 @@ class TwinkleShow(Show): 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) + for col in self.lights: + for light in col: + light.fade_to_black(fade_time * FRAME_RATE) else: - for light in self.lights: - light.update() + if random.random() < 0.01: + print "Adding star" + self.stars.add(ShootingStar()) + for col in self.lights: + for light in col: + light.update_base() + + for star in list(self.stars): + if not star.update(): + self.stars.remove(star) + + for x in xrange(int(star.x), int(star.x+star.width+1)): + if x < len(self.lights): + for light in self.lights[x]: + light.update_star(star) + + for col in self.lights: + for light in col: + light.blit() self.tick_count += 1 print self.tick_count @@ -106,18 +141,6 @@ def compute_coverage(fish, x, 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: @@ -127,14 +150,6 @@ def transition(start, end, 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)) |
