Skip to content

Coding project: Treasure Hunt

Make a fun game using objects from the Science Museum Group Collection


We’re working on new resources which will invite you to get creative with objects from the Science Museum Group Collection using Scratch. These resources are in beta mode and are currently being evaluated.

What you will make

A fun game where you need to find hidden objects.

You can see the finished game here:

https://scratch.mit.edu/projects/598952335/ 

What you will learn

  • How to copy a Scratch project and modify it (remixing)
  • How to use variables to keep track of the score, etc
  • How to detect when the mouse is clicked or when keys are pressed
  • How to create your own blocks
  • How to hide and show sprites, resize them, position them, etc
  • How to create buttons
  • How to broadcast and receive messages
  • How to use clones to avoid repetition

What you will need

  • A computer or a laptop with access to Internet
  • Scratch Developer Chrome extension (optional)

 

Getting started

  1. Go to the Scratch website or click the Scratch logo.
  2. Go to the starter project/ template at https://scratch.mit.edu/projects/581184007 and click Remix (at the top right).
  3. When the new project has opened (this takes a few seconds), click Share (at the top).

Building the game

The project we just copied already contains 24 sprites but (almost) no code yet.

We will develop the game in 4 steps:

  1. Create the basic game using the first 20 sprites (Object-Dress to Object-Ring).
  2. Add a guide to explain the game and tell when the game is over. We will add a new sprite for this.
  3. Add a button to make flash the objects not found yet. We will use the Cheat button sprite for this.
  4. Add a button to show a screen with all the objects to find. We will use the Help buttonHelp screen, and All Objects sprites for this.

Step 1 – Create the basic game

  1. Click on Stage (at the bottom right):
  2. Make a SCORE variable to keep track of the score:
  3. Make a LEFT variable to keep track of the number of remaining objects. Move SCORE to the bottom left corner and LEFT to the bottom right corner:
  4. Add this script to Stage to reset the variables when the game starts, and click it to test that it works. SCORE should show 0 and LEFT 20.
  5. Add this script to Stage to remove one point from the score when the player makes a wrong guess, and click it to test that it works. SCORE should go down.
  6. Add this script to Stage to add one point to the score and remove one from the number of remaining objects when the player makes a good guess, and click it to test that it works. SCORE should go up and LEFT should go down.

Now let’s add some code to Object-Dress.

  1. Click on this sprite.
  2. Create a variable which will be used to restore the objects to their original size:

    • Go to the Variables blocks (orange).
    • Click Make a Variable, name it ORIGINAL SIZE and untick it to stop showing it on the stage.
  3. Add a custom block to Object-Dress to animate it, and click it to test that it works. There should be a pop sound and the sprite should grow bigger then return to its original size.
  4. Add this script to Object-Dress to handle when the player clicks it, and click it to test that it works. SCORE & LEFT should change, the animation should run and the sprite should disappear.
  5. Add this script to Object-Dress to reset it when the game starts, and click it to test that it works. The sprite should become visible again.

Test the game:

  1. Click the green flag – Object-Dress should be visible, SCORE should be 0 and LEFT should be 20
  2. Click the dress – Object-Dress should disappear and SCORE & LEFT should change
  3. Click another object – nothing should happen
  4. Click the background – SCORE should go down by 1
  5. Click the green flag – Object-Dress should become visible again

Nothing happens when the other objects are clicked because they have no code yet. Let’s fix that by copying the code from Object-Dress to each of the other objects (from Object-Tricycle to Object-Ring):

  1. Drag the define animate block from Object-Dress and drop it on each of the other sprites.
  2. Drag the when flag clicked block from Object-Dress and drop it on each of the other sprites.
  3. Drag the when this sprite clicked block from Object-Dress and drop it on each of the other sprites.

Test the game again:

  1. Click the green flag – all the objects should be visible, SCORE should be 0 and LEFT should be 20
  2. Click an object – it should disappear and SCORE & LEFT should change
  3. Repeat step 2 until there are no more objects – when done LEFT should be 0
  4. Click the background – SCORE should go down by 1
  5. Click the green flag – all the objects should become visible again, SCORE should be 0 and LEFT should be 20

If an object doesn’t behave as expected, check that it has the same code as the other objects.

Now that the basic game works we will improve it little by little in the next sections. We will add:

  • guide character to explain what the game is about and when the game is over;
  • Cheat button to make the remaining objects flash; and
  • Help button to present all the objects and their descriptions.

Back to top

 


Step 2 – Add the guide

  1. Click the new sprite icon. Pick Marian from the People category or somebody else. Move the spite until the feet are at the bottom of the stage.
  2. Under the Sounds tab, click the new sound icon and choose Mystery from the Loops category. Repeat and choose Triumph from the same category.
  3. Add this script to Marian to display the instructions at the start of the game.
  4. Click it to test that it works. The guide should pop up and disappear after the music is over.
  5. Add this script to Marian to signal the end of the game.
  6. Click it to test that it works. The guide should pop up and stay on the stage after the music is over.

Test the game:

  1. Click the green flag – the guide should appear then disappear
  2. Click all the object
  3. The guide should appear when the game is over

Back to top


Step 3 – Add the Cheat button

  1. Add the following scripts to Cheat button.
  2. Add a custom block to Object-Dress to flash it, and click it to test that it works.
  3. Add this script to Object-Dress to handle when the flash message is received, and click it to test that it works.

Testing and fixing

Test that Object-Dress flashes when Cheat button is clicked or the C key is pressed.

The other objects are not flashing because they have no code yet. Let’s fix that by copying the code we just added from Object-Dress to each of the other objects (from Object-Tricycle to Object-Ring):

  1. Drag the define flash block from Object-Dress and drop it on each of the other sprites.
  2. Drag the when I receive (Flash objects) block from Object-Dress and drop it on each of the other sprites.

Test that all the objects flash when Cheat button is clicked or the C key is pressed.

Back to top


Step 4 – Add the Help button

  1. Add the following scripts to Help button sprite.
  2. Make a help visible? variable for Help screen only.
  3. Add these custom blocks to Help screen.
  4. Add these scripts to Help screen.
  5. Test by clicking Help button or pressing the C key.

Back to top


Fixing bugs

There are programming mistakes (bugs) here:

 

  1. SCORE & LEFT should not be visible when the help screen is up.
  2. The objects to be clicked should not be visible.
  3. The help screen does not list the objects.

Let’s fix issue #1 by adding these scripts to Stage.

Let’s fix issue #2 by adding a go to [front] layer block at the end of the define show block in Help screen.

To fix issue #3 we are going to use All Objects. It contains 20 costumes, one per object to find. We are going to create 20 clones. Each clone will show a different costume and we will arrange the clones in a grid with 4 rows and 5 columns. Each clone will also have its own id variable which will contain a number between 1 and 20. This variable will be used to select the right costume and also to display the right description when the clone is clicked. All Objects already contains a descriptions list with a description for each object.

  1. Make an id variable to differentiate the clones from each other.
  2. Add a custom block to All Objects.
  3. Add these scripts to All Objects.
  4. Test by clicking Help button or pressing the H key. Clicking on an object should display its description.

 

Back to top


What next?

Click the boxes to expand them.