Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions boids/boids.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def getavgpos(self):
avgx += b.xc
avgy += b.yc
bcount += 1

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the following improvement in Function Boid.getavgpos:

avgx = avgx/bcount
avgy = avgy/bcount
if bcount <= 2:
Expand All @@ -112,19 +112,15 @@ def getavgdir(self):
if b.withinrange(self):
avgd += b.rot
bcount += 1

avgd = avgd/bcount
return avgd

return avgd/bcount
Comment on lines -115 to +116
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Boid.getavgdir refactored with the following changes:


def cohesion(self):
ax, ay = self.getavgpos() # average x and y
if self.alone:
pygame.draw.rect(screen, (0, 0, 255), (ax%width, ay%height, 5, 5))
else:
pygame.draw.rect(screen, (0, 0, 255), (ax%width, ay%height, 5, 5)) # just draws a rectangle at where the average position is
pygame.draw.rect(screen, (0, 0, 255), (ax%width, ay%height, 5, 5))
relx = ax - self.x
rely = ay - self.y

Comment on lines -121 to +123
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Boid.cohesion refactored with the following changes:

This removes the following comments ( why? ):

# just draws a rectangle at where the average position is

rota, l = getVectorfromXY(relx, rely)
print(rota)
rotc = ((rota - self.rot) * 0.3) # change in rotation needed: change 0.5 to other values to increase/decrease sensitivity
Expand All @@ -144,15 +140,11 @@ def separate(self):
ox, oy = getXYFromVector(b.rot, 10)
rx = x - ox
ry = y - oy
if rx > 0 and ry > 0:

if rx > 0 and ry > 0 or (rx <= 0 or ry >= 0) and (rx >= 0 or ry >= 0):
self.rot += tcs
elif rx > 0 and ry < 0:
self.rot -= tcs
elif rx < 0 and ry < 0:
self.rot -= tcs
else:
self.rot += tcs
Comment on lines -147 to -155
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Boid.separate refactored with the following changes:

self.rot -= tcs



Expand Down Expand Up @@ -206,22 +198,28 @@ def getVectorfromXY(xc, yc):
center = Object(0, 0, 5, 5, (0, 0, 255))
backg = Object(0, 0, width, height, (0, 0, 0))

boids = []
boids = [
Boid(
randint(0, width),
randint(0, height),
pygame.image.load("boid.png"),
150,
)
for _ in range(100)
]


Comment on lines -209 to 211
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 209-224 refactored with the following changes:

This removes the following comments ( why? ):

# add boids

# add boids
for i in range(100):
boids.append(Boid(randint(0, width), randint(0, height), pygame.image.load("boid.png"), 150))
playerquit = False
main = True

while not playerquit:
backg.draw()
for i in boids:
i.update()

center.x, center.y = boids[0].getavgpos()
#center.draw()

for event in pygame.event.get():
if event.type == QUIT:
main = False
Expand Down
10 changes: 6 additions & 4 deletions boids/boidtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def getavgpos(self, dist):
avgx += b.x
avgy += b.y
bcount += 1

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the following improvement in Function Boid.getavgpos:

avgx = avgx/bcount
avgy = avgy/bcount
print(avgx, avgy)
Expand Down Expand Up @@ -117,10 +117,12 @@ def getVectorfromXY(xc, yc):

center = Object(0, 0, 5, 5, (0, 0, 255))
backg = Object(0, 0, 1920, 1080, (0, 0, 0))
boids = []
boid = Boid(100, 100, pygame.image.load("boid.png"))

boids.append(Boid(randint(500, 1000), randint(500, 1000), pygame.image.load("boid.png")))
boids = [
Boid(randint(500, 1000), randint(500, 1000), pygame.image.load("boid.png"))
]

Comment on lines -120 to +125
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 120-134 refactored with the following changes:

playerquit = False
main = True
while not playerquit:
Expand All @@ -131,7 +133,7 @@ def getVectorfromXY(xc, yc):
for i in boids:
i.update()
i.draw()

center.x, center.y = boids[0].getavgpos(500)
center.draw()
#print(pygame.mouse.get_pos())
Expand Down