Learn

Self-driving cars, recommendations systems, voice assistants. These are all great examples of what AI can achieve. But what does it do for video games?

Artificial intelligence (AI) is the ability of a computer system to perform tasks commonly associated with intelligent beings, such as driving cars, translating languages, folding laundry, and, yes, even playing video games.

AI in video games is most commonly used to control non-player character (NPC) behavior. AI can drive NPCs to act more intelligent as if a human player were controlling them. Having NPCs react to a player’s actions can make a game feel more responsive and adaptive.

AI powers an NPC’s decision-making ability, so when the player blocks an NPC’s path who’s trying to go from point A to point B, the NPC can choose an entirely different route on the fly. Interactivity introduced by AI can result in more immersive gaming experiences.

Basic AI in video games follows a simple three-step cycle:

  1. Sense: observe the surroundings
  2. Think: make a decision
  3. Act: carry out the decision

Let’s consider an enemy in a platformer game. To decide whether to attack the player, it gets the distance to the player’s character, asks itself if it’s close enough to attack the player, and if so, it attacks. Otherwise, it waits.

AI in an RPG is not so different: get all the player characters’ health, determine which character is weakest, and attack that character.

These are fundamental scenarios that can be programmed without an AI. Implementing advanced AI in video games is shown to be beneficial. Today’s games have become more innovative and realistic. For example:

  • Red Dead Redemption 2: one of the thousand revolutionary aspects of this game is the behavior of the NPCs. Every NPC that populates the world of this game lives a virtual life – completing chores, working, and pursuing hobbies.
  • FEAR: NPC opponents in this horror first-person shooter learn from the player’s playstyle. As the game progress, the player is forced to constantly switch strategies since enemies don’t repeat the same mistakes.
  • Minecraft: procedural generation of levels makes games replayable and eliminates the predictability of the in-game world.

AI in video games enables the integration of complex behaviors without needing to manually code behaviors, saving time and cutting costs.

Instructions

In Codey’s Adventure, we have gotten used to how the enemies act. What happens if we can alter the enemy’s Sense-Think-Act cycle? Use the sliders, checkbox, and dropdowns to create different enemy behaviors and try out the game. Enjoy!

  • The Enemy Walk Speed slider controls how fast the enemy walks when moving, and it has not detected Codey.
  • The Enemy Sprint Speed slider controls how fast the enemy sprints when it has detected Codey.
  • The Show Sense Radius checkbox toggles the red circles around each enemy, representing their sense radius.
  • The Enemy Sense Radius slider controls the size of each enemy’s sense radius.
  • The Enemy Idle Behavior dropdown selects what behavior the enemies have when Codey is not sensed.
  • The Enemy Sense Behavior dropdown selects what behavior the enemies have when Codey is sensed.

Codey’s Adventure Controls:

  • Move Left - A
  • Move Right - D
  • Jump - W
  • Binary Blast - Spacebar

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?