← Tutorials / Simulation

Boids in Python with Pygame

60 min read · Simulation

This lesson builds a full Boids implementation in Python. Clone the repo or follow along.


Prerequisites


pip install pygame numpy


Project Structure


boids/

main.py

boid.py

spatial_hash.py


Full Source


The complete implementation covers:

`SpatialHash` class with bucket resolution tuning

`Boid` class with vectorized NumPy rules

Pygame render loop at 60fps

Keyboard controls for live weight adjustment


See the `Swarm_tutorial/Swarm_Simulation/` folder in the Swarmsara repo for the full source.


Running It


python main.py


Controls:

`S` / `A` / `C` — increase separation / alignment / cohesion weight

`R` — reset to defaults

`+` / `-` — add / remove boids

← All TutorialsNeed Help? Book a Session →