Scratch is one of the most popular programming platforms among the kids that allows linking their creativity with logical thinking to craft projects like games, animations, and interactive stories.
Assuming you have previously explored scratch and are familiar with its basic elements and concepts, I will proceed with the blog.
Say you’ve spent a lot of time designing and coding your dream game from scratch, but you’re stuck at a point where you’re not sure how to keep track of your main character’s score.
So what are you going to do?
Well, I have the answers and it’s a lot simpler than you think.
How To Make A Score Function In Scratch
Before we dive in to create the score, let’s understand the concept of the variables.
- What are variables?
Why do we need to know about them to create a score?
A variable is a container that is used to store a certain type of data. The value that you store in a variable can be varied i.e. it can be changed, hence the name variable.
Consider an example below to learn more about variables.
Imagine a situation where your mother asks you to go grocery shopping. When you return, you see that your mother has kept three separate containers on the kitchen countertop.
These containers have different labels on them – Vegetables, Fruits, and Drinks.
Then she asks you to segregate the grocery items based on the labels on these containers. This makes it easier for her to use the items.
She can also remove or add similar items to the containers as needed.
The variables are just like these containers; variables hold the data and this data can be changed or used according to your requirement.
The labels on the containers are analogous to the variable names.
For more information on variables, you can refer to Transcode’s video.
The score is considered variable since its value changes according to the conditions
or the rules of the game.
How to create a score variable?
To create the score variable in scratch follow the steps below.
Step 1: Select the “Variables” block from the “Block Palette” of the scratch user interface. Then click on “Make a variable”.
Step 2: Type score as a variable name in the New variable name section and then click
ok.
Tip: Naming the variable accurately and descriptively is recommended so that it is easy to understand what type of information it will be storing.
Note: Select For all sprites if you want to create
Global variable i.e. it can be accessed/modified from any sprite or background in your project whereas if you select For this sprite only, the variable becomes local which means you can change/modify it from the sprite on which you created it.
Tip: Select the score, to display it on your game.
Different variable blocks can be used throughout the code scripts:
set variable to 0: It is used to set a particular value to a variable (here, 0).
change a variable by 1: This block changes the value of a variable by any value that you provide (here, 1).
show variable: Using this block a variable can be displayed whenever required.
hide variable: With this block, one can hide a variable.
It is recommended to set the score to 0 when the new game begins. To accomplish this add set my variable to 0 at the start of the script.
- Script Area Stage
Depending on the condition, the score can be increased or decreased using a change variable by 1 block.
For example, if we want that whenever our sprite touches green color the score should increase by 10 and it should decrease by 5 when it touches red color.
The script would look like the one should below:
Tip: Using forever here is important to keep checking the conditions again and again!
Conclusion
Congratulations! You have just learned an easy way to create a score variable for your game.
Just hit the Variables block from the block palette; make a variable named score and you are good to go. Now you can keep track of the player’s score in your game.
I can’t wait to see your amazing creations based on this lesson. Have fun creating!
Did you find this article helpful? Don’t forget to share it with your friends and family.
Learn Scratch at BrightChamps with its specially designed curriculum that makes learning programming easy for students in Grade 1-12.
BrightChamps also teaches a variety of other programs that help kids build a foundation in computer programming through activities, interactive lessons, and other means.
Frequently Asked Questions (FAQs)
Variables are used to keep track of information or data. Variable values can be changed at any time. For example, we can create a score variable to keep track of a player’s score, or we can add a time limit to our game by creating a timer variable.
The best and easiest method to keep track of the score in Scratch is using variables.