コード例 #1
0
 def __init__(self):
     self.original = pygame.Surface((size, size), SRCALPHA)
     self.color = (random.randint(0, 255), random.randint(0, 255),
                   random.randint(0, 255))
     pygame.draw.polygon(self.original, self.color,
                         ((0, 0 + margin), (0, size - margin),
                          (size, size / 2)))
     self.pos = self.original.get_rect()
     self.pos = [random.randint(0, width), random.randint(0, height)]
     self.vel = v2.Vector()
     self.accel = v2.Vector()
     self.vel.setVectorC(random.randint(-5, 5), random.randint(-5, 5))
     self.accel.setVectorC(0, 0)
     self.radius = radius
     self.angle = self.vel.getAngle()
コード例 #2
0
        tempBoids.remove(me)
        proximity = []
        for others in tempBoids:
            x1, y1 = me.pos
            x2, y2 = others.pos
            if (x1 - x2)**2 + (y1 - y2)**2 < radius**2:
                proximity.append(others)
        me.proximity = proximity

        #if there is anything in the boid's proximity, it Tries to align, avoid bumping, and generally move towards mean position
        if me.proximity:
            #alignment with direction of other boids
            sum_an1 = 0
            sum_an2 = 0
            for x in me.proximity:
                remVel = v2.Vector()
                remVel.setVectorC(0, 0)
                remVel = v2.addVects(remVel, x.vel)
                if remVel.getMag():
                    remVel.setMag(1 / remVel.getMag())
                sum_an1 += remVel.components[0]
                sum_an2 += remVel.components[1]
            sum_an1 /= len(me.proximity)
            sum_an2 /= len(me.proximity)
            angVector = v2.Vector()
            angVector.setVectorC(sum_an1, sum_an2)
            me.avgAngle = angVector.getAngle()  #Average angle for neighbors
            me.accel.setVectorA(me.avgAngle, 1)

            #seperation
            me_post = (me.pos[0], me.pos[1])