Adding a Score Variable
In this lesson you will be adding a score variable to the game. The score will begin at 0 and end with 5. This gives 1 point for each successful jump the cat makes.
When the cat earns 5 points, it says, “Yahoo!”
The cat, penguin and rock disappear.
The background changes and you are now in Level 2 of the game.
Level 2 is different from Level 1. You will build Level 2 in Lesson 6.
BE SURE TO SAVE YOUR WORK AS YOU COMPLETE EACH STEP!
Step 1: Create a score variable.
Click on Variables.
Click "Make a Variable".
Name it "Score" and select "For all sprites".
Step 2. Set the Score to 0.
Click the rock sprint.
Look at the blocks added to the coding that controls the rock sprite
Find the code blocks that will add a score of 5.
Replicate the code shown in the image below.
Notice there is a “hide” block. By adding this block, the rock sprite will hide, or disappear, when the score = 5. This is necessary so the rock does not show when moving to Level 2.
Step 3: Copying the code.
What else needs to hide, or disappear, when the score = 5 and a new game starts? Yes, the penguin. There is an easy way to do this.
Click on the rock.
Find the code that shows the score.
Drag the code to the penguin sprite. You will notice that the sprite darkens a bit.
Drop the code onto the penguin by letting go of your mouse or touchpad.
Click on the penguin. Check to make sure the code transferred. If not, try to drag the code again to the penguin sprite.
Step 4: Code a new action: Moving to Level 2
Click the Cat Sprite.
Take a look at the new code you will add to the cat spirte. You will need to use control, sensing, and looks blocks.
You also will need a new backdrop from Level 2. Choose the Nebula backdrop.
Add the new code. Make sure the values are correct. You will need to hide the cat because you will use a net spite for Level 2.
On the dropdown menu for “switch backdrop to,” choose “Nebula.”
You no longer “win” the game when the cat touches the rock. You simply move to the next level. So, you will need to change what the cat says when it now touches the rock. Notice in the coding that “I win” was changed to “Yahoo!” You can add “Yahoo!” or another word or words here .
Play the game to test your code. Everything should work. When the cat touches the penguin, there is a 1 second wait. Then the cat, rock, and penguin disappear and the backdrop changes to Nebula.
Step 5: Adding sound
Your Level 1 needs some sound. Here’s how to add sound ONLY to Level 1.
Look for the sound tab. It is found over the code blocks.
Click Sounds. The cat sound opens. If you click the play button, the cat sound will play.
Click the sound icon in the lower left hand corner of the screen. Lots of sound options appear.
Click on Loops.
Choose a sound you think works for Level 1. Remember the rock moves slowly so consider that when you choose your sound. “Loop” means the sound plays over and over again unless the coding tells it to stop.
Step 4: Code a new action: Moving to Level 2
Click the Cat Sprite.
Take a look at the new code you will add to the cat spirte. You will need to use control, sensing, and looks blocks.
You also will need a new backdrop from Level 2. Choose the Nebula backdrop.
Add the new code. Make sure the values are correct. You will need to hide the cat because you will use a net spite for Level 2.
On the dropdown menu for “switch backdrop to,” choose “Nebula.”
You no longer “win” the game when the cat touches the rock. You simply move to the next level. So, you will need to change what the cat says when it now touches the rock. Notice in the coding that “I win” was changed to “Yahoo!” You can add “Yahoo!” or another word or words here .
Play the game to test your code. Everything should work. When the cat touches the penguin, there is a 1 second wait. Then the cat, rock, and penguin disappear and the backdrop changes to Nebula.
GOOD JOB!
You now have set up your game to move to Level 2. In Lesson 6 you will build the second level of your game and choose a new sound to play.
What if you want a different sound for Level 1.
Where will you find that sound?
How will you change the code for the cat spite so the new sound changes?
Now let’s do one more fun coding change.
Bonus Step: Changing the Costume
Most sprites come with costumes. The costumes create animation, or movement. Let’s add animation to the cat sprite.
Now click Costumes tab above the code blocks.
Notice that the cat has two costumes.
Costume 1 shows the cat running.
Costume 2 shows the cat jumping.
Now click the Code tab. Then click the cat sprite to make sure you are coding the correct sprite.
Looks: Add the “switch costume to” block after each “when up (down) arrow key pressed” as shown.
Choose Costume 2 for the up arrow.
Choose Costume 1 for the down arrow.
Test your code by playing the game.
Well done! You have finished the first level of your game.
Wrap Up
With this lesson you learned:
how to add the score variable,
how add sound,
how to change costumes to add animation to your spite.