2. Before you start¶
This guide builds a small animated picture of Badshahi Masjid, the great Mughal mosque of Lahore completed in 1673, without drawing anything by hand. Every pixel is computed. You start from a single coloured square and add one idea at a time: lines, filled shapes, a palette, a still picture made of reusable parts, then time, light, and finally physics, with a kite on an elastic string and a flock of pigeons. The same picture is then rebuilt in plain C, the way programs written with graphics.h in Dev-C++ drew, so you see what high-level libraries do for you underneath.
The style is deliberately 8-bit: a 160 by 120 canvas and 16 colours, as on home computers of the 1980s. Small canvases make every idea visible, and the methods are the same ones used in modern games and scientific simulations.
2.1 What you will learn¶
-
Pictures: a picture as a grid of numbers; palettes; Bresenham’s line and the midpoint circle; filling shapes with inside tests; dithering.
-
Procedural modelling: describing architecture with geometry (a Mughal arch as two overlapping discs, an onion dome as a width that varies with height) and building a scene from a list of parts, using symmetry.
-
Animation: frames, the animation loop, everything as a function of time, a day-and-night cycle with a palette swap.
-
Simulation science: forces, Newton’s second law, numerical integration and why one form of Euler’s method explodes while another is stable; springs, drag and gusty wind; flocking from three local rules.
-
Design: drawing the program as a class diagram, a data-flow diagram, an entity–relationship diagram and a free-body diagram.
-
Low level: a framebuffer in C, the old BGI drawing calls, and how OpenGL and game engines relate to them.
2.2 Getting Python and the notebook¶
Open the Notebooks page of this guide and choose Open in Colab to run it in a web browser with nothing to install. On your own computer:
conda create -n pixels python=3.12 numpy matplotlib pillow jupyterlab
conda activate pixels
jupyter lab
Then open the downloaded pixel_animation.ipynb. Boxes labelled In [n] are code and Out [n] what it printed or drew. Run the cells in order from the top: each section uses functions defined in the ones before. The animated GIFs are written to a data folder beside the notebook.
2.3 Setup¶
This notebook builds an 8-bit animation of Badshahi Masjid, Lahore, from nothing: first single pixels, then lines, shapes and a still picture, then time, motion and physics. Nothing is drawn by hand. Every part of the picture is computed from a few numbers, so it can be changed, repeated and animated.
Libraries: numpy holds the picture as an array of numbers and does arithmetic on all pixels at once; matplotlib shows pictures in the notebook; Pillow saves images and animated GIFs. All three come with Colab and Anaconda.
import math
from dataclasses import dataclass, field
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
OUT = Path("data")
OUT.mkdir(exist_ok=True)
plt.rcParams.update({"figure.dpi": 110})
print("numpy", np.__version__)
numpy 2.0.1