This is a personal "fun" project to simulate and visualize the behavior of boids using Python. Boids are a type of artificial life program, simulating the flocking behavior of birds. The simulation mimics the way birds fly in flocks, fish swim in schools, or herds of animals move together.
Boids are a type of artificial life program, developed by Craig Reynolds in 1986, which simulates the flocking behavior of birds. The name "boid" corresponds to a shortened version of "bird-oid object", which refers to a bird-like object. Boids are governed by three simple rules:
- Separation: Avoid crowding neighbors (short-range repulsion).
- Alignment: Steer towards the average heading of neighbors.
- Cohesion: Steer towards the average position of neighbors (long-range attraction).
These simple rules result in complex and realistic flocking behavior.
This project simulates the behavior of boids using Python and visualizes the simulation using either Pygame (older testing around) or Matplotlib (recommended). The project is not intended to be a novel or optimal solution (would have used C++ if I wanted many many boids on the screen at once). It is a personal project for fun and learning as I was inspired by the murmuration behaviours seen in this video.
The project follows a modular design with clear separation of concerns:
-
Base Classes (
base_boids.py)BaseBoid: Abstract base class defining core boid behaviorBaseSpace: Abstract space for managing boid populationsCommunicationStrategy: Protocol for data transmission between components
-
Communication (
pipe_boids.py)- Implements pipe-based communication between simulation and visualization
- Extensible to other communication methods
-
Visualization
- Supports both Pygame (
boids_game.py) and Matplotlib (boids_animate.py) - Visualization choice handled by runner
- Supports both Pygame (
-
Runner (
boids_runner.py)- CLI interface for launching simulation
- Manages processes and communication setup
-
The runner creates two processes:
- Generator process: Runs the boid simulation
- Visualization process: Displays the simulation
-
Data flow:
Space → Communication Strategy → Pipe → visualiser -
Each frame:
- Boids update their positions
- States are written to communication channel
- visualiser reads and displays the new states
- Create a new class implementing
CommunicationStrategy - Implement required methods:
write_state(state: BoidState)write_frame_end()cleanup()
Example:
class NetworkCommunication(CommunicationStrategy):
def write_state(self, state: BoidState) -> None:
# Send state over network
pass- Create a new visualiser class
- Implement frame reading and display logic
- Add new option to runner CLI
- Inherit from
BaseBoid - override movement methods
move()OR override the movement helpersmove_together()move_away()handle_edges()
- Add parameters to
parameters.py - Use in relevant boid or space classes
- Always inherit from base classes for consistency
- Use type hints and documentation
- Follow the existing communication protocol
- Keep visualization logic separate from simulation logic
To run the simulation and view the animation, follow these steps:
-
Clone the repository:
git clone <repository-url> cd Boids
-
Install the required dependencies:
pip install -r requirements.txt
-
Run the simulation with Matplotlib:
python src/boids/boids_runner.py matplotlib

