Decisions and Variables in Scratch
In the first lesson, you wrote scripts to move the cat sprite using keys on the keyboard. In the last lesson, you learned to use loops and add music to a project. In this lesson, you will learn to make decisions and use random numbers and variables. Then you will put all those pieces together to create a game!
Making a Decision
Sometimes a sprite reaches the edge of the screen when it is moving around. When this happens, you might want it to turn and go the other way. You can do this by using the if then block to make a decision. This block is found on the Control menu. What do you notice about this block?
The if then block has a space in the middle that can hold other Scratch blocks. In addition, the if then block contains a hexagonal space. This space requires a Boolean block. A Boolean block is a block that can be either true or false. You can find some Boolean blocks in the Sensing menu. You will start by using the touching? block. Notice that this block matches the shape of the space in the if then block.
Drag a touching? block into the Scripts area. Don’t connect it to any other block; just place it away from the rest of your script. Click on the black triangle in the touching? block and select “edge.” Now click on the touching? block. The value of the block (true or false) will appear. If the sprite is not touching the edge, the value should be false. Next, drag the sprite so that it is touching the edge. Now click on the touching? block again. Is its value true?
Drag an if then block into the Scripts area. Don’t connect it to any other block; just place it away from the rest of your script. Drag the touching? block into the if then block.
When a script reaches an if then block, it checks the value of the Boolean block inside it. If the value is true, then the script runs the blocks inside the if then block. If the value of the Boolean block is false, then the script skips the blocks inside the if then and moves on to whatever is below the if then block.
You want the sprite to turn around when it is touching the edge. You can do this by adding a turn block inside your if then block, as shown below.
You have a very useful if then block. You can decide whether the sprite is touching the edge of the screen and then turn around. However, when you run this script, the decision will only be made once. You actually want to make this decision over and over again as you move the sprite around. In other words, you want to repeat this decision.
Exercise: Place your if then block inside a forever loop. Then drag the sprite around the screen. What happens when it is touching an edge?
Exercise: Place a move block inside your forever loop. It should not be inside the if then block. Before you run this program, make a prediction about what will happen. Then test your prediction!
Using Random Numbers
For the rest of this lesson, you will be working on a game. Start by opening a new project and choosing two sprites. In your game, you will have one sprite move randomly while you try to catch it with the other sprite. We will call the first sprite the moving sprite and the second sprite the chasing sprite.
Now you will write a script for your moving sprite. The script for the moving sprite should start with the when flag clicked block. Next, the sprite should move a random amount. You know how to make the sprite move, but how can you make the amount random? To do this, you will use a block on the Operators menu. This block is called the pick random block.
Drag this block into your program. Can you connect it to the other blocks?
The pick random block cannot be used on its own. It has to be placed within another block. For example, you can replace the number in the move block with the pick random block.
The pick random block contains two numbers. This block will chose a random number somewhere between those two numbers. How does changing these numbers affect how your sprite moves?
Exercise: Place your move block within a loop to make the sprite move until you click the stop sign to stop the program. Remember to make your sprite turn around when it hits the side of the screen.
Exercise: Right now, your sprite probably moves back and forth along a line. How can you use another pick random block to solve this problem? When you have finished this script, the moving sprite should move randomly all around the screen.
Exercise: Write scripts for the chasing sprite that will enable you to move it around using keys on the keyboard.
Often, you want to keep score in a game. To do this, you need a variable. A variable is just a name that represents a value. You will now create a variable named “score” that will hold the number of times that the chasing sprite has caught the moving sprite.
First, go to the Data menu. Then click Make a Variable.
The New Variable window will appear. Call the variable “score” and click OK.
You will notice that the Data menu looks different now. It contains some blocks that you can use to change the value of your variable. Also, the value of the variable is shown in the top-left corner of the stage.
Before you use a variable in a program, you should always give it a value. This is called initializing the variable. To initialize a variable, use the set to block. Place this block just below the when flag clicked block to set score to zero at the beginning of the program. To increase the variable, you can use the change by block.
Exercise: Create a when flag clicked script for the chasing sprite. Add an if then block that will increase the variable score by 1 when the chasing sprite is touching the moving sprite. You can use the touching? block to determine when the two sprites are touching. You can use the change by block in the Data menu to increase the value of score.
Exercise: Place your if then block within a loop to make the score increase any time the chasing sprite is touching the moving sprite. Remember to set the score to zero at the beginning of the program!
Exercise: Now that you have learned to use Scratch, use your imagination and modify your game to make it your own. How do you want to make your game better?
These lessons have covered only the basics in Scratch. There are a lot of other things you can do! You can use the resources below to learn more about all the things that you can do in Scratch.