summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hogenson <rayhogenson@openmailbox.org>2016-10-09 23:27:02 -0400
committerRaymond Hogenson <rayhogenson@openmailbox.org>2016-10-09 23:27:02 -0400
commit2dd0795104b14fbb7aeee95f9f6bcf349f119960 (patch)
tree4df0ebbb5172ecdf327e7afb28e8b1126a3600c4
parent9d8c8fa4251081b43610ad0805d14cc26e4b4e19 (diff)
parent9b60deedc508c74b249fd1da0776b5a719322807 (diff)
downloadbridge-2dd0795104b14fbb7aeee95f9f6bcf349f119960.tar.zst
Merge branch 'master' of github.com:rayhogenson/bridge
-rwxr-xr-xbridge.py6
-rw-r--r--color.py13
-rw-r--r--constants.py3
-rwxr-xr-xevents-example3.py4
-rw-r--r--fish_show.py60
5 files changed, 73 insertions, 13 deletions
diff --git a/bridge.py b/bridge.py
index 2018611..26beb24 100755
--- a/bridge.py
+++ b/bridge.py
@@ -12,8 +12,8 @@ else:
have_lumiversepython = True
class Bridge(object):
- WIDTH = 200
- HEIGHT = 2
+ WIDTH = BRIDGE_WIDTH
+ HEIGHT = BRIDGE_HEIGHT
def __init__(self):
if have_lumiversepython:
@@ -42,4 +42,4 @@ def main():
#show = SunriseShow(bridge)
show.run(framerate=FRAME_RATE)
-main() \ No newline at end of file
+main()
diff --git a/color.py b/color.py
index 058abb4..c65252b 100644
--- a/color.py
+++ b/color.py
@@ -7,7 +7,7 @@ class Vector(object):
__slots__ = ('x', 'y', 'z')
"""3-dimensional vector class."""
- def __init__(self, x, y, z):
+ def __init__(self, x=0, y=0, z=0):
"""Create a vector from its components."""
self.x = x
self.y = y
@@ -40,6 +40,17 @@ class Vector(object):
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z
+ def unit(self):
+ return self / abs(self)
+
+ def rot2d(self, theta):
+ costheta = math.cos(theta)
+ sintheta = math.sin(theta)
+ new_x = self.x * costheta - self.y * sintheta
+ new_y = self.x * sintheta + self.y * costheta
+ return self.__class__(new_x, new_y)
+
+
@property
def components(self):
return (self.x, self.y, self.z)
diff --git a/constants.py b/constants.py
index 1685c5a..34c6de0 100644
--- a/constants.py
+++ b/constants.py
@@ -20,3 +20,6 @@ MAX_SATURATION = 1
MIN_SATURATION = 0.5
MIN_VALUE = 0.3
MAX_VALUE = 1
+
+BRIDGE_WIDTH = 200
+BRIDGE_HEIGHT = 2
diff --git a/events-example3.py b/events-example3.py
index d21877b..18db630 100755
--- a/events-example3.py
+++ b/events-example3.py
@@ -114,7 +114,11 @@ def fixDirection(data, theta=None):
def doStep(data):
data.counter += 1
+<<<<<<< HEAD
if random.random() < 1 / 50:
+=======
+ if data.counter % 50 == 0:
+>>>>>>> 9b60deedc508c74b249fd1da0776b5a719322807
fixDirection(data)
#print(abs(data.tailAcceleration))
data.speed += data.drag * data.speed / abs(data.speed)
diff --git a/fish_show.py b/fish_show.py
index cda4174..7cd8300 100644
--- a/fish_show.py
+++ b/fish_show.py
@@ -1,5 +1,6 @@
from __future__ import division
import color
+from color import Vector
import random
import constants
from constants import *
@@ -18,15 +19,53 @@ class Fish(object):
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.x += self.step
- if self.x > 200:
- self.x = 0
- self.y = 0.25 + 0.25 * math.sin(math.pi * self.x / 10)
- #theta = random.random() * math.pi / 2 - math.pi / 4
- #self.y += 0.01 * math.sin(theta)
- #print(self.y)
+ 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."""
@@ -108,10 +147,13 @@ class FishShow(Show):
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, 23, 1.5, color.black, 1)
+ self.fish = Fish(0, 0.5, 23, 1.5, color.black, 1)
def update(self):
self.fish.update()
for light in self.lights:
- light.update(self.fish) \ No newline at end of file
+ light.update(self.fish)
+
+def rand_in_range(low, high):
+ return random.random() * (high - low) + low