Lesson Overview
In this Lesson, we will be adding collectible coins to our game. To begin, open up the following Template. Hover Mouse below to reveal template link
Step 1: Coin Collision
While adding in Gold Coins is easy enough, we will want to program them to “react” to the player. When the Player touches a Gold Coin, the Player should “collect” the coin.
Select the Gold Coin Sprite. Begin by getting a “When the Green Flag is Clicked” from Events. We will also need a Forever loop from Control.
Inside of the Forever loop, place a “If-Then” from the Control section. We will also need a “touching mouse-pointer?” from Sensing. Change “mouse-pointer” to be the Player, Sprite1.
If the Player comes into contact with the Coin, we want them to “collect it”. In order to track how many Coins we have collected, we will need to create a variable.
A variable is a changeable value recorded in Scratch’s memory. This allows us to track things like how many Coins we have picked up.
Go to “Variables” and click on the First option, Make a Variable. Name it “Coins‘” and make sure that the “For All Sprites” option is selected.
Now that the Coins variable has been created, we can incorporate it into the code. Inside of the If-Then, place the Change Coins by 1 block from Variables.
After the Coin is picked up, we will want it to Hide. Otherwise, we would be able to pick up the Coin more then once.
When our Game starts, we want to make sure that our Coins variable resets to Zero, and all of the Coins Show again
Step 2: Adding Additional Coins
Now that we have our Gold Coin coded, we can “Duplicate” it in order to create more. Right-Click on the Gold Coin Sprite, and select the “Duplicate” option. This will create a Copy of the Gold Coin. Drag and Drop the copy into a position inside of the Maze.
Continuing Duplicating until you are satisfied with the number of coins in your Maze. Make sure to properly distribute them, so that they are spread out across the Maze.