Platform Game Programming with AI
Senior 3 · ICT
Platform Game Programming with AI
Lesson 1 of 4
45 min
In a platform game the player steers a character that runs and jumps between ledges, avoids gaps, spikes and enemies, collects coins, and reaches a goal at the end of each level. Super Mario Bros. (1985) made the type famous, and thousands of platform games have been made since, many of them in Scratch by pupils your age.
In S2 you planned and built a Scratch game with sprites, a score and levels. This unit builds a harder kind of game: one where the character falls under gravity, lands on platforms, and faces enemies that decide what to do. That decision-making is called game AI. At the end of the unit, you will also let a real machine-learning model, trained by you, control part of a game.
Definition
Platform game (platformer): a game in which a character moves and jumps between platforms, avoiding obstacles, to reach a goal.
Non-player character (NPC): a character controlled by the computer, not by a player, such as an enemy or a helper.
Game AI: the rules and algorithms that make NPCs behave as if they were intelligent: patrolling, chasing, fleeing or choosing between actions.
Game loop: the cycle a game repeats many times a second: read the input, update positions and rules, check collisions, then draw the screen.
Level mock-up drawn by Ganzaa.
| No. | Component | Job in the game | In Scratch |
|---|---|---|---|
| 1 | Player character | Moved by the player's keys | A sprite with walking and jumping costumes |
| 2 | Platforms and ground | Surfaces to stand and land on | A Level sprite whose costume is drawn in one colour |
| 3 | Collectables | Coins or gems that add to the score | Small sprites, often clones |
| 4 | Hazards | Gaps, lava, spikes: touching them costs a life | Parts of the level in a special colour, or sprites |
| 5 | Obstacles | Things to jump over or around | Sprites or parts of the level |
| 6 | Enemy (NPC) | Moves by itself and harms the player | A sprite with its own AI script |
| 7 | Goal | Reaching it ends the level | A flag or door sprite; a broadcast starts the next level |
| 8 | HUD (heads-up display) | Shows score, lives, time | Variable monitors on the stage |
The background (a backdrop) sets the scene, while sound and levels that get harder keep the player interested.
A game feels alive because it repeats a loop many times a second. In Scratch, a script inside a forever block runs about 30 times a second. On each pass, or frame:
Because a frame is only about of a second, small steps (a few pixels per frame) look like smooth movement.
Most game AI is not machine learning. It is a set of clear rules written by the programmer, chosen so that the enemies seem clever, but fair. Common behaviours:
| Behaviour | What the NPC does | A simple rule |
|---|---|---|
| Patrol | Walks back and forth along a route | Move 2 steps; at the edge of the platform, turn round |
| Chase (seek) | Moves towards the player | If the distance to the player is less than 120, point towards the player and move |
| Flee | Moves away from the player | If the player has a power-up, point away and move |
| Wander | Moves in random directions | Every 2 seconds, turn by a random angle |
| Guard | Waits at a post and returns to it | If far from the post and the player is out of sight, go back |
An enemy usually has several behaviours and switches between them. A neat way to organise this is a finite state machine.
Definition
Finite state machine (FSM): a model of behaviour with a fixed number of states (such as Patrol, Chase, Return), where the character is in exactly one state at a time and transitions to another state when a condition becomes true.
Pathfinding: an algorithm that finds a route around obstacles to a target. A widely used one is called A* (“A-star”).
Diagram drawn by Ganzaa. Each arrow is a transition, labelled with its condition. In Scratch, a variable called state can hold the current state.
Worked example 1: tracing the state machine
An enemy uses the FSM above. Its distance to the player is checked once a second: 300, 150, 110, 140, 190, 230, and then it reaches its post.
| Check | Distance | State before | Rule that applies | State after |
|---|---|---|---|---|
| 1 | 300 | Patrol | not < 120: stay | Patrol |
| 2 | 150 | Patrol | not < 120: stay | Patrol |
| 3 | 110 | Patrol | < 120: start chasing | Chase |
| 4 | 140 | Chase | not > 200: keep chasing | Chase |
| 5 | 190 | Chase | not > 200: keep chasing | Chase |
| 6 | 230 | Chase | > 200: give up | Return |
| 7 | – | Return | back at post | Patrol |
Notice that the enemy starts chasing at 120 but only gives up at 200. Two different limits stop it from flicking between Patrol and Chase when the player stands near one line.
Worked example 2: recognising AI in a famous game
In Pac-Man (1980), each ghost switches between modes, including Chase (hunt Pac-Man), Scatter (go to its own corner of the maze) and Frightened (wander away after Pac-Man eats a power pellet). That is a finite state machine. Each ghost also chases in its own way: the red ghost aims straight at Pac-Man, while the pink one aims a few squares ahead of him. Simple rules, but together they feel like a team that thinks.
Warning
Rule-based AI is not machine learning. A patrolling enemy follows rules the programmer wrote; it does not learn. Machine learning (S1 Unit 7) learns patterns from training data. Lesson 4 of this unit uses a trained model, and you will see the difference.
AI and Games (Tommy Thompson): how the enemies of the game Half-Life use finite state machines to decide what to do. Watch for the states and the conditions that switch between them.
You will build this game over the next two lessons. Plan it on paper first.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.