🎮
AI🚀 Ages 7-10Beginner 7 min read

AI in Video Games

How AI works in video games: enemies that chase you, pathfinding, difficulty that changes, and what game AI can and cannot really do.

Key takeaways

  • Game characters follow rules written by people, not real thinking
  • AI helps enemies chase you, find paths and pick what to do next
  • Most game AI uses simple 'if this, then that' rules
  • Game AI can look smart but it does not really understand the game

The characters in your game

When you play a video game, you are not alone. There are enemies that chase you, friends that help you, and creatures that wander around. These are called characters the computer controls. People often call them NPCs, which means "non-player characters".

It can feel like these characters are thinking. The enemy seems to want to catch you. But there is no real thinking inside. Each character is following rules that a person wrote in the game's code.

This is what people mean by AI in games: not a brain, but a clever set of rules that makes characters act in interesting ways.

Rules that look smart

Most game AI is built from simple rules. The most common kind is "if this, then that".

Here are some examples a game might use:

  • If the player is far away, then walk around slowly.
  • If the player comes close, then chase them.
  • If your health is low, then run away and hide.
  • If you cannot see the player, then go back to your guard spot.

A game checks these rules many times every single second. So a goblin that walks calmly, then suddenly spots you and charges, is not surprised. It just had a rule waiting: "if you see the player, attack".

These "if this, then that" rules are exactly what coders learn early on. You can see how they work in Making Decisions with If.

Finding a path around walls

One tricky job for a game character is moving around the world. Real game maps have walls, doors, rivers and ledges. A character cannot just walk in a straight line toward you, or it would bump into a wall and get stuck.

So games use pathfinding. This is a clever way to work out a route from where the character is to where it wants to go, going around obstacles.

Imagine you are in a maze and you want to reach the exit. You try one way, hit a dead end, back up, and try another. Pathfinding does something like this, but very fast, before the character even moves. It tests many possible routes and picks a good one.

This is why a smart enemy can chase you around a corner instead of getting stuck against the wall. The game found a path for it.

Patterns and watching the player

Some games feel like they "know" you. They might get harder when you are doing well, or easier when you keep losing. This is called dynamic difficulty.

The game watches patterns in how you play. Are you winning every fight? Then it might send tougher enemies. Are you dying a lot? Then it might give you more health or weaker enemies. Spotting patterns like this is a big part of AI. You can learn more in What Is a Pattern?.

A few modern games go further and use machine learning, where the computer learns from lots of examples instead of fixed rules. If you want to understand that idea, see What Is Machine Learning?. But this is still rare. Most game AI you meet is good old "if this, then that" rules.

What game AI cannot do

Game AI can look amazing. But it is important to be honest about what it really is.

A game enemy does not understand the story. It does not feel angry or scared. It does not know it is in a game at all. It only follows the rules it was given.

This means game AI can also act silly. You may have seen a character walk into a wall again and again, or stand still while you sneak past. That is not the character being foolish. It is a rule that did not cover that exact situation. The person who made the game simply did not write a rule for it.

So when a game character does something clever, remember: a human designed that cleverness. And when it does something daft, a human missed a case. The "intelligence" comes from people, written down as rules the computer follows.

You can build this too

Every chasing enemy and every clever path started as code that a person wrote. Making game characters is one of the most fun ways to learn programming.

If you would like to try, a great first step is Making a Game in Scratch. You can write your own rules and watch your character follow them. That is real game AI, made by you.

Quick quiz

Test yourself and earn XP

What makes a game enemy chase you?

What is pathfinding in a game?

Does game AI really understand the story?

Why do some games get harder as you play?

Who decides the rules a game character follows?

FAQ

No. It is following rules a person wrote in code, like 'if the player is near, run toward them'. It can look clever, but it does not think or feel. It just checks rules very fast, many times each second.

Most cannot. They use fixed rules that do not change. A few modern games use machine learning so characters get a little better, but this is still uncommon. Usually a 'smart' enemy is just well-designed rules.