Scratch Programming Class
Ronald Bourret
http://www.rpbourret.com/scratch
This document is available under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA
4.0) license. See the last page of this document for more information.
Lesson 5: Variables
Terminology: A variable is a place where you store a value for later use. For example, you might save the x and y coordinates of a
sprite so that it can return to its original position.
Note that you have already been using variables, such as x position and y position in Motion. These are variables that Scratch
maintains for you. In this lesson, we will be creating and using our own variables.
Starmaking Machinery, Part 1
1.
Log in to Scratch (http://scratch.mit.edu) and click on Create.
2.
Click Costumes, delete one of the costumes, and erase the other. Draw a simple pencil and set the center of the sprite to the point
of the pencil.
3.
Click Scripts, click Events, and drag when green flag clicked to the scripts area.
4.
Click Control, add a forever block and place an if-then block inside it. Click Sensing and set the if condition to mouse down?.
5.
Click Pen and place a clear block and a set pen color to block inside the if-then block. Click on the color in the set pen color to
block and then click on a color elsewhere on the screen that you like.
6.
Click Control and add a repeat 12 block after the set pen color to block.
7.
Inside the repeat 12 block, add the following blocks:
a.
go to x: y: (Motion), with x set to mouse x and y set to mouse y. You can find mouse x and mouse y in Sensing.
b.
pen down (Pen)
c.
point in direction (Motion), with the direction set to direction + 30. You can find the + operation in Operators and
direction in Motion.
d.
move 40 steps (Motion)
e.
pen up (Pen)
f.
wait .5 secs (Control)
8.
Click the green flag and click somewhere on the stage. Do not move the mouse after you click. The script will draw a star by
drawing 12 lines from the place you clicked in twelve different directions. After each line, it moves back to the center of the star.
9.
Name your project Starmaking Machinery and save it.
Starmaking Machinery, Part 2
1.
Click the green flag and click on the stage again, but this time move your mouse after you click. What happened? Do you know
why this happened?
2.
The problem is that the script moves the starting point back to (mouse x, mouse y). This is fine if the mouse doesn’t move, but a
big problem if it does. To solve this problem, we will save the coordinates of the mouse and use these instead.
3.
Click Data and click Make a variable. Enter center x and click OK. Make a second variable named center y.
4.
Add a set center x to block after the set pen color to block. Set the value to mouse x. Add blocks to set the center y variable to
mouse y.
5.
Inside the repeat 12 block, change the go to x: y: block to use center x and center y instead of mouse x and mouse y.
6.
Click the green flag, click the stage, and move your mouse. What happened? Why?
7.
Save your project.
Banana Splat, Part 1
1.
Create three sprites: a stick figure named Thrower, a banana named Banana, and a bird named Bird. Make two costumes for the
banana -- one normal and one that is splatted. The man should be about 70 units tall.
2.
Create two scripts for the man:
3.
4.

When the green flag is clicked, go to (0, -170) and broadcast the message “Fly”.

When the space key is pressed, broadcast the message “ThrowBanana”.
Create three scripts for the banana:

When the green flag is clicked, set the rotation style (Motion) to “don’t rotate” and point in direction 0.

When it receives the message “Fly”, switch to costume1 and go to a place that looks like it is in the man’s hand.

When it receives the message “ThrowBanana”, repeat moving 10 steps until it touches the edge or the bird. After the
repeating stops, switch to costume2.
Create two scripts for the bird:

When the green flag is clicked, set the rotation style to “don’t rotate” and the direction to -90.

When it receives the message “Fly”, go to (200, 100) and repeatedly move 10 steps and wait .05 seconds until it either
touches the edge or touches the banana. After it stops repeating, broadcast the message “Fly”
5.
Click the green flag and press the space bar to try to hit the bird with a banana.
6.
Name your project Banana Splat and save it.
Banana Splat, Part 2
1.
By now, you are probably arguing with your friends about who is better at chucking bananas at birds. The only way to solve this
is to keep score. Because we’re too lazy to do this with pencil and paper, we’ll have the computer do it. And to do that, we’ll need
variables.
2.
Click on the Bird sprite and click on Data.
3.
Click Make a Variable, name it Attempts, and click OK. Notice that the box next to the variable’s block is checked. This displays
the variables value on the screen, which is good, because we want to see your score.
4.
Create two more variables, one named Hits and one named Percent.
5.
Add set Attempts to 0, set Hits to 0, and set Percent to 0 to the script that is executed when the green flag is clicked.
6.
In the script that is executed when the message “Fly” is received, add the following after the repeat until block:
7.

A change Attempts by 1 block.

An if-then block that changes Hits by 1 if the bird is touching the banana.

A block that sets Percent to (Hits/Attempts)*100.
Save your project. Click the green flag and earn your bragging rights.
Frogs, Cannonballs, and Flying Fruit, Part 1
1.
It’s time to start throwing things: frogs, cannonballs, flying fruit. The trajectory of flying objects is ruled by an equation called a
parabola. In its simplest form, a parabola looks like this:
y = ax2
and has a shape like this:
a is a coefficient that tells you the shape of the parabola. If a is negative, the parabola will go up and then down (like the trajectory
of a rock you throw in the air). If a is positive, the parabola will go down and then up, like the shape of a chain hung between two
posts. If a has a small absolute value (is close to 0), the parabola will be flatter. If a has a large absolute value (is far from 0), the
parabola will be steeper.
2.
Create a new project and name it “Frogs, Cannonballs, and Flying Fruit”.
3.
Change the first costume of Sprite1 to something you want to fly through the air: a frog, a cannonball, a piece of fruit, etc. Change
the second costume to what that object looks like when it lands: a frog, an explosion, a piece of splattered fruit.
4.
Drag a when green flag clicked block onto the scripts area, add clear and pen up blocks from Pen, and add a switch costume to
costume1 block from Looks.
5.
Create two variables named x and y. Set x to -40 and y to -.05*x*x. Add a block to move to (x, y).
6.
Add a pen down block from Pen.
7.
Add a repeat 20 block. Inside it, add blocks to change x by 4, set y to -.05*x*x and move to (x, y).
8.
After the repeat 20 block, add a switch costume to costume2 block.
9.
Click the green flag. If you have ever played a video game in which characters jump or are shot through the air (think Angry
Birds), the motion should be familiar to you.
10. Save your project.
Frogs, Cannonballs, and Flying Fruit, Part 2
1.
Of course, not everything has the same trajectory. When you throw a superball into the air, it goes way up and comes down near
the same place. When you throw a baseball to home plate (or a chicken at your friend), it goes pretty flat. So it’s time to play with
trajectories by changing the coefficient of the parabola. (Don’t remember what a coefficient is? Go back to the previous section
and reread the first step.)
2.
Click on the stamp tool and click on the when green flag clicked block. This will make a copy of your script.
3.
Move the copy to one side and replace the when green flag clicked block with a when space key pressed block.
4.
Right after the when space key pressed block, insert a ask What’s your nam?e and wait block (Sensing). Change “What’s your
name?” to “Enter a coefficient between -.1 and .1”.
5.
In the two blocks that set the value of y, replace the -.05 with the answer block from Sensing. (answer is a variable whose value is
set by the system to whatever you enter.)
6.
Click the green flag, then press the space bar. Enter a value and click the check mark to see what shape your parabola has. Play
with different coefficients and see what shapes you can make: tall and thin, long and flat, etc.
7.
Save your project.
Dizzy
1.
Create a new project with two costumes: a stick figure dog walking and a stick figure dog chasing its tail (see picture below).
Don’t worry if your dog looks like a dead rat. Set the center of each costume to the dog’s center (stomach).
2.
Create a script that does the following:

Go to (0,0)

Repeat 36 times: turn 30 degrees clockwise and wait .1 seconds.

Repeat 10 times: set the direction to a random number between 60 and 120 (Operators), move 20 steps, and think, “I’m
so dizzy” for 0.5 seconds (Looks).

Point in direction 180 and think, “Bleaaaaah.” for 1 second.
3.
Click the green flag.
4.
Name your project “Dizzy” and save it.
Dizzy: Counterdizzywise
1.
Remove the when green flag clicked block and replace it with a when I receive Spin block.
2.
Under the when green flag clicked block, add blocks to do the following:
3.

Ask “Clockwise (y/n)?” and wait.

If the answer is y, set the Direction variable to 1. Otherwise, set it to -1. Note that you will need to add the Direction
variable.
Modify the Spin script so it does two new things:

Turn clockwise when the Direction variable is 1 and counter-clockwise when the direction variable is -1.

The figure walks to the right when the Direction variable is 1 and to the left when the direction variable is -1.
You will need to figure out how to do this.
4.
Click the green flag, answer the question, click the check mark, and see if your figure does what it’s told.
5.
Save your project.
Dizzy: Spin Cycle
1.
At the bottom of the when green flag clicked script, add a new prompt to ask how many turns the stick figure should take. Set the
Turns variable (you will need to add it) to the number of turns.
2.
Modify the Spin script so that the stick figure spins the correct number of times. You will need to figure out how to do this.
3.
Click the green flag, answer the questions, and see if your figure does what it’s told.
4.
Save your project.
Dizzy: Hyperspace
1.
At the bottom of the when green flag clicked script, add a new prompt to ask how fast the figure should spin: fast (f), medium
(m), or slow (s)? Set the Speed variable (you will need to add it) to 3 for fast, 2 for medium, and 1 for slow.
2.
Modify the Spin script so that the figure walks fast, medium, or slow, depending on the value of the Speed variable.
3.
Click the green flag, answer the questions, and see if your figure does what it’s told.
4.
Now modify the Spin script again. This time, make the figure spin fast, medium, or slow based on the value of the Speed variable.
5.
Again, click the green flag, answer the questions, and see if your figure does what it’s told.
6.
Save your project.
More projects:
Calculator
1.
Build a simple calculator.
2.
Prompt the user for a number, an operation, and another number. Store these values in variables named number1, operation, and
number2.
3.
Using the variables number1, operation, and number2, calculate the result of the operation and display it. For example, if the user
enters “2”, “+”, and “4”, display “6” because 2+4=6. Hint: You will need one more variable.
4.
Click the green flag to test your project.
5.
Name your project Calculator and save it.
Spiralitudity
1.
It is possible to draw a curve from a series of short, straight line segments. If the angle between segments and the segment length
are both the same, you get a circle. If the segment length increases or the angle decreases (or both), you get a spiral.
2.
Write code to draw a spiral. Keep the angle between segments the same and increase the length of each segment. Hints: Use the
clear, pen down, and pen up blocks to do the drawing and a repeat block to draw each segment.
3.
Modify your code so that the length increases and the angle decreases.
4.
Name your project Spiralitudity and save it.
Fibonacci Numbers
1.
Leonardo Fibonacci (aka Leonardo Pisano Bigollo, Leonardo of Pisa, Leonardo Pisano, Leonardo Bonacci) was a 13th century
Italian mathematician (and not a pizza maker from New Jersey, as many claim). He invented a sequence of numbers in which you
start with 1, 1 and get each additional number by adding together the previous two numbers. Thus, the sequence is:
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
because 1+1=2, 1+2=3, 2+3=5, 5+8=13, 8+13=21, 13+21=34, and so on.
2.
Write a program to calculate and display the first 10 Fibonacci numbers.
Hint:
Some terminology: A string is a sequence of characters. Variables can have string values, like “abc”, or numeric values, like 27.
You can build a string using the join block in Operators. For example, if you have variables FirstName and LastName, you could
join these together and store them in a variable called FullName with the following blocks:
One problem is that this doesn’t insert a space between the first and last names. You need to add the space yourself. For example,
the following block joins FirstName to the join of a space (in the white square) and LastName:
You can also join a string variable to itself. This is a good way to keep adding stuff to the end of a string (like each Fibonacci
number that you calculate, HINT, HINT). For example, the following initializes FullName to nothing, then adds FirstName, then
adds a space, then adds LastName:
3.
Modify your project so the cat asks how many Fibonacci numbers you want to generate, then generate that many and display
them.
4.
Name your project Fibonacci and save it.
Primates do Primes
1.
Ask the user how many primes they want. Calculate as many primes as the user wants and display a list of them. Remember that 1
is not a prime number. The first prime number is 2.
Hints:
2.

To find out if one number is divisible by another, use the mod block in Operators. This tells you the remainder of
division. For example, 7 mod 3 is 1 because 7 divided by 3 has a remainder of 1.

Before you create your script, write down what your program will do on a piece of paper. For example, what numbers do
you need to divide by to figure out if a number is prime? When can you stop? What blocks will you use?
Name your project Primates do Primes and save it.
License
This document is available under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA
4.0) license. Under this license, you may:

Share — copy and redistribute the material in any medium or format

Adapt — remix, transform, and build upon the material
Under the following terms:

Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may
do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

NonCommercial — You may not use the material for commercial purposes.

ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same
license as the original.
For a human-readable summary of this license, see:
http://creativecommons.org/licenses/by-nc-sa/4.0/
For the complete license, see:
http://creativecommons.org/licenses/by-nc-sa/4.0/legalcode