Treasure Hunt with the Science Museum
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.
Go to the Scratch website at https://scratch.mit.edu. or click the 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):
otherwise click Join Scratch (at the top right):
Go to the starter project at https://scratch.mit.edu/projects/581184007 and click Remix (at the top right):
When the new project has opened (this takes a few seconds), click Share (at the top):
The project we just copied already contains 24 sprites but (almost) no code yet.
We will develop the game in 4 steps:
Click on Stage (at the bottom right):
Make a SCORE variable to keep track of the 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:
Add this script to Stage to reset the variables when the game starts:
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:
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:
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.
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:
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:
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:
Click it to test that it works. The sprite should become visible again.
Test the game:
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):
Test the game:
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:
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.
Under the Sounds tab, click the new sound icon:
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:
Click it to test that it works. The guide should pop up and disappear after the music is over.
Add this script to Marian to signal the end of the game:
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:
Add the following scripts to Cheat button.
Add a custom block to Object-Dress to flash it:
Click it to test that it works.
Add this script to Object-Dress to handle when the flash message is received:
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):
Test that all the objects flash when Cheat button is clicked or the C key is pressed.
Add the following scripts to Help button sprite.
Make a help visible? variable for Help screen only:
Add these custom blocks to Help screen:
Add these scripts to Help screen:
Test by clicking Help button or pressing the C key:
There are programming mistakes (bugs) here:
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.
Make an id variable to differentiate the clones from each other:
Add a custom block to All Objects:
Add these scripts to All Objects:
Test by clicking Help button or pressing the H key.
Clicking on an object should display its description.
Click the boxes to expand them.
Watch videos by Surfing Scratcher and improve your Scratch skills.
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:
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:
One option to position each clone is to have an if <> then block for each object:
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:
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.
After a few seconds you should see the JavaScript code. For example the code for Marian will look like this:
The Manual page explains how each Scratch block gets translated to JavaScript: