Tutorpip← About this lessonOpen the sheet →Download PDF
Ages 8+ · Coding · Build a project
A game that counts
1when flag clicked2set apples to 03when apple touches basket4change apples by 1
A game counts with a variable.
The big ideaA variable is a named box that holds a value until a block changes it.

How it works

A game has to remember things, like how many apples you caught. It keeps each count in a variable, a box with a name.

A set block puts a value into the box. A change block adds to the value already inside.

A variable keeps its value until a block changes it. It never forgets by itself, and it never starts at zero by itself.

So a game starts with set to zero, under the flag block, to give every new game a fresh start.

Flag: setapples to 0Apple touchesthe basketchange applesby 1The box shows1, 2, 3
How Noor's game counts.

Example 1: Counting up

  1. The box apples starts at 0.
  2. Each apple that lands in the basket adds one: 0 + 1 = 1, then 1 + 1 = 2, then 2 + 1 = 3.
  3. The box shows 1, then 2, then 3.

Example 2: Set and change

  1. The box apples holds 5.
  2. Change apples by 1 adds one: 5 + 1 = 6.
  3. Set apples to 0 puts a new value in, so the box holds 0.

Example 3: The box that said nine

  1. Teo caught nine apples, and the box held 9.
  2. Noor pressed the flag for her turn, but nobody had told the box to start again at zero.
  3. The mix-up to fix: add set apples to 0 right under the flag block.

Example 4: Another variable

  1. The game also has a box called lives, and the player starts with 3.
  2. The block set lives to 3 puts a 3 in the box.
  3. Games use variables to remember things, like apples caught or lives left.

Words to know

variable
A named box that holds a value.
set
A block that puts a value into the box.
change
A block that adds to the value in the box.
sprite
A character on the stage, like the basket.

Watch out

Try it

  1. The box apples holds 5. What does it hold after change apples by 1?
  2. The box apples holds 5. What does it hold after set apples to 0?
  3. Which block gives every new game a fresh start: set apples to 0 or change apples by 1?
  4. Does a variable forget its value by itself?
  5. Which one holds a number: the apples box or the basket?
Answers: 1. 6 · 2. 0 · 3. set apples to 0 · 4. No · 5. The apples box
Try at homeWrite apples on a cup and start it empty: set apples to zero. A grown-up says change, and you drop in one pebble. When they say set to zero, you empty the cup. Say how many pebbles are in the box after each step.
For grown-upsDo the Mission: write apples on a cup and start it empty, which is set apples to 0. You say change, and your child drops in one pebble. When you say set to zero, they empty the cup and say how many pebbles are in the box after each step.
Open these notes on screenScan the code, or go to tutorpip.com.