MCPcopy Create free account
hub / github.com/ActiveState/code / Fish

Class Fish

recipes/Python/578007_processing_DEMO_Boids/recipe-578007.py:70–189  ·  view source on GitHub ↗

Fish(location) -> Fish

Source from the content-addressed store, hash-verified

68################################################################################
69
70class Fish:
71
72 "Fish(location) -> Fish"
73
74 HOW_WIDE = 6 # Width of the fish
75 HOW_LONG = 12 # Length of the fish
76
77 MAX_FORCE = 0.05 # Maximum directional steering force
78 MAX_SPEED = 60.0 # Maximum speed at which to travel
79
80 SEP_FACTOR = 1.5 # Arbitrary separation mutliplier
81 ALI_FACTOR = 1.0 # Arbitrary alignment mutliplier
82 COH_FACTOR = 1.0 # Arbitrary cohesion mutliplier
83
84 DESIRED_SEPARATION = 7 # Turn from each other when closer
85 NEIGHBOR_DISTANCE = 17 # Maximum distance for interactions
86
87 ########################################################################
88
89 # DO NOT CHANGE THE FOLLOWING SECTION
90
91 limits = math.hypot(HOW_LONG, HOW_WIDE)
92 radius = limits / 2
93
94 DESIRED_SEPARATION += limits
95 NEIGHBOR_DISTANCE += limits
96
97 TOP = 0 - radius
98 LEFT = 0 - radius
99 RIGHT = WIDTH + radius
100 BOTTOM = HEIGHT + radius
101
102 SHAPE = processing.Polygon(vector.Vector2(HOW_LONG / +2, 0),
103 vector.Vector2(HOW_LONG / -2, HOW_WIDE / +2),
104 vector.Vector2(HOW_LONG / -2, HOW_WIDE / -2))
105
106 del limits, radius, HOW_LONG, HOW_WIDE
107
108 __slots__ = 'location', 'velocity', 'steering', 'body_color', 'trim_color'
109
110 # END OF PRECALCULATED FISH VARIABLES
111
112 ########################################################################
113
114 def __init__(self, location):
115 "Initialize the fish with several vectors and colors."
116 self.location = location.copy()
117 self.velocity = vector.Polar2(random.random() * self.MAX_SPEED,
118 random.random() * 360)
119 self.body_color = ''
120 self.trim_color = ''
121
122 def paint(self, body_color, trim_color):
123 "Assign colors to the fish's body and trim (outline)."
124 self.body_color = body_color
125 self.trim_color = trim_color
126
127 def render(self, graphics):

Callers 2

add_fishMethod · 0.85
mouse_pressedMethod · 0.85

Calls 1

hypotMethod · 0.45

Tested by

no test coverage detected