Simulate Bird Flocking Behavior with pygame: Algorithm and Code Explained

Bird flocking is a wonderful example of how simple behaviors can lead to complex, beautiful patterns in nature. We’ll learn the key principles behind this flocking behavior and write a python program to simulate this wonderful phenomenon.

The principles

  • Cohesion: Birds want to stay with the group, so they naturally move towards the center of the flock. It’s like friends sticking together at a crowded party.
  • Alignment: This continuous adjustment to match their neighbors allows birds to fly in harmony, preventing chaotic changes in the flock.
  • Separation: Birds maintain a ‘space’ around themselves to avoid collisions. This is a key rule for safe and coordinated movement within the flock.

Bird Flocking Simulation Algorithm

Setting Up the Simulation

  1. Define the space (width and height of the simulation area).
  2. Create the birds:
    • Set the desired number of birds.
    • Assign random starting positions within the space.
    • Give each bird a random initial velocity.

Each Frame of the Simulation

  1. For each bird in the flock:
    • Cohesion: Calculate the center of mass of the flock and adjust the bird’s velocity to move closer to it.
    • Alignment: Calculate the average velocity of the flock and adjust the bird’s velocity to align with it.
    • Separation: Check for nearby birds and adjust the bird’s velocity to avoid collisions.
  2. Update Positions: Using the new velocity, calculate each bird’s new position.
  3. Handle Boundaries: Make sure birds stay within the simulation area.
  4. Display the updated bird positions on the screen.

The pygame example



import pygame
import sys
import random
from pygame.locals import *

pygame.init()
clock = pygame.time.Clock()
display_x = 1200
display_y = 700
screen = pygame.display.set_mode((display_x, display_y))
lightgreen = (144, 238, 144)


class FlyingBird():
    def __init__(self):
        self.circle_radius = 2
        self.display_position_x = random.randint(self.circle_radius, display_x - self.circle_radius)
        self.display_position_y = random.randint(self.circle_radius, display_y - self.circle_radius)
        self.horizontal_speed = random.randint(-3, 3)
        self.vertical_speed = random.randint(-3, 3)

    def show(self):
        pygame.draw.circle(screen, (0, 0, 0), (self.display_position_x, self.display_position_y), self.circle_radius)

    def update(self, birds):
        center_of_mass_x = sum(b.display_position_x for b in birds) / len(birds)
        center_of_mass_y = sum(b.display_position_y for b in birds) / len(birds)

        self.horizontal_speed += (center_of_mass_x - self.display_position_x) * 0.001
        self.vertical_speed += (center_of_mass_y - self.display_position_y) * 0.001

        self.display_position_x += self.horizontal_speed
        self.display_position_y += self.vertical_speed

        if self.display_position_x + self.circle_radius >= display_x or self.display_position_x - self.circle_radius <= 0:
            self.horizontal_speed *= -1
        if self.display_position_y + self.circle_radius >= display_y or self.display_position_y - self.circle_radius <= 0:
            self.vertical_speed *= -1


total_bird_number = 1000
list_of_birds = [FlyingBird() for _ in range(total_bird_number)]

game_running = True
while game_running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.fill(lightgreen)

    for bird in list_of_birds:
        bird.update(list_of_birds)
        bird.show()

    pygame.display.update()






Practice More pygame example including this example code from github

Published by

mashiurrahman99

Programmer