What a bird sees
Every frame, each bird measures five things: its height, how fast it's rising or falling, the distance to the next pipe, and where that pipe's gap starts and ends.
inputs = [y, velocity, pipeDist, gapTop, gapBottom]The AI starts out terrible. Hundreds of birds with random brains crash within seconds. Give evolution a minute and see who wins.
How it learns
Nobody programs these birds to dodge pipes. They start with random brains and evolve. Scroll through one full cycle of the genetic algorithm that runs above.
01Senses
Every frame, each bird measures five things: its height, how fast it's rising or falling, the distance to the next pipe, and where that pipe's gap starts and ends.
inputs = [y, velocity, pipeDist, gapTop, gapBottom]Those five numbers flow through a small neural network: 5 inputs, 8 hidden neurons, 2 outputs. If FLAP scores higher than WAIT, the bird flaps. That's the whole brain.
if (flap > wait) jump()Every connection has a strength (a weight) and every neuron a bias. Line all 66 numbers up and you have the bird's DNA. Different numbers, different behaviour.
genes.length === 66 // 40 + 8 + 16 + 2We hatch 500 birds with completely random DNA. None of them know what a pipe is. Each bird is coloured by its DNA, so relatives look alike.
new Bird(randomGenes())The whole flock plays at once. Some never flap and drop like stones, others flap into the ceiling. Most crash within seconds; a lucky few slip through a pipe or two.
framesAlive++When the last bird falls, each one gets a fitness score: frames survived, plus a bonus that doubles with every pipe cleared. One more pipe beats any amount of hovering.
fitness = frames + 1000 * 2 ** pipesParents are picked on a roulette wheel where fitter birds own bigger slices: likely to win, never guaranteed. The champion also skips the wheel and is copied into the next generation untouched. That's elitism.
parent = spinWheel(fitness)Each child starts as an exact copy of its parent's DNA. Then every gene has a 10% chance of being nudged by up to ±0.5. Most nudges hurt, a few help, and selection keeps the helpful ones.
if (rand() < 0.1) gene += rand(-0.5, 0.5)Nature also mixes two parents. Switch on crossover in the lab and each child takes every gene from one parent or the other on a coin flip. In our tests it helps tiny flocks but slows big ones. Try it!
child[i] = coin() ? mom[i] : dad[i]Fly, score, select, mutate, repeat. Progress stalls for a while, then one lucky mutation cracks it and its descendants take over the flock. In this recorded run, generation 19 sailed past 150 pipes.
generation++Your turn
Every experiment below reconfigures the game at the top of the page and starts a fresh flock. Watch the score chart and the colours of the birds.
Yes. Each bird is driven by a genuine neural network. What's unusual is how it learns: there's no training data and no backpropagation, only random variation plus selection. This family of methods is called neuroevolution.
Each bird's colour is computed from its DNA, so relatives look alike. Early generations are a rainbow of unrelated random brains. As one family out-breeds the rest, the flock converges on its colour.
For a while every bird fails at the same hurdle and fitness barely moves. Then one mutation cracks it, that bird scores exponentially higher, wins most roulette spins, and its descendants flood the next generation.
Pipe heights are random every run. A champion that got lucky once may crash on a harder layout. That's why evolution works with a whole population rather than a single star.