diff options
| author | Raymond Hogenson <rayhogenson@openmailbox.org> | 2016-10-15 10:06:20 -0400 |
|---|---|---|
| committer | Raymond Hogenson <rayhogenson@openmailbox.org> | 2016-10-15 10:06:20 -0400 |
| commit | c4999bbbfdbca65986e876ac78863041e85cfbe4 (patch) | |
| tree | b3f300c16fb369061ea7139c3e3684e7eac8a924 /fish_show.py | |
| parent | Merge branch 'master' of github.com:rayhogenson/bridge (diff) | |
| parent | Add img-disable css and usage (diff) | |
| download | bridge-c4999bbbfdbca65986e876ac78863041e85cfbe4.tar.zst | |
Merge branch 'master' of github.com:rayhogenson/bridge
Diffstat (limited to 'fish_show.py')
| -rw-r--r-- | fish_show.py | 91 |
1 files changed, 66 insertions, 25 deletions
diff --git a/fish_show.py b/fish_show.py index 7cd8300..6677ddd 100644 --- a/fish_show.py +++ b/fish_show.py @@ -7,6 +7,8 @@ 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): @@ -31,6 +33,9 @@ class Fish(object): def update(self): """Swim gently to the other side of the bridge.""" + if self.x > constants.BRIDGE_WIDTH or self.x < -self.width: + return False + self.speed += self.drag * self.speed if abs(self.speed) < self.tailThreshold: self.tailMoving = True @@ -44,15 +49,8 @@ class Fish(object): 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) + return True def fix_direction(self): min_theta = -0.32 @@ -65,7 +63,29 @@ class Fish(object): #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) - + + @classmethod + def by_type(cls, fish_type): + if (fish_type == 'jellyfish'): + return cls(0, 0.5, 23, 1.5, color.black, 1) + if fish_type == 'dolphin': + return cls(0, 0.5, 23, 1.5, color.black, 1) + if fish_type == 'boat': + return cls(0, 0.5, 23, 1.5, color.black, 1) + if fish_type == 'dory': + return cls(0, 0.5, 23, 1.5, color.black, 1) + if fish_type == 'nemo': + return cls(0, 0.5, 23, 1.5, color.black, 1) + if fish_type == 'whale': + return cls(0, 0.5, 23, 1.5, color.black, 1) + if fish_type == 'stingray': + return cls(0, 0.5, 23, 1.5, color.black, 1) + if fish_type == 'eel': + return cls(0, 0.5, 23, 1.5, color.black, 1) + if fish_type == 'shark': + return cls(0, 0.5, 23, 1.5, color.black, 1) + + class Light(object): """Represent a bridge light and its state.""" @@ -78,23 +98,21 @@ class Light(object): self.x = x self.y = y - def update(self, fish): + def update(self, depth, fishes): """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.transitioning_to = pick_HSV(depth).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()) + new_color = new_background + for fish in fishes: + coverage = compute_coverage(fish, self.x, self.y) + new_color = coverage * fish.color + (1 - coverage) * new_color self.light.setRGBRaw( *map(lambda x: clamp(0, x, 1), new_color.components) ) @@ -113,10 +131,12 @@ def compute_coverage(fish, x, y): return coverage * scale -def pick_HSV(): +def pick_HSV(depth): """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 + min_hue = constants.MIN_HUE + (10/360)*depth + min_saturation = constants.MIN_SATURATION + 0.05*depth + hue = random.random() * (constants.MAX_HUE - min_hue) + 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) @@ -143,17 +163,38 @@ def clamp(low, x, high): return max(low, min(x, high)) class FishShow(Show): + # 1 minute show + TICKS_PER_DEPTH = FRAME_RATE * 10 + MAX_DEPTH = 6 + 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.fish = Fish(0, 0.5, 23, 1.5, color.black, 1) - + self.fishes = set() + self.tick_count = 0 + self.depth = 0 def update(self): - self.fish.update() + event = self.recv_event() + if event and event[0] == 'spawn': + new_fish = Fish.by_type(event[1]['type']) + self.fishes.add(new_fish) + + for fish in list(self.fishes): + if not fish.update(): + self.fishes.remove(fish) for light in self.lights: - light.update(self.fish) + light.update(self.depth, self.fishes) + + self.tick_count += 1 + if self.tick_count % self.TICKS_PER_DEPTH == 0: + self.depth += 1 + print("DEPTH: ", self.depth) + + if self.depth > self.MAX_DEPTH: + self.stop() + def rand_in_range(low, high): return random.random() * (high - low) + low |
