From b606a27d7ce7e91250c0c8795270bb1862e15e26 Mon Sep 17 00:00:00 2001 From: Raymond Hogenson Date: Sun, 16 Oct 2016 12:54:32 -0400 Subject: Create new sorts of fish to be displayed --- fish_show.py | 87 +++--------------------------------------------------------- 1 file changed, 3 insertions(+), 84 deletions(-) (limited to 'fish_show.py') diff --git a/fish_show.py b/fish_show.py index 6677ddd..62a07c9 100644 --- a/fish_show.py +++ b/fish_show.py @@ -6,85 +6,7 @@ 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.""" - 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 - 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] - - return True - - 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) - - @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) - +from fish_types import Fish class Light(object): @@ -112,7 +34,8 @@ class Light(object): 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 + fish_color = fish.color_at(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) ) @@ -194,7 +117,3 @@ class FishShow(Show): if self.depth > self.MAX_DEPTH: self.stop() - - -def rand_in_range(low, high): - return random.random() * (high - low) + low -- cgit v1.3.1 From e717c90bbdc3089f64c943afb98f44df031cab18 Mon Sep 17 00:00:00 2001 From: Raymond Hogenson Date: Sun, 16 Oct 2016 14:35:05 -0400 Subject: Improve eel movement --- fish_show.py | 2 +- fish_types.py | 47 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 7 deletions(-) (limited to 'fish_show.py') diff --git a/fish_show.py b/fish_show.py index 62a07c9..cb80d55 100644 --- a/fish_show.py +++ b/fish_show.py @@ -51,7 +51,7 @@ def compute_coverage(fish, x, y): 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 + return coverage * scale * fish.alpha_at(x, y) def pick_HSV(depth): diff --git a/fish_types.py b/fish_types.py index 63e082e..73b58c1 100644 --- a/fish_types.py +++ b/fish_types.py @@ -25,6 +25,8 @@ class Fish(object): self.tailAcceleration = C.Vector(self.tailAccelMag, 0) self.tailMoving = False self.tailMoveCount = 0 + if not hasattr(self, 'theta_scale'): + self.theta_scale = 12 @classmethod def by_type(cls, fish_type): @@ -71,14 +73,14 @@ class Fish(object): def fix_direction(self): min_theta = -0.32 max_theta = 0.32 - if self.y < 0: + if self.y + self.height < 0: min_theta = 0 - if self.y > 1: + if self.y > 2: max_theta = 0 #theta = rand_in_range(min_theta, max_theta) theta = random.gauss( - (max_theta+min_theta)/2, (max_theta - min_theta)/12 + (max_theta+min_theta)/2, (max_theta - min_theta)/self.theta_scale ) self.tailAcceleration = C.Vector(self.tailAccelMag, 0).rot2d(theta) @@ -86,6 +88,12 @@ class Fish(object): """May be overridden to provide multicolored fish.""" return self.color + def alpha_at(self, x, y): + """Return an additional scale amount for clear parts. + + This is overridden to have oddly shaped fish, such as the eel.""" + return 1 + class Jellyfish(Fish): color = C.HSV(298/360, 89/100, 53/100).to_RGB() @@ -174,6 +182,9 @@ class Whale(Fish): tailThreshold = 0.7 tailDuration = 3 tailAccelMag = 0.4 + theta_scale = 20 + + y = 0 white_x_min = 30 white_x_width = 8 @@ -210,11 +221,34 @@ class Stingray(Fish): class Eel(Fish): color = C.HSV(109/360, 83/100, 35/100).to_RGB() drag = -0.09 - width = 23 - height = 1.5 + width = 50 + height = 2 tailThreshold = 0.7 tailDuration = 2 - tailAccelMag = 0.4 + tailAccelMag = 0.1 + + front_offset = 0 + y = 0 + + def alpha_at(self, x, y): + rel_x = x - self.x + rel_y = y - self.y + eel_height = math.sin( + 2 * math.pi * (rel_x + self.front_offset) / self.width + ) / 2 + 0.5 + y_blend = abs(rel_y - eel_height) + if y_blend > 0.5: + return 0 + return (1 - y_blend) + + def update(self): + self.front_offset += abs(self.speed) + if self.front_offset > self.width: + self.front_offset = 0 + return super(Eel, self).update() + + def fix_direction(self): + pass class Shark(Fish): @@ -225,6 +259,7 @@ class Shark(Fish): tailThreshold = 0.7 tailDuration = 3 tailAccelMag = 0.4 + theta_scale = 20 def rand_in_range(low, high): -- cgit v1.3.1