This video will show you how to keep score.
You will make the score advance by one if you or your friend catch an object that is the same color as your sprite, and you'll lose points if you catch one that is a different color.
“Score” is a variable.
Variables in computer science store information that can change.
In this case, the variable is the score you and your friend have earned by catching the falling objects.
Start by clicking on the blue star.
To make a variable in Scratch, click on the data menu, then click the “make a variable” button.
Give the variable a name that makes sense, like “score.”
When you do this, more orange blocks show up in the data menu.
Make sure the checkbox next to “score” is clicked, so you and your friend can see the score during the game.
To change the score, use the “change by” block.
Click on it to watch the score go up by 1.
Drag it to the scripts area.
The score should go up if the blue star touches the blue Android.
From the “control” menu, place an “if” block around the “change by” block.
Then, from the sensing menu drag out a “touching” condition.
Click on the dropdown, and select the “blue Android” sprite.
The blue star should increase the score every time it touches the blue Android, so the program should keep checking the “if” over and over.
Put this "if" statement into the forever loop.
Test the code by clicking the green flag and moving the blue Android to catch the blue star.
The score goes up… but wait, it keeps going up and up!
That’s because the if block is run each time the program goes through the loop - the object goes down, checks if it is touching the character, if it is, it adds 1 to the score, then goes back to the top of the loop, moves down, checks if it is touching the blue Android, and since it still is, changes the score by 1 again.
To fix this, delete the clone once it touches the sprite and after the score advances by one.
From the “control” menu add another “delete this clone” after the “change score by 1” block.
Next, program what happens when the blue star touches the yellow Android.
The code for this will be a lot like the code you just created to advance the score, except touching the yellow sprite should subtract one from the score, instead of adding one to it.
Right click on the “if” block, and select “duplicate.”
Place the copied code under the original inside the forever loop.
Change “blue android” to “yellow android,” and change the value in “change score by” to negative 1.
Test the code by clicking the green flag and moving the yellow android to catch the blue star.
The score should go down by one.
Finally, the score should reset to 0 at the start of every game.
From the data menu, place a “set score to” block under the “when flag clicked” block, and make sure the score resets to 0.
Test this by playing the game until you score.
Stop playing, then click the green flag to restart the game.
The score should reset to 0.
Now it’s your turn: Create a variable called “score” Add to the score when the blue star touches the the blue Android using “if/then,” “change score by,” and “touching” blocks.
Make the score go down when the blue star touches the yellow Android by duplicating the previous "if" block, and changing the sprite name and amount to change the score by.
Reset the score to 0 when the green flag is clicked using a "set score" block.
- Create a variable called "score."
- Add to the score when the blue star touches the the blue android using "if/then," "change score by," and "touching" blocks.
- Make the score go down when the blue star touches the yellow android by duplicating the previous "if" block, changing the sprite name and changing the amount in the "change score by" block.
- Reset the score to 0 when the green flag is clicked using a "set score to" block.