Project title:

Treasure Hunt with the Science Museum

What you will make:

A fun game where you need to find hidden objects from the Science Museum.

You can see the finished game here: https://scratch.mit.edu/projects/588542885.

What you will learn:

What you will need:

Instructions:

Getting started

Go to the Scratch website at https://scratch.mit.edu. or click the Scratch logo:

Scratch logo

You should sign in or join Scratch to be able to save your work and share it with others.

If you already have a Scratch account, click Sign In (at the top right):

Sign-In button

otherwise click Join Scratch (at the top right):

Join Scratch button

Go to the starter project at https://scratch.mit.edu/projects/581184007 and click Remix (at the top right):

Remix button

When the new project has opened (this takes a few seconds), click Share (at the top):

Share button

Overview

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 button, Help screen, and All Objects sprites for this.

Step 1 - Create the basic game

Click on Stage (at the bottom right):

Stage

Make a SCORE variable to keep track of the score:

Stage, make variable
Stage, new variable SCORE

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:

Stage, phase 1

Add this script to Stage to reset the variables when the game starts:

Stage, when start

Click it to test that it works. SCORE should show 0 and LEFT 20.

Add this script to Stage to remove one point from the score when the player makes a wrong guess:

Stage, when click

Click it to test that it works. SCORE should go down.

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:

Stage, when receive good

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. So click on this sprite.

Object

Create a variable which will be used to restore the objects to their original size:

Add a custom block to Object-Dress to animate it:

Object, animate custom block

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.

Add this script to Object-Dress to handle when the player clicks it:

Object, when click

Click it to test that it works. SCORE & LEFT should change, the animation should run and the sprite should disappear.

Add this script to Object-Dress to reset it when the game starts:

Object, when start

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:

  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:

Step 2 - Add the guide

Click the new sprite icon:

Choose sprite

Pick Marian from the People category or somebody else. Move the spite until the feet are at the bottom of the stage.

Under the Sounds tab, click the new sound icon:

Choose sound

and choose Mystery from the Loops category. Repeat and choose Triumph from the same category.

Add this script to Marian to display the instructions at the start of the game:

Guide

Click it to test that it works. The guide should pop up and disappear after the music is over.

Guide at start

Add this script to Marian to signal the end of the game:

Guide

Click it to test that it works. The guide should pop up and stay on the stage after the music is over.

Guide at end

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

Step 3 - Add the Cheat button

Add the following scripts to Cheat button.

Cheat button

Add a custom block to Object-Dress to flash it:

Object, flash custom block

Click it to test that it works.

Add this script to Object-Dress to handle when the flash message is received:

Object, when flash

Click it to test that it works.

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.

Step 4 - Add the Help button

Add the following scripts to Help button sprite.

Help button

Make a help visible? variable for Help screen only:

Stage, make variable
Stage, new variable LEFT

Add these custom blocks to Help screen:

Help screen, custom blocks

Add these scripts to Help screen:

Help screen, scripts

Test by clicking Help button or pressing the C key:

Help screen, issues

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:

Stage, help

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

Stage, show fix

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.

Make an id variable to differentiate the clones from each other:

All Objects, make variable
All Objects, new variable id

Add a custom block to All Objects:

All Objects, custom blocks

Add these scripts to All Objects:

All Objects, scripts

Test by clicking Help button or pressing the H key.

Help screen, final

Clicking on an object should display its description.

What next?

Click the boxes to expand them.

Watch videos by Surfing Scratcher and improve your Scratch skills.

And plenty more on his YouTube channel.

Watch videos by Scratch Team and make fun projects.

And plenty more on their YouTube channel.

Discover the power of clones.

You already met Scratch clones in step 4 of this activity. There we created 20 clones of All Objects to display all the objects in the help screen.

A clone is a temporary copy of a sprite created by create clone of (). Each clone has its own attributes like position, direction, size, color, and pen. Variables can also be used to differentiate multiple clones of the same sprite (like id in All Objects). All the clones are deleted automatically when the project is stopped or restarted. A clone can also delete itself with delete this clone. When a clone is created it will execute the code under when I start as a clone.

Clones are for example used to create projectiles or enemies in shooting games, to create a school of fish in underwater games, for special effects, etc.

Here are a few resources to find out more about clones:

Idea #1 - Change some of the animations when the objects are clicked.

Choose an object and modify the code under define animate. For example you could:

  • make the tricycle move using glide () secs to x: () y: ()
  • make the propeller spin using turn left () degrees and/or turn right () degrees
  • make the dress change color using change [color] effect by ()
  • make the television fade out using change [ghost] effect by ()
  • make the phones ring or the radio play music using play sound ()
  • make the microphone say something like "Test 1 2 3" using speak () (part of the Text to Speech extension)

Don't be shy! Use your imagination! 🤪

Idea #2 - Make Marian say something else when Cheat button was used.

Currently Marian always says "Well done. You made it!" at the end of the game. Make her say something like "Well done. You made it! But you cheated..." if the player used Cheat button.

Hint: You will probably need a new CHEAT CLICKED? variable (initialized to "no" at the start of the game).

Idea #3 - Make the objects go to different positions.

Currently the game is a bit boring because the objects are always in the same positions.

One way to improve this is to make each object go to a random position using go to (random position) when the game starts. However this has the disadvantage that the objects could overlap each other or be too easy to see against the backdrop.

Challenge: Can you think of a better solution?

Idea #4 - Use clones to replace the 20 object sprites.

Currently we have separate sprites for the objects to find and we had to copy all the code from Object-Dress into the 19 other sprites. All this copying is very tedious and is bad practice in general. For example think about how slow it would be to modify define animate for the 20 sprites.

A better option is have a sprite with one costume per object and clone it. This sounds like All Objects and we could modify its code but it will be simpler to duplicate it instead. Duplication is bad in general but we have to be careful not to break the All Objects code arleady in place for the help screen. At a high level we need to:

  1. Duplicate All Objects
  2. Remove all the code from the new sprite
  3. Copy the code from Object-Dress into the new sprite
  4. Delete the 20 sprites (Object-Dress to Object-Ring)
  5. Make the new sprite create 20 clones at start up (each clone has a different id)
  6. When a clone starts:
    • change its costume
    • set its position, direction and size

One option to position each clone is to have an if <> then block for each object:

Naive option

but this means lots of code and will not scale up well. A better solution is to have lists with the position, the direction and the size of each object and the code becomes very simple:

Better option

Idea #5 - Modify the help screen to grey out the objects already found.

Currently the help screen displays all the objects the same. It would be much better if it would grey out the objects which have already been found.

Greying out a sprite can be done easily using set [ghost] effect to (). But how do you keep track of what objects have already been found?

Hint #1: You will probably need a new OBJECT IDS FOUND list which will be reset every time the game starts. When an object is clicked, its id will be added to the list.

Hint #2: It will be easier to do this task after idea #4 is complete.

Convert a Scratch project to JavaScript with Leopard.

Scratch is an awesome environment for beginners and amazing projects can be achieved with it but it has limitations. So you will probably want to move to a programming language like Python or JavaScript in the future. In these languages the code is described using text instead of blocks but most of the concepts stay the same.

Go to leopardjs.com, enter the URL of your Scratch project (which must be shared) and click the Edit button.

Leopard, automatic

After a few seconds you should see the JavaScript code. For example the code for Marian will look like this:

Leopard, code

The Manual page explains how each Scratch block gets translated to JavaScript:

Leopard, manual