In this lesson, Arun and Raj will be discussing how to create your own variable and control it in Scratch.
Arun: Let’s start. To create a new variable, click on the Make a Variable button in the Variables menu.
Raj: A Make a Variable dialogue box appears.
Arun: Write the name of your variable.
Raj: I will write “score”.
Arun: Click on OK. You will see your variable appears in the Variables menu.
Raj: What do these blocks do?
Arun: To show your variable on the screen, tick the checkbox in front of the score
block.
Raj: Or you can use another block to show your score?
Arun: Yes, you can use the show variable [variable]
block. This will show the score without ticking the checkbox.
Raj: How?
Arun: Drag out this block into the starfish’s scripting area and select your variable “score” from the drop-down list.
Raj: On clicking, the starfish’s score appears on the screen.
Arun: Similarly, to hide this, use the hide variable [variable]
block and select your variable “score” from the drop-down list. Click on it.
Raj: The starfish’s score is removed again.
Arun: Correct! Now let’s move on to the set [variable] to [value]
block.
Raj: What is the use of this block?
Arun: Let’s see by dragging out this block into the scripting area of the starfish and selecting your variable “score” from the drop-down list. Set it to a specific value and click on it.
Raj: The value is set to the particular value given.
Arun: To gradually increase the score, use the change [variable] by [value]
block and select your variable “score” from the drop-down list. Click on it.
Raj: Every time I click, the score increases by the given value.
Arun: To understand more properly, let’s create a game.
Raj: Which game?
Arun: Drag out these blocks and snap them together. Copy the code to both the sprites.
Raj: The score is at 0.
Arun: Yes, now click on the green flag.
Raj: Whenever the sprites touch each other, the value of the score increases.
Arun: These are the Variable blocks; every variable you create will have these four controlling blocks.
Raj: What are the next blocks?
Arun: There are also some List blocks, which we will be discussing next.