Bourret ScratchProgramming
Bourret ScratchProgramming
Table of Contents
Introduction .................................................................................................................................3
Chapter 1: Movement and Loops ................................................................................................5
Chapter 2: Conditional Actions and Keyboard Commands ........................................................9
Chapter 3: Messages .................................................................................................................13
Chapter 4: Animation ................................................................................................................15
Chapter 5: Practice, practice, practice! .....................................................................................21
Chapter 6: Variables .................................................................................................................23
Chapter 7: Algorithms...............................................................................................................29
Chapter 8: Program Structure ...................................................................................................33
Chapter 9: Advanced Programs ................................................................................................41
Chapter 10: Project ...................................................................................................................43
Scratch
This class introduces programming using the Scratch programming language. The Scratch programming
language and environment are a project of the Lifelong Kindergarten Group at the MIT Media Lab.
They are available free of charge.
You can find Scratch at:
http://scratch.mit.edu
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
2
Introduction
This tutorial will introduce you to programming using Scratch from MIT.
Scratch Editor
The Scratch editor has three main parts:
Stage: Where your program runs.
Sprite list: A list of the sprites (objects) in your program.
Script editor / costume editor: Where you edit your programs or your sprite’s pictures.
When the Scripts tab is chosen, the script editor is shown (outlined in red):
3
When the Costumes tab is chosen, the costume editor is shown (outlined in red):
4
Chapter 1: Movement and Loops
In this chapter, you will learn how to build simple scripts to make a sprite move around the stage.
3. From the Motion menu, drag a goto x: 0 y: 0 block to the scripts area and snap it to the bottom of the
when green flag clicked block. Your script should look like this:
4. Add a move 10 steps block to the bottom of your script and change the 10 to 100.
5. Click the green flag at the top right corner of the stage to run your program. Let’s look at what
happened:
The when green flag clicked block tells Scratch that the other blocks in the script are to be
executed at the start of the program — that is, when the green flag is clicked.
The go to x: 0 y: 0 block tells Scratch to move the sprite — that’s the cat, which is what you’re
writing instructions for — to the coordinates (0, 0). The stage has a coordinate system with (0, 0)
in the center, x values ranging from –240 to +240, and y values ranging from –180 to +180.
The move 100 steps block tells Scratch to move the sprite 100 units in the direction it is facing.
5
Lesson 1-2: Turning and Waiting
Program Name: Square (continued)
Now it’s time to make your sprite move in a square.
1. Drag a turn counterclockwise 15 degrees block to the bottom of the script and change the 15 to 90:
2. Add more blocks until you have four move/turn pairs in your script:
3. Run your program. What happened? Nothing? Actually, the sprite moved in a square, but so quickly
that you couldn’t see it. You can fix that by adding wait 1 secs blocks from the Control menu to the
stack:
4. Run your program again. Better? If your sprite is too slow, you can always change the wait time.
6
Lesson 1-3: Repeat Loops
Program Name: Square (continued)
Look at your script. One thing that should strike you is that three blocks — move, turn, wait — are
repeated four times. Imagine if you wanted your cat to walk in a square 100 times. You’d need 1,200
blocks! That’s insane. (Not to mention a lot of work.)
Fortunately, there is an easy way to fix this: the repeat loop.
1. Drag the four sets of move/turn/wait blocks away from the bottom of the script.
2. Drag a repeat 10 block to the bottom of your script and change the 10 to a 4.
3. Drag one set of move/turn/wait blocks inside your repeat block. The result should look like this:
4. Now run your program. Your sprite still moves in a square, but with far fewer commands.
Repeat loops are an incredibly important programming tool. They allow you to repeat actions without
having to add extra blocks, thus saving lots of work. Use repeat loops!
7
Lesson 1-6: Cleanup and Save
Program Name: Square (continued)
You probably have a lot of unused blocks laying around the script area.
1. Drag the unused blocks to the blocks palette to delete them.
2. Click File/Save to save your project.
8
Chapter 2: Conditional Actions and Keyboard Commands
In this chapter, you will learn to execute actions only under certain conditions. You will also learn to
make your sprite respond to keyboard commands.
(If your sprite doesn’t do anything at all, make sure you started your script with a when green flag
clicked block.)
4. After the move block, add blocks to create an if x position > 240 then command. You will need an if
___ then block from the Control menu, a greater-than (>) block from the Operators menu, and the
x position block from the Motion menu. You will also need to enter the number 240 in the greater-
than block. Your blocks should look like this:
5. Inside the if-then block, add a turn counterclockwise 180 degrees block:
This script tells Scratch to check if the x position of the sprite is greater than 240 — that is, if it is at
the right edge of the stage. If so, Scratch rotates the sprite 180 degrees. If not, nothing happens.
(There is also an if-then-else block so you can do something else if the test condition is false.)
Along with repeat loops, if-then and if-then-else statements are two of the main building blocks of
programs. Use them!
6. Run your program. Your sprite should move to the right edge of the stage, turn around, and move to
the left edge of the stage, where it gets stuck. To fix this, add blocks to rotate the sprite 180 degrees
if the x position is less than –240.
7. Run your program again. Your sprite should move endlessly between the right and left edges of the
stage.
8. To finish your program, keep your cat right side up. (You may have noticed that it is upside down
when moving left.) To do this, add a set rotation style left-right block from the Motion menu as the
first block in your script.
9
Lesson 2-2: Keyboard Commands
Program Name: Back and Forth (continued)
Let’s face it. Walking back and forth is boring. Time to make your sprite jump. To do this, you will add
blocks that move the sprite up and down 50 steps when the space bar is pressed.
1. From the Events menu, drag a when space key pressed block to the scripts area:
An event is something that happens. The main events in Scratch are starting the program, pressing
keys, and broadcasting and receiving messages, which are used to communicate between scripts and
between sprites.
2. Beneath the key-pressed block, add blocks to move the sprite up and down 50 steps in increments of
10 steps. You will need to use the change y by 10 block from the Motion menu.
3. Run your program. When you press the space bar, the sprite should jump up and fall back down.
2. Select the Lightning sprite and click OK. This adds a new sprite to the sprite list.
3. Add a script to hide the Lightning sprite when the program starts. You will need the hide block from
the Looks menu.
4. Add a script to make the Lightning strike the cat when the space bar is pressed. The lightning sprite
should move to the cat sprite, show itself, wait 0.1 seconds, and hide itself again. In addition to the
blocks you have already learned, you will need the go to mouse-pointer block on the Motion menu
— you will need to change mouse-pointer to Sprite1 in the drop-down list — and the show block
from the Looks menu.
5. Run your program, press the space bar, and watch the cat jump.
10
Lesson 2-4: Adding Sound
Program Name: Back and Forth (continued)
To finish the program, add a really good mrowr! when the cat gets zapped.
1. Click Sprite1 in the sprite list and click the Sounds tab.
2. Try the meow sound already there. If you don’t like it, click the speaker icon to choose a different
sound from the sound library.
3. Click the Scripts tab and find the play sound block from the Sound menu. Add this block to the
when space key pressed script. (Select the sound you want from the drop-down list.)
4. Run your program. Zap your cat.
3. In the lower right corner of the costume editor, click Convert to vector. (If you are already in vector
mode, you don’t need to do this. We’ll explain bitmap and vector modes later. For now, all you need
to know is we will always use vector mode.)
4. Using the line tool (on the upper right side of the editor), draw a simple maze with red lines. Make
sure that the lines are far enough apart for the cat sprite to (barely) fit between them.
11
Pressing the arrow keys moves the cat.
If the cat touches the edge or a red line, it goes back to the lower left corner.
In addition to the blocks you already know, you will need the touching ? and touching color ?
blocks from the Sensing menu. To use the touching ? block, select edge from the drop-down list. To
use the touching color ? block, click on the color in the block, then click on the red in the maze. The
color in the block should turn red.
For extra credit, play a sound when the sprite reaches the lower right corner of the maze.
12
Chapter 3: Messages
In this chapter, you will learn to use messages, which are used to communicate between scripts and
between sprites.
4. Drag a when I receive block to the scripts area and set the value to sprite 1’s turn:
5. Add blocks to the when I receive script to glide to a random position, then broadcast a new message
named sprite 2’s turn. You will need the glide 1 secs to x: y: block from the Motion menu, two pick
random 1 to 10 blocks from the Operators menu, and a broadcast block. Use the random blocks to
choose an x value between –240 and 240 and a y value between –180 and 180.
6. Click Sprite2. Create a script to receive the message sprite 2’s turn, glide to the position of Sprite1,
and broadcast the message sprite 1’s turn. To determine the position of Sprite1, use
__ of __ blocks from the Sensing menu, set as follows:
7. Run your program. Sprite1 should glide to a random position on the stage; Sprite2 should glide to
Sprite1’s position; Sprite1 should glide to a new position; and so on.
A message is like yelling into a crowd of people, “Who is supposed to wash the dishes?”
Everybody can hear it, but only those people (sprites) who have been told they are on dish duty
(have a when I receive block for the dish duty message) will respond. This could be zero, one,
many, or all people (sprites).
b) Because Sprite1 has a when I receive block for the message sprite 1’s turn, it executes the script
for that message. In this case, it randomly picks an x value between –240 and +240 and a y value
between –180 and +180, then glides to the new position. After gliding, it broadcasts the message
sprite 2’s turn.
13
c) Because Sprite2 has a when I receive block for the message sprite 2’s turn, it executes the script
for that message. In this case, it finds the x and y coordinates of Sprite1, glides to that position,
and broadcasts the message sprite 1’s turn, starting the cycle all over again.
14
Chapter 4: Animation
Lesson 4-1: Bitmap Mode
Before learning animation, you need to learn about two different ways to draw with a computer: bitmap
drawing and vector drawing. In this lesson, you will learn about bitmap drawing.
1. Create a new project and name it Bitmap vs. Vector.
2. Click the Costumes tab and click Clear to delete the cat.
3. Look in the lower right hand corner and make sure you are in Bitmap Mode. If you are not in Bitmap
Mode, click Convert to bitmap.
4. Using the line tool, draw an X with a short and a long arm:
5. Now suppose you want to rotate the short line while keeping the long line in place. Using the Select
tool, select the short line:
6. Using the rotation handle (the circle at the top of the line coming out of the selection box), rotate the
short line:
What happened? Both the short line and a segment of the long line in the selection box rotated. This is
because you didn’t actually select just the short line, you selected everything in the selection box.
In bitmap mode, the computer does not understand what a line is. Instead, it just stores a grid of dots.
For example, this is what the computer stores for a short line:
(Actually, the computer stores a grid of numbers, where each number tells the computer what color to
draw each dot in the grid. But it’s easier to think of the computer as just storing a grid of dots.)
Because the computer just stores dots, it doesn’t understand anything about those dots. That is, it doesn’t
realize that the grid shown above is a line, or that a different grid might show a circle or a triangle. All it
sees are dots.
As a result, when you use the select tool, you can’t select a particular line in the grid. You can only
select the dots in the grid. And when you rotate the grid, you rotate all of the dots in the grid.
15
Lesson 4-2: Vector Mode
In the lesson 4-1, you learned about bitmap drawing. In this lesson, you will learn about vector drawing:
1. Click Costume2, clear the canvas, and check that you are in Vector Mode. If you are not in Vector
Mode, click Convert to vector.
2. Using the line tool, draw the same lopsided X you drew in the previous lesson.
3. Using the select tool, select the shorter line and rotate it:
What happened? This time, only the shorter line rotated. The reason for this is the difference between
vector graphics and bitmap graphics. In vector graphics, the computer does not store a grid of bits.
Instead, it stores information about the actual objects you draw.
For example, for a line, it stores the endpoints of the line, as well as the fact that you are drawing a line.
For a circle, it stores the center and radius of the circle, and the fact that you are drawing a circle.
Because vector graphics store information about individual objects — as opposed to a grid of dots —
you can select, rotate, move, and resize individual objects without affecting other objects, even if the
objects overlap.
In general, you will use vector graphics. Bitmap graphics are useful when creating objects that can’t
easily be described by equations, such as the cat sprite or a photograph. However, they are more difficult
to work with and this class is about writing programs, not drawing pictures.
16
3. Click the Costumes tab, clear the canvas, make sure you are in Vector Mode, and use the line and
oval tools to draw a simple stick figure of a man walking. Make sure you use separate lines for the
body, arms, and legs. This is because we need to reposition the arms and legs later.
Don’t spend too much time drawing. We’re learning programming, not fine art.
4. Using the Set costume center tool (the plus sign in the upper right corner of the screen), set the
center of the costume to a point on the “ground” just beneath the man’s feet:
A costume’s center is used for movement and rotation. When you tell a sprite to go to a certain
point, its center goes to that point. When you tell a sprite to rotate, it rotates around its center.
5. Click costume2 and delete it, then make two copies of costume1. You can use the stamp tool at the
top of the screen, or right click and select duplicate.
6. Click costume2 and use the zoom controls — the plus and minus in the lower right corner of the
screen — to zoom in on your stick man. Rotate and move his arms and legs:
8. Finally, click costume2 and duplicate it. You should now have four costumes, with costume2 and
costume 4 being identical:
17
Lesson 4-4: Simple Animation
Program Name: Hiding Man (continued)
Now it’s time to make your man walk.
1. Click the Scripts tab and create a script that changes to the next costume forever. You will need the
next costume block in the Looks menu:
2. Run your program and watch your man walk in place. If he’s moving too fast, you can add a wait
block after each costume change.
3. After you’re done, delete your forever block. We just used it to show you how animation works.
18
You will need to play with the number of repetitions, the pause time, and the amount you change the
size by to get this to look right.
Notice that the Tree sprite covers the Hiding Man sprite. This is because of something called layers.
Each sprite has its own layer and the layers are placed in a stack. It’s easiest to think of painting each
sprite on its own sheet of clear plastic (layer). When you place the sheets in a stack, you will only see
those sprites (or parts of sprites) that are not covered by a sprite higher in the stack.
To see layers in action, go to the Looks menu and click the go back 1 layers and go to front blocks.
These will move the tree behind the man and back in front of the man. Although we won’t use these
blocks in this program, you should know they exist.
19
2. In each of Walking Man’s costumes, select the entire costume and flip it using the Flip left-right
tool:
3. Change the script so that the Walking Man sprite walks horizontally from right to left across the
screen, from (250, –140) to (–250, –140):
20
Chapter 5: Practice, practice, practice!
Now it’s time to practice what you’ve learned.
IMPORTANT: KEEP YOUR GRAPHICS SIMPLE! Don’t spend more than two or three minutes
drawing pictures. Spend your time writing scripts.
21
Lesson 5-4: Auto-mazeing
Program Name: Auto-mazeing
Create a sprite that can find its own way through a maze. Hints:
Draw a simple maze in red and an arrow sprite to go through
the maze.
Write a script to move through the maze, starting from the
lower left corner. The script should use knowledge of the shape
of the maze and use as few commands as possible.
22
Chapter 6: Variables
Variables in computing are used to store values. You have already seen some variables. For example, in
the Back and Forth project, you used the variable to determine if the sprite was near an edge.
In Taking Turns, you used the and variables so that Sprite2
could go to the position of Sprite1.
, , and are examples of variables maintained by
Scratch. That is, Scratch stores values in these variables and you can use those values in your programs.
You can also define your own variables and store values in them. For example, you could use variables
to store scores in a game, store the speed of a meteor crashing to earth (so you know how big a hole to
draw when it hits), or calculate the value of y in the equation y = 3x + 10 for a given value of x.
Variables can be shared by all sprites or used only by one sprite. is an example of a
variable used by only one sprite: it gives the current x position of that sprite. Other sprites have their
own copies of this variable. is an example of a variable shared by all sprites —
any sprite can use it.
Variables that can be used only by one sprite are called local variables. Variables that can be used
by all sprites are called global variables. As a general rule, you should use local variables.
3. When the program starts, use ask _______ and wait in the Sensing menu to ask for a number:
4. Scratch stores the number entered by the user in the answer variable from the Sensing menu. Use
the set ______ to ____ block in the Data menu to store the answer in your number variable:
6. To show the user what their number plus 2 is, use the say ____ for 2 secs block in the Looks menu:
23
7. Run your program, enter a number, and check that the program displays the correct answer.
8. Create local variables to hold the value of the number minus 2 and times 2, set these variables, and
display their values. (Note that the multiplication operator uses an asterisk. This is because the letter
x is commonly used as a variable name; it would be confusing if it also meant multiplication.)
9. Test your program to make sure it displays the correct values.
It is important to understand that this is a program you could not have written without variables. Because
you did not know what number the user was going to enter, you could not have computed the value of
that number plus, minus, and times 2 without using a variable.
The join operator takes two strings and joins them together. In this case, you are joining the string
“Your number plus 2 is: ” and the value of the plus2 variable.
3. Change your say plus2 for 2 secs block to use the output variable:
24
Lesson 6-4: Displaying Accuracy in Archery
Program Name: Archery (continued)
You may have noticed in the previous lessons that the values of variables were displayed on the stage.
This is useful for checking variable values as a program runs. It can also be used for things like
displaying scores in a game. (If you don’t want to display a variable’s value, uncheck the box next to the
variable in the Data menu.)
In this lesson, you will use variables to display your accuracy in the archery game.
1. Reopen your archery game.
2. Create local variables for the number of shots, the number of hits, and the accuracy (percentage of
hits). Only display the accuracy.
3. At the start of the game, initialize the variables.
4. After each shot:
a) Add 1 to the number of shots with the change ____ by 1 block in the Data menu.
b) If the arrow hits the target, add 1 to the number of hits.
c) Calculate the accuracy from the number of hits and number of shots.
5. Test your program by deliberately hitting and missing the target and making sure that the calculated
accuracy is correct.
25
2. Change the costume of Sprite1 to a small circle and then draw horizontal and vertical lines through
the point (0, 0). You can either create a Graph sprite whose costume has these lines or draw them on
the backdrop for the stage. Your stage should look like this:
Setting the value of a variable for the first time is called initializing the variable. It is very
important: If you don’t initialize a variable, the computer doesn’t know what value to use when
you use that variable. Not initializing variables is the cause of many bugs.
b) Set y to (x * 3) + 10. When you do this, make sure that you multiply x by 3 before adding 10. It’s
a bit hard to see, but be sure to use:
and not:
This is important, as the first expression multiplies x by 3 and then adds 10 to the result — that
is, (3 * x) + 10 — while the second expression adds x to 10 and multiplies the result by 3 — that
is 3 * (x + 10).
c) Go to the newly calculated point (x, y):
5. Add the following blocks from the Pen menu to your script:
These blocks clear (erase) any previous drawing, change the pen color to blue (it doesn’t actually
matter what color you use), and put the pen “down” — that is, get the pen ready to draw.
26
6. Now you’re ready to draw our graph. Inside a loop that repeats 11 times, do the following:
a) Use the change x by 10 block from the Data menu to increment (add to) the value of x:
Changing the value of a variable by a fixed amount is called incrementing the variable and is a
very common thing to do in computer programming. For example, when you added 1 to the
score in your archery and Pong games, you were incrementing the score.
b) Set the value of y using the same calculation you used above.
c) Go to the new point (x, y). Because the pen is “down”, your program will draw a line from the
previous point to the new point.
7. After the loop, use the pen up block from the Pen menu to lift the pen off the stage.
8. Run the program and watch Sprite1 graph the equation y = 3x + 10.
27
instead of y = 3x + 10. You will need to experiment with the initial value of x, the number of
repetitions, and the amount by which x is incremented to draw a smooth curve that doesn’t go off the
edge of the stage.
4. When it reaches the end of its flight, use the second costume to make it go splat.
5. Splat away.
28
Chapter 7: Algorithms
Most computer programs use algorithms, which are a set of instructions designed to complete a certain
task, such as tracking the flight of a rotten tomato through the air.
It is usually a good idea to think of your algorithm before you start writing your program. For example,
the algorithm for graphing the points on the line y = 3x + 10 is:
pick a starting value for x
calculate a corresponding value for y using the equation y = 3x + 10
go to (x,y)
This loop adds the numbers from 1 to the number entered by the user. It is important to understand
how it works.
Remember that we already set lastValue to the number entered by the user and initialized number to
1 and total to 0. Suppose the user entered the number 3.
The first time through the loop, number (1) is less than lastValue (3), so we go inside the loop.
The first block in the loop sets the new value of total to the old value of total (0) plus number
(1). Thus, the new value of total is 0 + 1 = 1.
The second block in the loop changes the value of number by 1. That is, it adds 1 to the current
value of number. Since the current value of number is 1, the new value is 1 + 1 = 2.
Changing the value of a variable by a fixed amount is called incrementing the variable. It is a
very common operation in loops.
29
The second time through the loop, number (2) is still less than lastValue (3), so we go inside. This
time, total is set to 1 (old value) + 2 (number) = 3 and number is incremented to 3.
The third time through the loop, number (3) is equal to lastValue (3), so we go inside. total is set to
3 (old value) + 3 (number) = 6 and number is incremented to 4.
The fourth time through the loop, number (3) is greater than lastValue (3), so we stop.
6. Add blocks to display the output using the string “The sum of the numbers from 1 to n is: m”, where
n is the number entered by the user and m is the total.
7. Test your program.
30
Lesson 7-5: Sorting (Optional)
Program Name: Sorting
In computer programming, a list is a kind of variable that can store as many values as you want. The
values are stored in specific positions in the list. For example, in the following list, 3 is stored in the first
position, 5 is stored in the second position, 2 is stored in the third position, and so on.
3, 5, 2, 17, 1
You can insert, delete, or replace values in the list. For example, if you insert the number 43 in the
second position in the list, the list becomes:
3, 43, 5, 2, 17, 1
If you then delete the fourth value in the list, the list becomes:
3, 43, 5, 17, 1
And if you then replace the fifth value in the list with 99, the list becomes:
3, 43, 5, 17, 99
You can create a list with the Make a List button on the Data menu. After you create a list, the Data
menu contains blocks for working with lists. (Note: To delete all of the values in the list, use the delete
__ of ___ block and set the entry number to all.)
Write a program that randomly generates 10 numbers between 0 and 100, stores these in a list, and then
sorts them from lowest to highest.
31
32
Chapter 8: Program Structure
Most of the programs you have created so far have contained a single script. In complex programs, a
single script has two significant problems:
Lack of readability. In a single, long script, it is difficult to understand the overall flow of a
program because you get lost reading all the details.
Lack of flexibility. In a single, long script, it is difficult to modify, rearrange, or reuse parts of the
script.
In this chapter, you will see how to add structure to a program through the use of messages and More
blocks, which are blocks you define yourself.
33
At a glance, it’s pretty difficult to see what is happening. This is because the script is a single, long set of
commands with no structure. In computer programming, this is known as spaghetti code and, much as
you might like spaghetti, it’s not a good thing. In addition to being difficult to read, spaghetti code is
hard to rearrange (for example, spinning first and jumping second) and to change (for example, adding a
second group of five jumps).
To fix this, replace each set of actions — initialize, walk, jump, walk, spin, walk — with a message and
another script. For example, replace the loop to jump three times with this broadcast ___ and wait
block:
Notice that this uses broadcast ___ and wait and not broadcast ___. This is because a broadcast block
without the wait passes control immediately to the next block; it does not wait for any scripts that
receive the message to finish. For example, these messages make the sprite jump and then spin:
while these messages make the sprite jump and spin at (almost) the same time:
1. Replace each of the actions in your script — initialize, walk 200 steps, jump 3 times, walk 100 steps,
spin twice, walk 500 steps — with a broadcast-and-wait block and a script.
34
Lesson 8-3: More Blocks
Program Name: Walk, Jump, Spin (continued)
Your main script now looks something like this:
This is much better, as it is easy to see what is happening. It would also be easy to rearrange the order in
which things occur.
One problem is that there are three different messages for walking, depending on how far the sprite
walks. To fix this, use a More block, which is a block you define yourself. (The name More block comes
from the fact that you are defining more blocks. In most programming languages, these are known as
functions.)
1. In the More Blocks menu, click Make a Block and then click Options:
2. In the purple block, type walk. This is the label on the block, like “move” on a move ___ steps block.
35
3. Click Add number input and change the label of the number input to number_of_times. This adds a
parameter to the block, allowing you to send a value to the block. A parameter is a variable used in a
More block. In this case, the parameter specifies the number of sets of 10 steps to walk.
4. Click Add label text. This adds another label to the block. Type times.
5. Click OK. This creates a walk __ times block in the More Blocks menu and a define walk
number_of_times times block in the scripts area. You will use the walk __ times block in your script
to walk, and the define walk number_of_times times block to define what the walk __ times block
actually does.
36
6. Beneath the define walk number_of_times times block, add a repeat loop. Inside the repeat loop,
place a move 10 steps block. Drag the number_of_times variable from the define block to the space
that tells the repeat loop how many times to repeat:
7. In your main script, replace the first broadcast-and-wait block with a walk ___ times block and set
the value to 20:
8. Replace the other broadcast-and-wait blocks that send walk messages with walk __ times blocks.
9. Run your program.
It is important to understand what is happening here. When the walk __ times block is executed:
a) The calling script specifies the number of times to walk. For example, it is 20 the first time the
walk __ times block is used.
b) Scratch sets the number_of_times parameter to the value used in the block.
c) The loop in the walk __ times block executes the move 10 steps block number_of_times times.
Most programs are written this way, with a main set of commands calling functions — More blocks,
in our case — to do most of the work. In complex programs, functions commonly call other
functions. You can do this in Scratch, too: The definition of a More block can include More blocks.
37
Lesson 8-5: Messages vs. More Blocks
One question that might have occurred to you by now is when to use messages and when to use More
blocks. In some cases, the choice is clear:
Use More blocks when you need to pass different values to a script. For example, your script draws
a line between two points you pass or makes a sprite jump in a parabola whose height you pass.
Use messages when you need to tell more than one sprite to do something. (More blocks can only be
used in the sprite they were created in.)
Use messages — in particular, use broadcast message blocks — when you want things to happen
simultaneously, such as spinning and moving at the same time. Scripts wait until More blocks (and
broadcast message and wait blocks) finish before executing the next block. They continue
immediately after executing a broadcast message block.
In other cases, you can use either More blocks or messages but one feels more appropriate:
Use More blocks for “utility” routines that can be used from many different places in your project.
For example, you used a More block in three different places to tell your sprite to walk.
Use messages to control the flow of your project. For example, you used a message to initialize your
program.
38
Lesson 8-7: Return Value Practice
Program Name: Run Away
The distance between a point (x1, y1) and a point (x2, y2) is:
square root of ( (x1 – x2)2 + (y1 – y2)2 )
Create a program with two sprites. The sprites are identical except that one starts on the left side of the
screen and walks right and the other starts on the right side of the screen and walks left. The two sprites
should walk until they are within 60 steps of each other, at which point they turn around and run back to
the edge.
Use a More block to calculate and return the distance between the two sprites.
39
One thing that is important in writing recursive functions is to have a stopping condition. That is, the
function needs to decide to stop sometime. Otherwise, it will keep calling itself forever. In this case, the
spiral stops when it touches the edge.
40
Chapter 9: Advanced Programs
In this chapter, you will practice more complicated programs. Use More blocks and messages to break
your program into more manageable pieces. For example, most of these programs need a More block
that can determine if a number is prime.
41
42
Chapter 10: Project
The final four class sessions will be used to create a project of your own design. You will spend half a
class designing your project and 3 1/2 classes writing it. In the final class, everybody will present their
project to the class.
Your design should include:
A summary of your project. For example, “My project tells the story of Hansel and Gretel” or “My
project is a game where Mervin the Cucumber runs through the forest and fights vegetables.”
Sketches of the background and sprites in your project and how they fit together.
Descriptions of what the sprites do and how they interact. For example:
o “Hansel, Gretel, the house, and the witch are separate sprites. Hansel and Gretel walk
through the woods and eat the house. The witch then catches and eats them.”
o “Mervin spits cucumber seeds (which are separate sprites) at vegetables, which dodge the
seeds. After five hits, a vegetable explodes into three separate sprites, which fly through the
air. If any of these sprites hit the cucumber, it dies. The cucumber must slay a carrot, an
onion, a rutabaga, and a tomato to win.”
IMPORTANT:
Keep your design simple. You can always add more later.
Spend your time writing code, not drawing costumes.
The teacher must approve your design before you can start writing your project.
43