Enemy brains and pathfinding
Aliens that fly straight at you
In Kai and Remy's first Hyper Shards, every alien flew in a straight line toward the ship. After one wave, players knew all their tricks.
Game AI is the set of rules that decides what enemies and other characters do. It is not the same as the AI tools our studio uses. Claude Opus 5.5 is a large language model, trained on huge amounts of text, that helps us write code. Game AI is usually a few dozen lines of ordinary code we write ourselves, running every frame on the player's device.
Simple behaviours
- PatrolMove along a set route and back, like a drone sweeping left and right.
- ChaseHead toward the player. A Hyper Shards diver turns toward the ship.
- FleeMove away, like a shard thief that runs once it is hit.
- Attack patternA fixed, learnable move: the boss fires a ring of 12 bullets, then a spread of 3.
Most enemies are built from a few simple behaviours. In Pac-Man (1980), the red ghost aims at Pac-Man's own tile, while the pink one aims four tiles ahead to cut you off.
Each behaviour is a state, and an enemy is in one state at a time. A finite state machine is the rule list that says when to switch: "patrol until the ship is closer than 200 pixels, then chase". The Game loops and state machines lesson goes deeper.
Bug hunt
1// one state at a time: patrol, chase or flee2function think(d, ship) {3 const dist = Math.hypot(ship.x - d.x, ship.y - d.y);4 if (d.state === "patrol" && dist > 200) {5 d.state = "chase";6 }7 if (d.state === "chase" && d.health <= 1) {8 d.state = "flee";9 }10}
Divers ignore the ship when it flies close, but start chasing when it is far away. Which line is the bug?
- Line 3
- Line 4
- Line 7
- Line 8
Show the answer
Line 4
Line 4 switches to chase when the distance is more than 200 pixels. It should be dist < 200, so divers react only when the ship comes close. Math.hypot is the distance formula from the Game math lesson.
Telegraph every attack
A telegraph is a warning before an attack: a wind-up, a glow, a sound, or a line showing where a shot will go. It turns "that came from nowhere" into "I should have dodged".
The Hyper Shards boss laser glows red and draws a thin aim line for one second before it fires, so players who watch can always get out of the way. In Garden Defense, the soil shakes before a mole pops up.
Bigger attacks get longer, louder warnings. And never let other effects cover a warning, or players cannot see it.
Quick check
The one-second glow before the boss laser fires makes the fight too easy, so it should be removed.
- True
- False
Show the answer
False
The warning makes the attack fair, not easy. Players still have to notice it and move in time. Without it, getting hit feels like the game cheated.
Finding the way: breadth-first search
A Garden Defense map is a grid of tiles, and some are blocked by hedges. How does a beetle find its way to Sprig?
Breadth-first search (BFS) spreads out from the start like ripples in a pond. It visits every open tile 1 step away, then every tile 2 steps away, then 3, and notes for each tile which tile it came from. When it reaches Sprig, it follows those notes backwards, and that is the path. Because it goes ring by ring, the first path it finds is a shortest one.
Bolt says: Tower defense games often run BFS once, outward from the goal. Then every tile knows its next step, and 100 beetles share one map. This is sometimes called a flow field.
A*: search with a good guess
BFS searches in every direction, even away from Sprig. A* (say "A star") adds a guess. For each tile, it adds the steps taken so far to a guess of the steps left, and explores the lowest total first.
On a grid, a common guess is the Manhattan distance: steps across plus steps down, ignoring walls, like walking city blocks. As long as the guess is never more than the real distance, A* still finds a shortest path, usually after checking far fewer tiles.
Peter Hart, Nils Nilsson and Bertram Raphael published A* in 1968, to plan routes for Shakey, an early robot.
Count the steps
1// S = snail, P = Sprig2// # = hedge (blocked), . = grass3// moves: up, down, left or right4 5S . . . .6# # # # .7P . . . .
Sprig is 2 tiles below the snail, so the Manhattan distance is 2. How many steps is the shortest real path?
- 2
- 6
- 10
- 12
Show the answer
10
The hedges block the way down, so the snail goes 4 right, 2 down and 4 left: 10 steps. Manhattan distance ignores walls, so it can be too low but never too high. That is exactly the kind of guess A* needs.
Steering and flocking
Pathfinding says where to go. Steering says how to move there smoothly: turn a little each frame toward the target, never faster than a top turning speed, and slow down when close.
In 1986, Craig Reynolds made boids, simulated birds that flock with three rules. Each bird only looks at its neighbours:
- Separation: don't crowd the others.
- Alignment: fly the same way they do.
- Cohesion: stay near the middle of the group.
A swarm of Hyper Shards drones with these rules moves like a real flock, with no leader.
Fun, not perfect
A perfect enemy is easy to write: an alien that always knows where you are and never misses. Playing against it is miserable. The goal is an enemy that is fun to beat.
- Reaction delay: wait a moment, like 0.3 seconds, before turning to chase.
- Aim error: add a little randomness, so some shots miss.
- Take turns: only two aliens attack at once while the rest circle.
- Stay readable: the same telegraph always means the same attack.
Keep these numbers in balance.json, so playtests can tune them.
Bolt says: Next lesson: procedural generation, where rules and randomness build levels for us.
Fact-checked 9 October 2026
- Game Developer: the Pac-Man dossier gamedeveloper.com
- Red Blob Games: introduction to A* redblobgames.com
- Red Blob Games: flow field pathfinding redblobgames.com
- Wikipedia: A* search algorithm en.wikipedia.org
- Wikipedia: Boids en.wikipedia.org