Programming for Children
Primary 6 · Science and Elementary Technology
Programming for Children
Lesson 5 of 6
30 min
Let's begin
Niyonzima wants to make a Scratch story called Keza and the Lost Goat. He wants the reader to click on things and choose where Keza searches, and he wants the story to count the clues Keza finds. To count, he needs a variable. To let the reader take part, he needs events.
A variable is a named box that stores a value, and the value can change while the program runs: a score, the number of lives, the time left, or a player's name.
To make one: click Variables, then Make a Variable, type a name such as score, choose For all sprites or For this sprite only, and press OK. The variable appears on the stage.
| Block | What it does | Example |
|---|---|---|
| set [score] to (0) | Puts an exact value in the box, replacing what was there. | Start every game at 0. |
| change [score] by (1) | Adds to the value already in the box. | Add 1 point each time you catch something. Use −1 to count down. |
| show variable / hide variable | Shows or hides the box on the stage. | Hide the score on the title screen. |
In the Raspberry Pi project Ghostbusters, a ghost appears in random places and the player clicks it:
when green flag clicked
set [score] to (0)
when this sprite clicked
hide
change [score] by (1)
when green flag clicked
set [time] to (30)
repeat until <(time) = (0)>
wait (1) seconds
change [time] by (-1)
broadcast [end]
A good story has a beginning, a middle and an end. Events blocks let the reader decide when things happen:

| Part | What happens | Blocks used |
|---|---|---|
| Beginning | Village backdrop. Keza says "My goat is lost! Click me to start the search." | when green flag clicked, switch backdrop, say, set [clues] to (0) |
| Middle | The reader presses the left arrow for the forest or the right arrow for the river. Clicking hidden objects (a hoofprint, a rope, some chewed leaves) adds a clue. | when key pressed, switch backdrop, when this sprite clicked, change [clues] by (1) |
| End | When 3 clues are found, the goat appears and Keza says "Thank you for helping me!" | if (clues) = (3) then broadcast [found]; when I receive [found], show |
Tip
SET once at the start, CHANGE every time something happens. If you forget to set the score to 0, a new game starts with the old game's points.
Answers
What you learned today
Sources and credits
The exercises (Try it yourself and the practice questions) are written by Ganzaa.
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.
Quick check