Unit B · Telling a computer what to do
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.
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.
score. Lower case, no spaces.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.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.
lives, set it to 3 at the start, and make something take one away.Game over when lives reaches 0.| What you see | Why | What to do |
|---|---|---|
| The score shows but never changes | You used set instead of change. | set replaces, change adds. Swap the block. |
| The score starts where it finished last time | No 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 stage | The checkbox is unticked. | Tick the box beside the variable name in the Variables list. |
| The score jumps up by lots at once | The change block is inside a forever loop. | It runs hundreds of times a second in there. Put it where the event happens. |
In this session