diff --git a/boids/boids.py b/boids/boids.py index 09fc446..bec9854 100644 --- a/boids/boids.py +++ b/boids/boids.py @@ -98,7 +98,7 @@ def getavgpos(self): avgx += b.xc avgy += b.yc bcount += 1 - + avgx = avgx/bcount avgy = avgy/bcount if bcount <= 2: @@ -112,19 +112,15 @@ def getavgdir(self): if b.withinrange(self): avgd += b.rot bcount += 1 - - avgd = avgd/bcount - return avgd + + return avgd/bcount 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 - + 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 @@ -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 + self.rot -= tcs @@ -206,11 +198,17 @@ 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) +] + -# 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 @@ -218,10 +216,10 @@ def getVectorfromXY(xc, yc): 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 diff --git a/boids/boidtest.py b/boids/boidtest.py index 70e20c2..6a0b1d2 100644 --- a/boids/boidtest.py +++ b/boids/boidtest.py @@ -74,7 +74,7 @@ def getavgpos(self, dist): avgx += b.x avgy += b.y bcount += 1 - + avgx = avgx/bcount avgy = avgy/bcount print(avgx, avgy) @@ -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")) +] + playerquit = False main = True while not playerquit: @@ -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())