Week 4 of 6 ยท Lesson for Students

Variables & Data

Today you will learn about variables โ€” the labelled boxes that store changing data. You will add a score counter and a lives system to your game.

1
2
3
โœ“
4
5
6
โœ“
7
๐Ÿ†
1
Create the "score" variable
Variables live in the orange "Variables" section of the palette
  1. In the Block Palette, click the orange "Variables" section.
  2. Click the "Make a Variable" button at the top.
  3. In the box that appears, type the name: score (all lowercase).
  4. Make sure "For all sprites" is selected โ€” this means every sprite can use it.
  5. Click OK.
  6. You will see new blocks appear: set [score] to 0, change [score] by 1, and others.
  7. A score display should also appear in the top-left corner of the stage automatically.
๐Ÿ‘€ What you should see

The Variables section now has several orange blocks with "score" in them. On the stage, a small display box shows "score: 0".

โš ๏ธ Watch out!
  • If you choose "For this sprite only", only your player sprite can use the variable โ€” other sprites won't be able to read or change it. For score, always choose "For all sprites".
  • Variable names are case-sensitive in some places. Use lowercase "score" consistently.
  • If you don't see the score display on the stage, look for a small checkbox next to the "score" variable in the palette. Make sure it is ticked.
2
Set score to 0 when the game starts
Always initialise variables โ€” otherwise old values carry over
  1. Click your player sprite in the Sprite Pane.
  2. Find or create a when ๐Ÿšฉ clicked block in the Scripts Area.
  3. Snap set [score] to 0 below it (this goes at the very top of your startup script).
  4. Click the green flag. The score display should now show 0 every time you start.
  5. Click the flag a second time โ€” score should still be 0, not carrying a value over from before.
โš ๏ธ Watch out!
  • This is one of the most important habits in programming. If you skip this step, the second time you play the game the score will start at whatever it ended on last time โ€” which makes no sense to the player.
  • The set [score] to 0 block must be under your flag block, not floating by itself. A floating block never runs.
3
Add a collectible coin sprite
The player gains a point by touching the coin
  1. Add a new sprite to act as a coin or collectible โ€” use the library (choose Ball, Star, or any small object).
  2. Place it somewhere accessible on your stage.
  3. Click on the coin sprite to select it.
  4. Add this script to the coin sprite:
    when ๐Ÿšฉ clicked โ†’ forever
    Inside the forever: if touching [Player sprite]? โ†’ change [score] by 1 โ†’ go to x: [random] y: [random]
  5. For the "go to" block, use a Motion block with random x and y values (e.g. go to x: (pick random -200 to 200) y: (pick random -150 to 150)).
  6. Test: collect the coin and watch the score go up!
โš ๏ธ Watch out!
  • If the score goes up very fast when touching the coin, that is because the forever loop runs many times per second. Add wait 0.5 seconds after the score change to slow it down.
  • Make sure the change [score] by 1 block is on the coin sprite, not the player sprite. The coin is what detects being touched and awards the point.
  • If the coin teleports off-screen, the random range for x might be too large. The stage goes from about -240 to 240 for x and -180 to 180 for y.
๐Ÿ” Check Your Progress โ€” After Step 3
  • The score display on the stage starts at 0 each time the flag is clicked
  • Touching the coin increases the score
  • After being collected, the coin moves to a new position
๐Ÿ†˜ Something looks wrong?
  • Score doesn't reset to 0: Find your flag-clicked script on the player sprite. Make sure set [score] to 0 is connected directly below the flag, as the very first block.
  • Score jumps by lots immediately: Add wait 0.5 seconds inside the coin's if block, after changing the score.
  • I can't find "pick random" block: It's in the green "Operators" section of the palette. It looks like: pick random 1 to 10. Drag it into the x slot of the "go to" block.
4
Create the "lives" variable
The player starts with 3 lives โ€” losing all of them ends the game
  1. In the Variables section, click "Make a Variable" again.
  2. Name it: lives โ€” make sure "For all sprites" is selected.
  3. Click OK. You should now see "lives" blocks in the palette.
  4. On your player sprite's flag-clicked script, add set [lives] to 3 below the "set score to 0" block.
๐Ÿ‘€ What you should see

The stage now shows both "score: 0" and "lives: 3" when the flag is clicked.

โš ๏ธ Watch out!

The two set blocks must be in your flag-clicked script โ€” not floating by themselves. Floating blocks are never run automatically.

5
Lose a life when hit by the enemy
Decrement lives by 1 on each enemy collision
  1. Click your player sprite. Find your collision-detection forever loop.
  2. Find the if block that handles enemy touching.
  3. Remove the broadcast game-over (we are replacing it with something smarter).
  4. Instead, add: change [lives] by -1 (note the minus 1).
  5. Send the player back to the start: go to x: [start x] y: [start y].
  6. Test: walk into the enemy โ€” a life should disappear and the player resets.
โš ๏ธ Watch out!
  • The number in "change lives by" must be -1 (negative one), not 1. Click the number and type -1.
  • Lives can go below zero if you do not check for it โ€” that is what the next step fixes.
  • If lives decreases too quickly (multiple times per second while overlapping), add wait 1 second after the change block so there is a grace period.
6
End the game when lives reach zero
Use an if block to check lives and broadcast "game-over"
  1. Below your change [lives] by -1 block, add an if <> then block.
  2. From Operators (green section), find ( ) = ( ). Put it in the if's hexagonal slot.
  3. Drag the lives variable (the oval block from Variables) into the left side of the = block.
  4. Type 0 in the right side of the = block.
  5. Inside the if: add broadcast [game-over] and stop [this script].
  6. Test: lose all 3 lives by walking into the enemy three times. Game over should trigger on the third hit.
๐Ÿ‘€ What you should see

The script inside the if block reads: if (lives) = 0, then broadcast [game-over], stop [this script]. Lives counts down from 3 โ†’ 2 โ†’ 1 โ†’ 0, then game over fires.

โš ๏ธ Watch out!
  • The lives oval goes in the left slot of the = block โ€” not typed as text. Drag the orange lives oval from the Variables palette into it.
  • If game over fires immediately, check that your flag-clicked script correctly sets lives to 3. Press the flag and watch the lives display update.
  • If the game-over message shows but you can still move, that is expected โ€” full state control comes in Week 6.
๐Ÿ” Check Your Progress โ€” After Step 6
  • Pressing the green flag sets score to 0 and lives to 3
  • Collecting a coin increases the score by 1
  • Touching the enemy decreases lives by 1 and resets position
  • When lives reach 0, the "game-over" broadcast fires
๐Ÿ†˜ Something looks wrong?
  • Lives goes negative (below 0): Your if (lives = 0) check might be in the wrong place. It should be after the change-lives-by-1 block โ€” so it checks after deducting, not before.
  • Score variable not visible on stage: In the Variables section of the palette, look for a tiny checkbox next to "score". Make sure it is ticked.
  • I accidentally set "lives" to "for this sprite only": Delete the variable (right-click its block โ†’ "Delete the [lives] variable") and create it again with "For all sprites".
7
Tidy up and customise
Position the variable displays and try a win condition
  1. Drag the score and lives displays on the stage to a corner where they don't block the gameplay.
  2. Add a win condition: if score reaches a target (e.g. 5), broadcast "you-win". Use an if block with (score) = 5 inside the coin's forever loop.
  3. Add a "when I receive [you-win]" script somewhere that shows a winning message.
  4. Save your project as "Week 4 โ€” Score and Lives".
โš ๏ธ Watch out!
  • You cannot drag the variable displays while the game is running. Stop it first with the red stop button, then drag them.
  • If you want to hide a variable display (e.g. during a "game over" screen), use hide variable [score] in your game-over script.
๐Ÿ“Š

Superb โ€” Week 4 complete!

You have added variables for score and lives, initialised them properly, and built a working lives system. Next week you will use lists and cloning to create falling objects.