Computer Science Grade 7 20 min

Lesson 7: How AI is Used in Games: Creating Intelligent Opponents

Discuss how AI is used in video games to create intelligent and challenging opponents.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what a Non-Player Character (NPC) is and explain the role of AI in controlling its behavior. Identify and describe at least three common behaviors for game AI, such as patrolling, chasing, and fleeing. Use 'if-then-else' statements to create a simple decision-making process for a game opponent. Explain the concept of a 'State Machine' in the context of an NPC's behavior (e.g., switching between 'patrolling' and 'chasing' states). Design a simple 'Sense-Think-Act' cycle for a game character on paper. Analyze a simple game scenario and predict the AI opponent's actions based on a given set of rules. Ever wondered how the computer-controlled characters in your favorite games seem to know exac...
2

Key Concepts & Vocabulary

TermDefinitionExample Non-Player Character (NPC)Any character in a game that is not controlled by a human player. Their actions are controlled by the computer's code (the AI).The Goombas in Super Mario, the villagers in Animal Crossing, or the enemy guards in a stealth game. Game AIThe code and logic that determines how NPCs behave and react to the player and the game world. It's about creating believable and challenging behavior, not true intelligence.The code that makes a dragon breathe fire when the player gets close, or makes a race car opponent try to avoid crashing. State MachineA model for an NPC's behavior. The NPC can only be in one 'state' (or mood/activity) at a time, and it switches between states based on rules.A guard might have three states: 'P...
3

Core Syntax & Patterns

The Sense-Think-Act Cycle 1. SENSE: Gather information about the game world. 2. THINK: Use rules to decide what to do. 3. ACT: Perform the chosen action. This is the fundamental loop for almost all game AI. In every moment (or 'frame') of the game, the AI runs through this cycle to seem alive and responsive. For example, a zombie 'senses' the player's location, 'thinks' about the best way to get there, and 'acts' by moving one step closer. Conditional Logic for Decisions (If-Elif-Else) if (condition is true): do_action_A elif (another_condition is true): do_action_B else: do_default_action This is the primary tool for building an AI's brain. You use it in the 'THINK' part of the cycle to check conditions (like...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
An enemy boss always fires its laser three times, then pauses for exactly two seconds before repeating the pattern. Based on the 'Common Pitfalls' section, how could you best improve this AI to be more challenging and engaging?
A.Make the laser do more damage but keep the same pattern.
B.Make the pause last for five seconds instead of two.
C.Randomize the number of shots (e.g., 2 to 4 shots) and slightly vary the pause duration (e.g., 1.5 to 2.5 seconds).
D.Remove the pause entirely so the boss is always firing.
Challenging
An AI guard's design is being debated. Design A: The guard has 360-degree vision and instantly attacks if the player is on screen. Design B: The guard has a 90-degree forward-facing 'cone of vision' and a 0.5-second delay before attacking. Which design is better according to the tutorial's principles, and why?
A.Design A is better because it is more powerful and challenging.
B.Design B is better because its limitations (vision cone, delay) make it feel more realistic and fair for the player.
C.Design A is better because it requires less code to program.
D.Design B is better because the game will run faster with a simpler AI.
Challenging
An AI guard's rules are: 1. If it sees the player, it enters 'CHASING' state. 2. If in 'CHASING' and loses sight for 3 seconds, it enters 'SEARCHING'. 3. If in 'SEARCHING' and doesn't find the player for 5 seconds, it returns to 'PATROLLING'. The guard sees the player, chases them around a corner, and doesn't see them again. What is the correct sequence of states?
A.PATROLLING -> CHASING -> PATROLLING
B.PATROLLING -> SEARCHING -> CHASING
C.CHASING -> PATROLLING -> SEARCHING
D.PATROLLING -> CHASING -> SEARCHING -> PATROLLING

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Chapter 6: Artificial Intelligence (AI) Basics: Making Machines Think

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.