Every example from Daniel Shiffman's The Nature of Code, ported to Python 3.12+ and pygame-ce.
The book's examples are written in p5.js and Processing. To make it possible for developers more familiar with python, I have translated the examples to pygame-ce. A community fork of the pygame game engine.
The port uses pygames ecosystem, and solves the problems in a pythonic way.
You need Python 3.12+ and pygame-ce. The easiest setup uses
uv:
git clone https://github.com/Hohnik/noc-examples-pygame.git
cd noc-examples-pygame
uv syncThen run any example from the repo root:
uv run chapter5/Example_5_11_Flocking/main.pyWithout uv (plain pip)
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install pygame-ce noise
python chapter5/Example_5_11_Flocking/main.pyTo compare an example against the book's code, click the example's image or the pink book icon beneath it on natureofcode.com/examples, then choose Open in Web Editor.
The original Processing examples are here, along with ports to other languages.
Code is MIT licensed — see LICENSE.
This is a derivative work of The Nature of Code Examples (MIT, © 2014 Daniel Shiffman). The book's text and illustrations are separately licensed (CC BY-NC) and are not included here.
All of the ideas, and the structure of every example, come from Daniel Shiffman's The Nature of Code. If this repo is useful to you, buy the book and watch The Coding Train.
