How Does Enemy AI Work in Video Games? A Simple Guide
If you have ever asked how does enemy AI work in video games?, the short answer is simple. It is a small set of rules the game checks many times a second. There is no thinking mind behind the enemy that just cornered you. There is a goal, a way to choose a direction, and a few timers that decide when to move and when to fire. This guide walks through each piece in plain words, then shows the exact rules the malware tanks in Tank City Reboot use.
What game AI means
The word "AI" means two things today. One is the chat tool you type questions into. The other is game AI. The Wikipedia page on artificial intelligence in video games describes it as the code that controls non-player characters, the ones you do not steer yourself.
That same page makes a useful point. Game AI borrows ideas from computer science, control theory and robotics. It is not "true" artificial intelligence. When people say an enemy has a personality, that is often a scripted behavior that looks clever from the outside.
So when you ask how enemies "think", the honest answer is that they do not. They follow rules. The skill is in picking rules that feel alive.
How does enemy AI work in video games, step by step?
Every game runs a loop. It reads your input, updates the world, then draws the result, over and over. If you want the full picture, read how a game loop works. Enemy AI lives in the update step.
On each update, every enemy answers a few small questions:
- Where is my goal right now?
- Which way should I move?
- Is it time to fire?
The game then moves the enemy a tiny step. It checks whether that step hits a wall or another tank. That check is called collision detection. Because the loop runs so often, many tiny choices add up to something that looks like a plan.
States: one mode at a time
Many games give each enemy a few modes, called states. A guard might patrol, then chase when it spots you, then return when it loses you. Each state has its own rules. A switch from one state to another happens when a condition is met.
This setup has a name: a finite state machine. Wikipedia notes that new kinds of games in the 1990s pushed designers to use formal tools like this. The idea stuck because it is easy to read and easy to fix. You can draw the whole enemy brain as a few boxes and arrows.
Not every game needs many states. Some enemies only ever do one thing, like "head for the base". The interesting part is then how they choose a way to get there.
Finding a way around walls
An enemy that walks straight at its goal will get stuck on the first wall. So games need a way to move around obstacles. This is called pathfinding. Wikipedia lists pathfinding problems among the hard parts of games with many units moving at once.
There are two common answers:
- Pick a direction and react. The enemy picks a direction that leans toward its goal. When it bumps into something, it picks again. This is cheap and feels a bit wild.
- Follow a map of distances. The game works out, for every spot on the map, how far it is from the goal. Each enemy simply steps to the neighbouring spot that is closer. This feels much more deliberate.
Both Tank City games use one of these each, which makes them a neat side-by-side example.
Why randomness and timers keep it fun
A perfect enemy is no fun. It would always take the shortest path and always fire the instant you are in line. You could never get ahead of it.
So designers add two things. The first is randomness: a dice roll that sometimes sends the enemy a different way. The second is timers: a short wait between choices and between shots. Together they make enemies feel a little human. They also give you gaps to slip through.
Wikipedia's history section notes that early arcade games made AI opponents popular with distinct movement patterns and rising difficulty. The tools were simple then too.
Worked example: how a malware tank picks its way
In the first game at Tank City Reboot, your antivirus tank guards the CPU core from malware tanks. Here is what one malware tank does, taken from the game's code.

When it is time to choose a direction, the tank rolls a random number:
- Half the time it aims toward the CPU core.
- A quarter of the time it aims toward your tank.
- A quarter of the time it aims in a random direction.
A comment in the code sums it up: "Malware heads mostly for the CPU core, sometimes for the player, sometimes anywhere."
Next, the tank looks at which directions are open. The aimed direction gets a weight of 4. Every other open direction gets a weight of 1. Say the tank aims at the core, which is below it, and three ways are open: down, left and right. Down has weight 4, left 1, right 1. That is 6 in total. So it goes down 4 times in 6, about two thirds of the time. It goes left or right about one time in 6 each. That is why it usually, but not always, goes the way it aims.
The tank does not rethink every frame. It chooses again every 0.8 to 2.4 seconds, when it is lined up on the grid. If it gets stuck for 0.12 seconds, it has a 40% chance to fire, then picks a new direction other than the blocked one. That is how a tank stuck on a wall can blast its way through.
Firing is on its own timer. A malware tank fires every 0.6 to 1.8 seconds, and faster on looped stages. If you grab the Breakpoint power-up, every enemy freezes for 8 seconds, which pauses all of these rules at once.
A smarter map in Zero Day
The second game, Zero Day, uses the other answer. Tanks move freely in 8 directions. Enemies follow a distance map built over 8 pixel cells. Most head for the core. Botnet drones follow a map to you instead.
When a wall breaks, the game rebuilds the map. So enemies find the new gap at once. Botnet drones are fast, come in threes, and hunt you, not the core. The Cryptominer is a turret that fires rings of shots. Each threat is a small rule set, and the mix is what makes a stage feel busy.
Frequently asked questions
Do game enemies learn from you?
Usually not. Most game enemies follow fixed rules written by the designer. The malware tanks in Tank City Reboot use simple rules, a dice roll and a few timers.
What is a finite state machine in a game?
It is a way to give an enemy a few modes, like patrol and chase, with clear rules for switching. Only one mode is active at a time.
Why do enemies sometimes walk the wrong way?
Often it is on purpose. A bit of randomness makes enemies harder to predict and easier to beat, so the game stays fun.
How do enemies get around walls?
Some pick a direction and pick again when they bump into something. Others follow a map that says how far every spot is from the goal.
Get started
The best way to understand enemy AI is to watch it. Notice how often a malware tank drifts toward you, and how a stuck one starts firing. Then try Zero Day and see how the drones close in.
Play Tank City Reboot: it is free, plays in your browser on a computer or a phone, and needs no account.
Comments
No comments yet.
Sign in or make an account to comment.