Decisions and Variables in Snap!
In the first lesson, you wrote scripts to move a 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 block to make a decision. This block is found on the Control menu. What do you notice about this block?
The if block has a space in the middle that can hold other Snap! blocks. In addition, the if 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 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 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 block.
When a script reaches an if 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 block. If the value of the Boolean block is false, then the script skips the blocks inside the if and moves on to whatever is below the if 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 block, as shown below.
Exercise: Place your if 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 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 Variables menu. Then click Make a Variable.
The New Variable window will appear. Call the variable “score” and click OK.
The Variables menu also contains some blocks that you can use to change the value of your variable. 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 the 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 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 and the change by block in the Variables menu to increase the value of score.
Exercise: Place your if 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 Snap!, 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 Snap!. There are a lot of other things you can do! You can use the Snap Manual to learn more about all the things that you can do in Snap!.