Predator & Prey
Lotka–Volterra wolves, sheep, and grass — watch the population oscillations emerge live.
What this is
A self-running ecosystem in a single HTML file. Brown earth greens over as grass regrows. White sheep wander and graze. Dark wolves prowl with a soft red halo and a yellow eye-glint. Below the field, three lines — green grass, white sheep, orange wolves — record the last 1000 ticks of population. The classic Lotka–Volterra cascade plays out in front of you: grass crashes, sheep lag, wolves lag more, sheep collapse, wolves starve, grass rebounds. Repeat. Forever.
Why this is mind-blowing
The textbook predator–prey curves aren't programmed in — they emerge from three rules: grass regrows, sheep eat grass and reproduce, wolves eat sheep and reproduce. Every generation is its own story. Sometimes the wolves overhunt and die out, sometimes the sheep explode and strip the field bare. The HUD generation counter is a leaderboard for ecosystem stability.
Build a single-file HTML page that runs an entire Lotka–Volterra ecosystem on
one canvas. Earth that slowly greens, white sheep that graze, dark wolves that
hunt, and a triple-line live population graph below the field. When grass
collapses, sheep boom-and-bust, then wolves do too. Wolf or sheep extinction
flashes a banner and a fresh generation auto-spawns. Spatial hashing for
performance. No user input — autoplay forever.
Paste this into Claude, Cursor, or Copilot. Change one thing that matters to you.
What I learned shipping it
- A 60×40 grass `Uint8Array` regrowing 8 tiles per tick is enough world for the chain to oscillate cleanly.
- An 80×80 spatial hash on sheep makes wolf vision O(neighbors), not O(N·M) — keeps 200+ sheep and 30+ wolves smooth at 2 ticks/frame.
- Toroidal wrap is the difference between organic LV waves and edge-piled sims that look broken.