Game Programming
Senior 2 · ICT
Game Programming
Lesson 3 of 4
25 min
Your design table says WHAT should happen; Scratch blocks say HOW. Each rule in the table becomes a small script: a trigger block on top, then blocks that check and act. In this lesson you will meet the block categories a game needs and read real game scripts line by line.
Scripts are written here as text, one block per line. Lines indented under a block sit inside it, and end marks where a C-shaped block closes. Pointed brackets < > show a yes/no block, and round brackets ( ) show a value.
| Category | Job in a game | Example blocks |
|---|---|---|
| Events (yellow) | Start scripts when something happens | when green flag clicked; when [space] key pressed; when this sprite clicked |
| Motion (blue) | Move and turn sprites | move (10) steps; turn right (15) degrees; go to x: () y: (); change x by (); point towards []; if on edge, bounce |
| Looks (purple) | Change appearance and screens | say (); switch costume to []; switch backdrop to []; show; hide |
| Control (orange) | Repeat, wait and decide | forever; repeat (10); repeat until <>; if <> then; if <> then … else; wait (1) seconds; wait until <>; stop [all]; create clone of [myself] |
| Sensing (light blue) | Ask yes/no questions about the game | touching [Basket]?; touching color []?; key [right arrow] pressed?; timer |
| Variables (dark orange) | Store and change values | set [score] to (0); change [score] by (1) |
| Operators (green) | Do maths, compare and combine | pick random (1) to (10); () + (); () > (); <> and <>; <> or <>; not <> |
In Catch the Dots, the player turns a wheel. This script belongs to the wheel sprite:
Events starts the script. Control (forever) repeats the checks many times a second. Sensing (key pressed?) asks whether a key is held down, and Control (if … then) acts only when the answer is yes. Motion turns the sprite 3 degrees each time. Holding the right arrow for 30 passes of the loop turns the wheel 30 × 3 = 90 degrees. For a basket that slides sideways instead, use change x by (10) and change x by (-10).
This script, also adapted from Catch the Dots, runs in each clone of a dot. A clone is a copy of a sprite made while the game runs, so one sprite can put many dots on the stage. The list start positions holds two items, −180 and 180.
Picking −180 or 180 at random for x, and again for y, puts the clone in one of the four corners: (−180, −180), (−180, 180), (180, −180) or (180, 180). Motion points it at the wheel, and Control (repeat until) moves it 1 step at a time until Sensing reports that it is touching the wheel. It then moves 5 more steps to overlap the wheel's colours. The if … else decides the result: the matching colour adds a point, any other colour costs a life. Finally the clone deletes itself so that it cannot count twice.
This script belongs to the stage, because it controls the whole game:
Variables set the starting values every time the game begins; without them, the score from the last game would carry over. wait until pauses only this script (the others keep running) until lives drops below 1. Then Looks shows the Game Over screen and stop [all] ends every script.
Worked example
A cherry at y = 170 runs repeat (10) containing change y by (-5). Each pass lowers it by 5, so at the end y = 170 − 10 × 5 = 120.
Tip
Shapes are clues: a hat block starts, a C block wraps, a pointed block asks yes or no, a rounded block holds a value, and a cap block ends. In exams, name the category AND the block, for example "Sensing: touching [Basket]?".
Name the category of each block: (a) switch costume to []; (b) pick random (1) to (10); (c) wait until <>; (d) key [space] pressed?; (e) change [score] by (1).
Solution: (a) Looks; (b) Operators; (c) Control; (d) Sensing; (e) Variables.
A sprite at x = −200 runs repeat (8) containing change x by (15). What is its final x?
Solution: −200 + 8 × 15 = −200 + 120 = −80.
A cherry runs: when green flag clicked → forever → if <touching [Basket]?> then → change [score] by (1). When it lands in the basket, the score jumps by 20 or more. Explain the bug and fix it.
Solution: The forever loop checks many times a second, and while the cherry stays in the basket every check adds 1. Fix: straight after change [score] by (1), add go to x: (pick random (-220) to (220)) y: (170) so that the cherry leaves the basket at once.
Which Control block lets a script choose between two different actions?
Solution: if <> then … else. The first group of blocks runs when the condition is true and the second group when it is false.
Why does the stage script begin with set [score] to (0)?
Solution: A variable keeps its last value. Resetting it when the green flag is clicked makes every game start from 0.
Summary
Sources and credits
The block-shape guide, the tracing 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.
when green flag clicked
forever
if <key [right arrow] pressed?> then
turn right (3) degrees
end
if <key [left arrow] pressed?> then
turn left (3) degrees
end
endwhen I start as a clone
go to x: (item (pick random (1) to (2)) of [start positions]) y: (item (pick random (1) to (2)) of [start positions])
point towards [controller]
show
repeat until <touching [controller]?>
move (1) steps
end
move (5) steps
if <touching color [blue]?> then
change [score] by (1)
else
change [lives] by (-1)
end
delete this clonewhen green flag clicked
set [lives] to (3)
set [score] to (0)
wait until <(lives) < (1)>
switch backdrop to [Game Over]
stop [all]