Platform Game Programming with AI
Senior 3 · ICT
Platform Game Programming with AI
Lesson 3 of 4
60 min
Your player can now run and jump, but nothing is at stake. A game becomes a game when there is something to win (points), something to lose (lives), something in the way (hazards), a goal, and a rival that reacts to what the player does. This lesson adds all five. The rival, an enemy with a small finite state machine, is the AI.
Definition
Variable: a named store for a value that can change while the program runs, such as score or lives. A variable for all sprites (global) can be read and changed by every sprite; one for this sprite only (local) belongs to one sprite.
Broadcast: a message sent to all sprites, which starts any script beginning with when I receive [message].
Sense–decide–act: the cycle of an AI character: sense the world (distance to the player), decide what to do (which state), then act (move).
Make two variables for all sprites: score and lives. Their monitors on the stage are the game's HUD. The Stage resets them when the game starts:
A coin adds to the score once, then disappears:
To make more coins, right-click the coin sprite → duplicate, and move each copy to its place (each has its own go to x: y: at the start of its script). A gem worth more is the same script with change [score] by (25).
Add this script to the Player. The red parts of the Level costume (spikes, lava) and the Enemy both cost a life. The one-second wait gives the player a moment of safety after returning to the start.
Add a Game Over backdrop and a stage script when I receive [game over] → switch backdrop to [Game Over]. Also make the falling script from Lesson 2 subtract a life.
A Flag sprite at the end of the level runs: when green flag clicked → wait until <touching [Player]?> → broadcast [next level]. The Level sprite answers when I receive [next level] → next costume, and the Player answers when I receive [next level] → go to x: (-200) y: (-100). One message, three sprites, a new level.
Worked example 1: a scoring rule
A game gives 10 points per coin and 25 per gem; at the goal it adds a bonus of 50 points for every life left. The player starts with 3 lives, collects 7 coins and 2 gems, and loses one life on the spikes.
In Scratch, the bonus is one block in the goal script, placed before the broadcast: change [score] by ((lives) * (50)). Because lives is a global variable, the Flag sprite can read it.
Paint an Enemy sprite and place it on the ground between x = 60 and x = 220 (adjust these numbers to your level). Make a variable state, for this sprite only. Its script is a two-state version of the Lesson 1 state machine:
| Stage of the cycle | Blocks | Meaning |
|---|---|---|
| Sense | (distance to [Player]) from Sensing | How far away is the player, in pixels? |
| Decide | set [state] to [chase] or [patrol] | Closer than 120: chase. Further than 200: patrol. In between: keep the current state. |
| Act (chase) | compare x positions, change x by (3) or (−3) | Walk towards the player along the ground. |
| Act (patrol) | move (2) steps; turn round at x = 60 and x = 220 | Walk to and fro on its section of ground. |
The enemy moves only along x, so it stays on its platform. Its chase speed, 3, is a little below the player's walking speed, 4, so a skilful player can escape. That choice is game design, not luck.
Worked example 2: tracing the enemy
The enemy is patrolling at x = 150. The player lands on the same ground at x = 40 and stands still, so the distance is .
The enemy closes 3 pixels a frame. To cover 110 pixels it needs , so 37 frames, about s. In practice the sprites touch a little sooner, because each costume has a width. If the player walks away at 4 pixels a frame, the gap grows by pixel per frame, and after the gap passes 200 the enemy returns to patrol.
Warning
Common mistakes
THE WASD CODER: how to make a chasing AI in Scratch, an enemy that follows the player. Compare it with this lesson's enemy, which chases only when the player is near.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.