コード例 #1
0
def update_boids(boids, time):

    for index in range(len(boids)):
        b = boids[index]
        b[2] += cohesion(index, boids)[0]*10
        b[3] += cohesion(index, boids)[1]*10
        b[2] += separate(index, boids)[0]*5
        b[3] += separate(index, boids)[1]*5
        b[2] += align(index, boids)[0]
        b[3] += align(index, boids)[1]
        # Limit velocity to 500 pixels per second horizontally and vertically
        b[2] = speed_limit(b[2], 500)
        b[3] = speed_limit(b[3], 500)

        # Update the boid's position based on its velocity and the
        # time that has passed since the last update.
        b[0] += float(b[2])/1000 * time
        b[1] += float(b[3])/1000 * time

        # Make the boid bounce off the walls.
        if b[0] < 0:
            b[0] = 0
            b[2] = -b[2]
        elif b[0] > WIDTH:
            b[0] = WIDTH
            b[2] = -b[2]
        if b[1] < 0:
            b[1] = 0
            b[3] = -b[3]
        elif b[1] > HEIGHT:
            b[1] = HEIGHT
            b[3] = -b[3]
コード例 #2
0
def update_boids(boids, time, SEPARATION_MULTIPLIER, COHESION_MULTIPLIER, align_on, sl):

    for index in range(len(boids)):
        b = boids[index]
        b[2] += cohesion(index, boids)[0] * COHESION_MULTIPLIER
        b[3] += cohesion(index, boids)[1] * COHESION_MULTIPLIER
        b[2] += separate(index, boids, random_color, b[4])[0] * SEPARATION_MULTIPLIER
        b[3] += separate(index, boids, random_color, b[4])[1] * SEPARATION_MULTIPLIER
        if align_on == "y" or align_on == "Y":
            b[2] += align(index, boids)[0]
            b[3] += align(index, boids)[1]
        b[4] = separate(index, boids, random_color, b[4])[2]
        # Limit velocity to 500 pixels per second horizontally and vertically
        b[2] = speed_limit(b[2], sl)
        b[3] = speed_limit(b[3], sl)

        # Update the boid's position based on its velocity and the
        # time that has passed since the last update.
        b[0] += float(b[2]) / 1000 * time
        b[1] += float(b[3]) / 1000 * time

        # Make the boid bounce off the walls.
        if b[0] < 0:
            b[0] = 0
            b[2] = -b[2]
        elif b[0] > WIDTH:
            b[0] = WIDTH
            b[2] = -b[2]
        if b[1] < 0:
            b[1] = 0
            b[3] = -b[3]
        elif b[1] > HEIGHT:
            b[1] = HEIGHT
            b[3] = -b[3]