New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Scratch› Variables and Keeping Score

Variables and Keeping Score

💻 Scratch

Create variables, change them during a game and display a live score.

Lesson 5 of 9 Tutorial
0 of 9 completed

A variable is a named box that holds a value. In a game it can store the score, lives, time or level.

By the end of this lesson you can:
  • Create a variable in Scratch.
  • Change a variable with blocks.
  • Show a variable on the stage.

Make a Variable

Open the Variables category and click Make a Variable. Name it score. Make sure it is available for all sprites if every sprite needs to read it.

Change the Score

When the player catches a falling apple, increase the score by one. Use a when green flag clicked block to set the score to zero at the start, then add one inside the collision check.

when green flag clicked
set [score v] to [0]

when I receive [catch apple v]
change [score v] by (1)

Show It on Stage

Tick the checkbox beside the variable to show it on the stage. You can drag the display to a corner. For a cleaner look, use a say block joined with the score.

Tip Give variables clear names. score, lives and level are easier to read than variable1 and variable2.

Practice

  1. Create a variable called lives and set it to 3 at the start.
  2. Subtract one life when the sprite touches a danger colour.
  3. Show the score on the stage and move the display to the top-left corner.

Hands-on exercise

Build a small catching game.
1. Create a variable named score.
2. Set score to 0 when the green flag is clicked.
3. Increase score by 1 whenever the catcher touches the falling object.
4. Reset the object to the top after each catch.

Recap

Variables store values that can change. Set them at the start, update them during play and show them when the player needs to see them.

Example

when green flag clicked
set [score v] to [0]

when I receive [catch apple v]
change [score v] by (1)

Exercises

Exercise 1. Score on every catch Easy
Build a small catching game.
1. Create a variable named score.
2. Set score to 0 when the green flag is clicked.
3. Increase score by 1 whenever the catcher touches the falling object.
4. Reset the object to the top after each catch.
Scratch exercise-1.txt
Expected Output
The score increases by one each time the catcher touches the falling object.

Lesson Nav

Lesson sections