from __future__ import division import color from color import Vector import random import constants from constants import * import math from show import Show class Fish(object): """Object displayed on the bridge, probably a fish.""" def __init__(self, x, y, width, height, color, step): """Create fish with default parameters.""" self.x = x self.y = y self.width = width self.height = height self.color = color self.speed = step self.step = step self.speed = Vector(0.1, 0) self.drag = -0.09 self.tailAccelMag = 0.4 self.tailAcceleration = Vector(self.tailAccelMag, 0) self.tailDuration = 2 self.tailMoving = False self.tailThreshold = 0.7 self.tailMoveCount = 0 def update(self): """Swim gently to the other side of the bridge.""" self.speed += self.drag * self.speed if abs(self.speed) < self.tailThreshold: self.tailMoving = True self.tailThreshold = rand_in_range(0.5, 1.4) self.fix_direction() if self.tailMoveCount > self.tailDuration: self.tailMoveCount = 0 self.tailMoving = False if self.tailMoving: self.speed += self.tailAcceleration self.tailMoveCount += 1 self.x += self.speed.components[0] self.y += self.speed.components[1] # # THIS NEEDS TO CHANGE # if self.x > constants.BRIDGE_WIDTH: self.x = -self.width if self.x < -self.width: self.x = constants.BRIDGE_WIDTH print(self.x, self.y) def fix_direction(self): min_theta = -0.32 max_theta = 0.32 if self.y < 0: min_theta = 0 if self.y > 1: max_theta = 0 #theta = rand_in_range(min_theta, max_theta) theta = random.gauss((max_theta+min_theta)/2, (max_theta - min_theta)/12) self.tailAcceleration = Vector(self.tailAccelMag, 0).rot2d(theta) 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.x = x self.y = y def update(self, fish): """Change color depending on where the fish is.""" if abs(self.transitioning_to - self.color) <= self.step: self.transitioning_to = pick_HSV().to_RGB() self.step = (random_change() * abs(self.color - self.transitioning_to)) new_background = transition( self.color, self.transitioning_to, self.step ) coverage = compute_coverage(fish, self.x, self.y) #coverage *= coverage new_color = coverage * fish.color + (1 - coverage) * new_background #new_color = new_background self.color = new_background if self.x == 30 and self.y == 0: pass #print(self.color.r(), self.color.g(), self.color.b()) self.light.setRGBRaw( *map(lambda x: clamp(0, x, 1), new_color.components) ) 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_HSV(): """Return a random color in an appropriate interval.""" hue = random.random() * (constants.MAX_HUE - constants.MIN_HUE) + constants.MIN_HUE saturation = random.random() * (MAX_SATURATION - MIN_SATURATION) + MIN_SATURATION value = random.random() * (MAX_VALUE - MIN_VALUE) + MIN_VALUE return color.HSV(hue, saturation, value) 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(constants.MIN_TIME * FRAME_RATE) max_time = int(MAX_TIME * 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)) class FishShow(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)] random_hue = random.random() fish_color = color.HSV(random_hue, 1, 20/100).to_RGB() self.fish = Fish(0, 0.5, 23, 1.5, fish_color, 1) def update(self): self.fish.update() for light in self.lights: light.update(self.fish) def rand_in_range(low, high): return random.random() * (high - low) + low