Game Programming
Senior 2 · ICT
Game Programming
Lesson 1 of 4
22 min
A game is a program that reacts to the player many times every second. To build even a simple one you use the core ideas of all programming: events (something happens), loops (keep checking), conditions (if this, then that) and variables (remember the score). The mobile-money app on a phone uses exactly the same ideas.
In this unit you will plan and build a complete game in Scratch, a free block-based programming language made by the MIT Media Lab and available in more than 70 languages. In a block-based language you build a program by snapping blocks together instead of typing code.
In the Raspberry Pi Foundation's game Catch the Dots, the player turns a coloured wheel in the middle of the screen with the arrow keys. Coloured dots fly in from the corners. If a dot meets the part of the wheel with the same colour, the player scores 1 point; if the colours do not match, the player loses one of 3 lives. As time passes, the dots arrive faster. When the lives run out, the game stops.
Each part of that description is one of the game concepts in this lesson:
| In Catch the Dots | Game concept |
|---|---|
| The wheel and the dots | Sprites |
| The picture behind them | Backdrop |
| Points and lives | Variables (score and lives) |
| "Match colours to score; lose all 3 lives and the game ends" | Rules (scoring rule and lose condition) |
| The dots arrive faster over time | Levels (increasing difficulty) |

A sprite is a character or object on the stage that you can program: the player, an enemy, a ball, a coin. Each sprite has its own scripts. A sprite can have several costumes, which are different pictures of it. Switching quickly between costumes makes animation: a runner with two costumes, legs together and legs apart, seems to run when the costumes alternate.
A backdrop is a picture that fills the stage behind the sprites. Changing the backdrop can show a new level, a title screen or a "Game Over" screen. In Scratch, sprites change their look with costumes; the stage changes its look with backdrops.
The Scratch stage is 480 steps wide and 360 steps high. Every position is written as (x, y), with (0, 0) at the centre. x runs from −240 at the left edge to 240 at the right edge; y runs from −180 at the bottom to 180 at the top.
Worked example
A sprite starts at (0, 0). It runs "change x by 10" three times, then "change y by −50" once. x = 0 + 10 + 10 + 10 = 30 and y = 0 − 50 = −50, so it ends at (30, −50), a little right of the centre and below it. Is (250, 0) on the stage? No: the largest x is 240, so that point is just off the right edge.
A variable is a named store for a value that can change while the program runs. Games use variables for the score, the number of lives, the level, the time left and the speed.
Worked example
tracing the score.
| What happens | score | lives |
|---|---|---|
| Game starts | 0 | 3 |
| A dot meets the matching colour | 1 | 3 |
| A dot meets the matching colour | 2 | 3 |
| A dot meets the wrong colour | 2 | 2 |
| A dot meets the matching colour | 3 | 2 |
The rules say how the game is played and how it ends:
Worked example
difficulty in Catch the Dots. A variable delay, the number of seconds between new dots, starts at 8. Every 10 seconds it drops by 0.5, until it reaches 2. How long until the game is at its hardest? The delay must fall by 8 − 2 = 6 seconds in steps of 0.5, which takes 6 ÷ 0.5 = 12 steps. Each step takes 10 seconds, so 12 × 10 = 120 seconds (2 minutes).
Tip
Memory hook, a football match: sprites are the players, the backdrop is the pitch, variables are the scoreboard and the rules are the referee. If you can name all four for a game, you understand how it is built.
Name the game concept: (a) a bat that the player moves; (b) a desert picture behind everything; (c) the number of seconds left; (d) "if the ball touches the bottom edge, lose a life".
Solution: (a) sprite; (b) backdrop; (c) variable; (d) rule, specifically part of the lose condition.
A sprite at (100, 60) runs "change x by −30" twice, then "change y by 20" once. Where does it end?
Solution: x = 100 − 30 − 30 = 40 and y = 60 + 20 = 80, so it ends at (40, 80).
The score starts at 0 and lives at 3. The player catches 4 good items, hits 1 trap, catches 2 more good items, then hits 2 traps. What are the final score and lives?
Solution: score = 4 + 2 = 6; lives = 3 − 1 − 2 = 0, so the game is over with a score of 6.
A game's speed starts at 2 and increases by 1 each time the score goes up by 5. What is the speed when the score reaches 20?
Solution: 20 ÷ 5 = 4 increases, so speed = 2 + 4 = 6.
Summary
Sources and credits
The coordinate and variable worked examples and the practice items were written by Ganzaa.
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.