Scratch 05 · Build a treasure hunt and keep score

A fox follows a trail to gems and a one-point score.

Scratch lesson 5 of 10 · Beginner to intermediate · About 35 minutes

Guide an explorer to treasure, earn one point for each catch and send the treasure to a new hiding place. Build a scoreboard that resets every time you start.

What you’ll make Use a shared Score variable and a touching check to award exactly one point per catch.

Get ready

Open the Scratch editor (opens in a new tab) and start a new project.

  • Open a new project. Choose and rename two small sprites Explorer and Treasure. A character and a simple object such as a gem, star or fruit work well. Leave a clear backdrop so you can see both sprites.
  • Select Variables and choose Make a Variable. Name it Score and select For all sprites. Keep the Score monitor visible on the Stage.
  • You will make one message called Set up hunt and another called Start hunt. Open the menu on a broadcast block and choose New message to create each one when you need it.

To keep your project, choose File → Save to your computer in Scratch.

Find the treasure. Check for a touch. → Add one point. Update the score. → Move the treasure. Set up the next find.
Project flow · Find the treasure → Add one point → Move the treasure

Build it in Scratch

Choose the sprite named above each stack. The colours help you find the block categories. Indented rows sit inside the block above. Follow the end marker to see where each loop or condition closes.

1. Reset the game on the Stage

Click the Stage thumbnail to build its script. Set Score to zero, then send Set up hunt using the block that ends in and wait. That lets both sprites finish taking their starting positions before the treasure starts looking for a catch.

Block stack for Stage
when green flag clicked
set [Score] to (0)
broadcast [Set up hunt] and wait
broadcast [Start hunt]

2. Set up Explorer and the arrow keys

Select Explorer and give it a starting point below the treasure. Make one short stack per arrow. Use x for left and right and y for up and down. Click the green flag and check all four before moving on.

Block stack for Explorer
when I receive [Set up hunt]
set size to (35) %
go to x: (0) y: (-100)
when [right arrow] key pressed
change x by (10)
when [left arrow] key pressed
change x by (-10)
when [up arrow] key pressed
change y by (10)
when [down arrow] key pressed
change y by (-10)

3. Give Treasure its first hiding place

Select Treasure. Begin its setup at x = 130, y = 70. These coordinates are comfortably inside the Stage. If the two sprites look unusually large, make both smaller before testing. Treasure needs room to jump clear.

Block stack for Treasure
when I receive [Set up hunt]
set size to (30) %
go to x: (130) y: (70)

4. Score a catch and move the treasure

Stay on Treasure. The blue Sensing block watches for contact with Explorer. The inner if then else uses a green greater-than operator with the blue x position reporter. If Treasure is on the right, it jumps left. Otherwise it jumps right. The large jump breaks contact so a catch only earns one point.

Block stack for Treasure
when I receive [Start hunt]
forever
if <touching [Explorer]?> then
change [Score] by (1)
if <(x position) > (0)> then
go to x: (-130) y: (70)
else
go to x: (130) y: (70)
↳ End of block
↳ End of block
↳ End of block

Test your project

  1. Click the green flag. Check that Score reads zero and Treasure starts on the right.
  2. Steer into Treasure. Score should turn to 1 and the treasure should jump left. Wait without moving and make sure the score stays at 1.
  3. Collect Treasure on the left. Score should turn to 2 and the treasure should return to the right.
  4. Click the green flag again while standing in a hiding place. Score should still reset to zero.

If something goes wrong

The score never changes.
Put the catch script on Treasure and select Explorer in the touching block. Verify the Stage sends Start hunt and the Treasure hat listens for that same message.
The score rises without a fresh catch.
Make sure both go to blocks sit inside the catch check, and set the hiding spots far apart. Shrink any sprite that reaches both spots.
The score monitor is missing.
In Variables, tick the box beside Score. Use one shared Score variable and check that both scripts select it.

Make it your own

  • Move the hiding places to opposite heights as well as opposite sides. Check that both locations are reachable and that each catch still adds exactly one point.
  • Change the game so Treasure says a short congratulation after the fifth catch. Write down which condition would detect Score = 5 before you add it.

What you’ve learned A variable remembers the score. A collision changes it, and moving the treasure makes the next point depend on a new catch.