Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jcxl/flock_game
Browse files Browse the repository at this point in the history
Conflicts:
	tests/bird_test.py
  • Loading branch information
jxcl committed Nov 9, 2013
2 parents 452a770 + c7eee43 commit e6f3dff
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
43 changes: 40 additions & 3 deletions bird.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pygame

import math

class Bird():

Expand All @@ -10,12 +10,13 @@ def __init__(self, position, velocity, bird_id):

self.PERCEPTION_LIMIT = 1
self.TOO_CLOSE = 1
self.VISION_ANGLE = 1
self.VISION_ANGLE = 160

self.position = position
self.velocity = velocity
self.bird_id = bird_id


def __str__(self):
return str(self.bird_id)

Expand All @@ -31,6 +32,12 @@ def distance_squared(self, pos1, pos2):
y_comp = (pos1[1] - pos2[1])

return (x_comp**2 + y_comp**2)

def diff_pos_x(self, b):
return self.position[0] - b.position[0]

def diff_pos_y(self, b):
return self.position[1] - b.position[1]

def birds_in_range(self, bird_list, perception_limit):
pl_squared = perception_limit**2
Expand All @@ -44,4 +51,34 @@ def birds_in_range(self, bird_list, perception_limit):
if distance_sqrd < pl_squared:
in_range.append(b)

return in_range
return in_range

def heading(self):
return math.atan2(self.velocity[1], self.velocity[0])

def birds_in_angle(self, bird_list):
in_angle = []

for b in bird_list:
x = self.diff_pos_x(b)
y = self.diff_pos_y(b)
b_angle = math.atan2(x,y)

if math.fabs(b_angle - self.heading()) < math.radians(self.VISION_ANGLE):
in_angle.append(b)

return in_angle

def cohesion_sensor(self, bird_list):
a_sensor = (0,0)
x_comp = 0
y_comp = 0

if len(bird_list) > 0:
for b in bird_list:
x_comp += self.diff_pos_x(b)
y_comp += self.diff_pos_y(b)
x_comp = x_comp/len(bird_list)
y_comp = y_comp/len(bird_list)

return (x_comp,y_comp)
13 changes: 13 additions & 0 deletions tests/bird_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ def test_in_range(self):
in_range = b1.birds_in_range(b_list, 8)
self.assertEquals(in_range[0].bird_id, 1)
self.assertEquals(len(in_range), 1)

def test_in_angle(self):
b1 = bird.Bird((0, 0), (0, 1), 0)
b2 = bird.Bird((0, -5), (0, 0), 1)
b3 = bird.Bird((-10, -10), (0, 0), 2)

b_list = [b1, b2, b3]
in_angle = b1.birds_in_angle(b_list)
for x in in_angle:
print x.bird_id
self.assertEquals(in_angle[0].bird_id,1)
self.assertEquals(len(in_angle), 1)

0 comments on commit e6f3dff

Please sign in to comment.