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 that 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 Variable menu.
Then, click the make a variable button.
Give the variable a name that makes sense, like score.
Click okay. When you do this, more orange blocks show up in the Variable 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 one.
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 drop down 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 statement over and over again.
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 is because if the 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 one to the score, and 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 one 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 one 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've 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 one to negative one.
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. It does.
Finally, the score should reset to zero at the start of every game.
From the Variable menu, place a set score to block under the when flag clicked block and make sure the score resets to zero.
Test this by playing the game until you score.
Stop playing, then click the green flag to reset the game.
The score should reset to zero.
Awesome. Now it's your turn.
Create a variable called score.
Add to the score when the blue star touches the blue android by 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 zero using the when 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.