Level 1Unit B · Session 10
10

Unit B · Telling a computer what to do

Keeping score

What we are making: a score that goes up, shows on the screen, and starts at zero every time. Almost every game needs this and nothing you have met so far can do it.

A variable is a box with a name

Your program can remember a number by putting it in a box and giving the box a name. The box is called a variable. You can look in it, you can put a new number in it, and it stays until you change it.

Call it score and you can ask ‘what is in score?’ at any point in the program, and change it from anywhere.

New words
VariableA named box holding one number. You choose the name.
set toPut this exact number in the box, throwing away what was there.
change byAdd this much to what is already in the box.
CheckboxTicking it next to the variable shows it on the stage.

Do it

  1. In the Variables section, click Make a Variable and call it score. Lower case, no spaces.
  2. Tick the checkbox beside it. The score appears in the corner of the stage.
  3. Build the program below and click the green flag.
  4. Click the sprite a few times and watch the number climb.
📷
images/l1-s10-make-variable.png
Screenshot: Make a Variable, the name score typed in, and the checkbox ticked beside it
A score that counts clicks
when green flag clicked
set score to 0
when this sprite clicked
change score by 1
📷
images/l1-s10-script-score.png
Screenshot: the two scripts side by side — set under the green flag, change under the click
💡 Tip
set and change are not the same block and mixing them up is the commonest mistake of the session. set score to 1 makes the score one, no matter what it was. change score by 1 adds one to it. Use set once at the start, and change every time something happens.

The line that everybody forgets

Take out set score to 0 and run it. It works. Play again and the score carries on from where it was, forever, because nobody ever emptied the box.

That one missing line is the single most common bug in every game ever made in this room. Every variable needs setting at the start. Put it in now and you will not spend session 15 hunting for it.

📷
images/l1-s10-stage-score.png
Screenshot: the stage with the score showing in the corner

Try it

  1. Make a second variable called lives, set it to 3 at the start, and make something take one away.
  2. Add an if that says Game over when lives reaches 0.
  3. Make the score go up by 10 instead of 1. Does anything else need changing?
What you seeWhyWhat to do
The score shows but never changesYou used set instead of change.set replaces, change adds. Swap the block.
The score starts where it finished last timeNo set-to-zero at the start.Add set score to 0 under the green flag. This is the big one.
I cannot see the score on the stageThe checkbox is unticked.Tick the box beside the variable name in the Variables list.
The score jumps up by lots at onceThe change block is inside a forever loop.It runs hundreds of times a second in there. Put it where the event happens.

What you learned

  • A variable is a named box your program can remember a number in.
  • set replaces what is in the box; change adds to it.
  • Every variable must be set at the start, or the second game is wrong.
  • You can now keep score, count lives, or time something.
Challenge optional — only if you finish early
  • Add a timer variable that counts down from 30 and stops the game at 0.
  • Show the best score so far, as well as the current one.
9. Moving it yourself