Scratch Basics
Scratch Basics
74
Scratch map
Take a look at the diagram
on the right. This shows you
which area of the screen to
look at when performing STAGE
different actions. We will use BLOCKS PALETTE
these terms for each area SCRIPTS AREA
when referring to them in
the instructions.
SPRITE LIST STAGE
LIST
Change the
screen layout.
Programs run
on the stage.
Position of the
current sprite
selected on the stage.
Backdrops
Zoom in or out 1
of scripts using
these buttons. Sprite 1
Button to
change the
backdrop.
Current sprite Button to add
selected new sprites.
75
Coding in Scratch
The blocks in Scratch are color-coded according to what
sort of code they run. Snapping them together is as easy
as drag, drop, click! Here are some tips and tricks that will
have you building and running exciting programs in no time.
Types of block
There are nine main types of block in Scratch. Each block
type has a special color so that you know where to find it
in the Blocks Palette and what type of code it runs.
Events
play sound meow ▾ until done when clicked wait 1 second
You can add extensions to the Blocks Palette by clicking here. draw a triangle
Extension blocks are all the same color, but allow you to add music,
video, and other features to your program. My Blocks
You can create your own blocks,
called functions, and they are a
reddish pink.
76
Flow of scripts Running scripts
Each stack of blocks is called a script. Scripts run To run your script, click on it in the Scripts Area
in order, from top to bottom. Different scripts or do what is shown on the event at the very
can run at the same time, but each instruction in top. If you want to run two lines of code at the
a particular script will run immediately after the same time, you can build two different
instruction before it is finished. programs with the same event on top.
wait 2 second instruction runs wait 2 second The code will run
before the when the green
think Hmmm... for 2 seconds think Hmmm... for 2 seconds
second, and so on. flag above the
wait 1 second wait 1 second stage is clicked.
move 100 steps move 100 steps
Pixels in Scratch
Pixels are used to make up images on a screen,
but in Scratch they are also used as locations
that tell the computer where to put something. The entire Scratch stage
is divided into an area
that is 480 pixels wide
and 360 pixels tall.
go to x: 0 y: 0
go to x: -100 y: 0
77
Sprites Sprite List
When you add a sprite to the stage, you
will see it appear in the Sprite List below it.
This gives you information about the sprite
Here, we’ll show you how to add you have selected. The current sprite is
highlighted in blue. Make sure you pick
images to your projects. In Scratch, the right sprite before you start building
any code for it.
you add code to objects called
sprites to make them do things. Change the name of
your sprite here.
You can also add scenes to the
stage to make your projects Sprite cake x 0 y 0
Delete a sprite by
clicking on this cross.
cake
Pick a random
sprite.
Paint
Choose a
I can move around Create your sprite from the
the stage. own sprite by Scratch library.
drawing it using
the Paint tool.
78
Costumes Code Costumes Sounds
Add a backdrop
You can make the stage more colorful, or
set the scene for a game, by using a backdrop.
A backdrop is like a costume for the stage!
You can code the backdrop to change or
interact with your sprites.
Sprites appear
I can make things
in front of the
run away!
backdrop.
Backdrops
1
Surprise You can add or
create backdrops
cake
Add a new backdrop in the same way
from this menu. you do for sprites.
79
Algorithms
An algorithm is a list of steps that tells you how
to do something. When professional coders start
programming, they like to have a clear algorithm
so that they know what to make. In this activity,
you’ll make your own algorithm and prepare for
building your first program.
80
Wrong order Right order
This algorithm uses the right Now the steps are in the right
instructions but not in the right order. First, the cat walks
order. Here, the cat says it’s tired forward, then turns around,
before it has gone anywhere! comes back, and says it’s tired.
Start Start
End End
81
Adding detail
When an algorithm is too simple, it can miss important details.
This can lead to a misunderstanding when it’s time to code. To turn around, the cat
needs to flip from left
In order to avoid issues later, it’s helpful to add as much detail to right. If it simply
to your algorithm as you can think of so the computer does turned around it would
exactly what you expect when it reads your code. be upside down.
The program
Once you have planned out your algorithm, you need
to find the right blocks to make it happen.
when clicked
wait 1 seconds
82
Every time the cat moves Finally, the cat says it’s
think about exactly how far tired. You can choose how
you want it to go. Here it long you want the speech
moves 200 steps. bubble to appear for.
The algorithm
finishes once all
the code has run.
The cat starts by facing to the right before it The cat pauses and then turns around and walks
1 begins to walk, so it will move to the right. 2 back to the left before speaking.
83
Scratch Python
Date created: 2003 Date created: 1991
Creator: Massachusetts Creator: Guido van Rossum
Institute of Technology (MIT) Country: The Netherlands
Country: USA Text or blocks: Text
Text or blocks: Blocks
Scratch is a visual programming language. Python is a text-based language. It’s a great tool
Code is written by dragging blocks from a for teaching people to code, since it stresses
special toolbox. This is great for new coders, good habits. However, Python could not be
since commands are premade and easy to used for programming 3-D games or other
use, but advanced users might prefer a text tasks that require a lot of computer memory.
language where they have more freedom.
when clicked p r i n t ( “ H e l l o , Wo r l d ! ” )
say Hello, World!
Programming
languages
Programming languages, or computer languages,
are sets of code that computers can understand.
Each language has its good points and bad points.
Depending on what a programmer needs, they
might find that a certain language works better
than another for what they want to do.
84
JavaScript Ruby
Date created: 1995 Date created: 1995
Creator: Brendan Eich Creator: Yukihiro Matsumoto
Country: USA Country: Japan
Text or blocks: Text Text or blocks: Text
JavaScript is everywhere! It’s very fast and used Ruby is a fairly simple language that allows
by lots of applications, especially web pages! a lot of freedom and flexibility. It is very
However, it runs on the user’s computer—not on popular and great for beginners, but
an online server—so it can be less secure than because it doesn’t require a strict structure,
other languages, and can look different, bugs can be introduced in Ruby that are
depending on the browser you use. well disguised and very hard to find!
C++
“Hello, Date created: 1969
World!” Creator: Bjarne Stroustrup
It’s a tradition among Country: USA
programmers to have Text or blocks: Text
the first program say the
phrase “Hello, World!”
when testing the new
language. Some coders C++ is a complex language that is very popular
believe that they can tell with professionals because of its speed and
how difficult a language reliability. However, it’s not always the best
will be based on how language for new programmers, since it
easily they can get the requires a lot of code to do even simple tasks.
computer to say “Hello,
World!” in that language.
#i n cl u d e < s t d i o . h >
m a i n { } { p r i n t f( “ H e l l o , World ! ” ; }
85
Coding
Plan
1 The first step to making your program
is to figure out what you want to do.
In this program, we want to make a
programs
pirate say “Ahoy!,” walk across the
stage, then stop and say “Let’s walk,
matey,” before walking off the screen.
Start
3
Putting it in Scratch
Once you have created your algorithm,
all you have to do is work out which block
makes each step happen and add it to
your code! Ahoy!
The pirate
when clicked
should say
this for
say Ahoy! for 2 seconds
2 seconds.
move 50 steps
86
Can you work out
what block will let
the pirate say Let’s walk,
Ahoy! something for matey.
2 seconds?
Say “Lets
Say “Ahoy!” Move Move
walk, matey.” End
for 2 seconds 50 steps 300 steps
for 2 seconds
87
Debugging
What the
program
should do
This program will first
Sometimes we know exactly what we want move the sprite to the
other side of the stage.
an algorithm to do but our program just Then the sprite pauses
doesn’t work. When that happens, it doesn’t before hiding and
reappearing in a
usually make sense to throw all of the code completely new position.
out and start over. Instead, you can look for
errors, also called bugs, that you can fix.
The program
Try coding the blocks in step 1 and
running it in Scratch. There are a few
bugs in this program. Can you work
through the steps below to make the
program do what it is supposed to do?
Look at the next step to help you.
The sprite spins around here, which we The program moves so fast that you can’t see
1 don’t want it to do. One of the blocks is 2 the sprite move at the beginning. Wait blocks
incorrect and needs to be changed. need to be added to slow the sprite down.
88
1, 2 seconds...
The program should make the sprite move The program then hides the sprite for 2 seconds
1 across the stage. 2 before it teleports and reappears.
i
Useful tip
Ask yourself, “What is this program supposed to do?”
and then check what the program is actually doing.
Walk through the code to see where it stops working.
After the first step? Or the next? Once you find the bug,
write down a few options for how you might fix it,
then test out the different options.
The program now has all of the right blocks, The program almost works, but the sprite nearly
3 but the sprite doesn’t hide after moving 4 disappears off the stage at the beginning. The
forward because the blocks are in the wrong move value is too high! Reducing it should make
order. Can you rearrange them? the program run perfectly.
89
Why use loops?
It can be very useful to make a program that does something
over and over. Loops let you create sprites that keep changing
color or never stop moving. A forever loop is great for this.
The only way to stop this type of loop from running is to
stop the whole program.
Here, we want an octopus sprite to keep By placing the color change blocks inside a
1 changing color every second. Without a 2 forever loop, the octopus will go on changing
loop, you have to add two new blocks for color forever, without you having to add any
every color change. extra code.
wait 1 seconds
Loops
Programs become much more powerful once you start
adding loops. A loop block repeats everything inside it so
you don’t have to create the same code again and again.
One simple loop can help your program do so much more.
90
Breaking loops i
Not all loops go on forever. You can also use loops
that stop after they repeat a certain number of times,
Useful tip
or loops that will stop when you do something special. There are 199 different color effects
This is useful when you want something to keep that every sprite can be in Scratch.
changing until an event happens. A change of 25 will completely
change the color; a change of 1 will
change it only slightly. Remember,
A repeat loop with a changeable value lets you choose
1 how many times you want the code inside the block to changing the color effect will shift all
of the colors of the sprite.
repeat.If you want this monster sprite to change color
three times, you simply put 3 in the loop.
wait 1 seconds
when clicked
when clicked
go to x: 0 y: 0
Your sprite will start in the middle of the stage.
pen down This block turns your 1 The code will create a line behind it as it moves.
sprite into a pen that
can draw a line.
repeat 4
wait 2 seconds
Creativity
Programming can be creative, artistic, and fun! All
you have to do to make something special is keep
trying different ideas until you figure out how to put
it into code. Sometimes the most amazing projects
are the ones that don’t turn out as expected!
92
i
Useful tip
To remove all the lines drawn by a
sprite, click on the “erase all” block
in the Pen section of the Blocks
Palette, or add it to your program.
You choose
where the sprite
goes each time
the loop runs.
The given program
lets you move the
sprite four times.
Now try...
Can you use your program
to draw a hexagon? Can you
change it so that you can?
Now what else can you draw?
93
Collaboration
Collaboration means working together,
and it’s a powerful tool! If you get stuck, The program
Here are three scripts that will
then working with others can help you all play a rhythm on a different
come up with ideas that you might not drum at the same time. Can you
collaborate with these programs
have thought of by yourself. by adding a fourth drum?
1 when clicked
repeat 8
Copy each of these three
scripts, then click the green rest for 0.25 beats These blocks play the
flag to listen to what
drum for a set number
they sound like together. of beats.
rest for 0.25 beats
when clicked
repeat 8
94
when clicked
This block makes the rhythm
repeat 8 times. This gives you
time to listen to them together.
i
repeat 8 Useful tip
When you first open Scratch,
play drum (12) Triangle ▾ for 0.25 beats
the instruments are set to
play 60 beats per minute.
rest for 0.25 beats That’s one beat every
second. Every program here
rest for 0.25 beats repeats four quarter beats
8 times. This means each
will last for 8 seconds and
play drum (12) Triangle ▾ for 0.25 beats
that they should all start
and finish together.
Triangle
2
Choose another drum to go Use the drop-down menu
when clicked
with the existing programs, to select a type of drum.
and create a rhythm for it. repeat 8
Play your code a few times
and make changes until you play drum ▾ for 0.25 beats
are happy with how it sounds
with the other drums. rest for 0.25 beats
Now try...
Add more instruments to make your own orchestra!
You can create a tune by changing the length of
time that each plays for, or when they rest.
95
Persistence 1
Can you make the sprite
spin around to make one
Programming can be confusing, and complete circle? Look at
how far it turns on each
sometimes it’s difficult. It’s possible that repeat to help you.
erase all
The program
How many times go to x: 0 y: 0
This program can make different patterns does this loop
using a sprite. However, each one is missing need to repeat?
repeat ?
some code. Can you figure out how to make
the sprite create the patterns shown?
stamp
The stamp
To make the sprite spin from tool makes
the bottom, you need to edit a print of the
turn 60 degrees
it in the Costumes editor. sprite on
Costumes the stage.
Sprite 1 more
Group Ungroup Forward Backward
Fill Outline
This code will make the sprite spin. It also makes a stamp
Convert to Bitmap of the sprite on the stage before it moves again.
96
i Useful tip
Do you want your sprite to turn in a full circle?
Make sure that the degrees it turns and the
number of times the code repeats multiply to 360.
This code fits 12 stamps into a circle. Look what happens when the sprite
when clicked
There are 12
sprite stamps
packed in here.
This pattern has twice as many sprite stamps in the circle. In this program, the sprite spins around three times and
That means the code needs to repeat double what it did shrinks at the same time!
before, but that the sprite should turn fewer degrees.
97
Conditionals
The programs that we’ve written so far have been
fun, but they’re about to get a whole lot better!
Conditionals let the computer make decisions based
on whether a condition is true or false, which lets
you create programs that can change.
Condition blocks
are turquoise.
Types of conditional key space ▾ pressed?
These orange blocks are conditionals.
You add a turquoise “condition” block if then repeat until
to them so that the computer knows
whether or not to run the code inside.
If the condition is true, the computer
will do one thing. If the condition is
false, it will do something else, or The “repeat until” conditional
An “if then” conditional will
nothing at all. only run the code inside it if the will repeat code inside it until the
condition attached to it is true. condition attached to it is true.
What is a condition?
A condition is a statement that can either be true or false. A sprite can collide
For example, if you press the space bar on your keyboard, with another sprite or
the edge of the stage.
then the condition “key space pressed” is true. In Scratch,
conditions are turquoise blocks with pointed ends.
If your sprite is touching a color of your choice, If you click your mouse, the “mouse down”
then the “touching color” condition is true. condition becomes true.
98
The program
This program challenges you to press the space bar
when you think the crab is touching the edge of the
stage. Take a good look and see if you can identify
the conditionals and the conditions in the code.
wait 1 seconds
Each time the code
runs, this block picks a
random position for the
sprite to appear in.
if touching edge ▾ ? then
This condition asks
say You win! for 2 seconds whether the sprite is
touching the edge of the
stage, but the program will
never get to it unless the
space bar is pressed.
If the sprite is touching the edge,
then the “touching edge?” condition
is true and the sprite will say this.
Otherwise, the program will end.
You win!
99
If/else
It’s great to be able to run a set of code if a
condition is true, but it’s even better to be able to
run a different set of code if it’s not! That’s why we
have if/else blocks. An if/else block is a type of
conditional that will let you tell the computer what
to do whether your condition is true or false.
The program
Here is some code that will make a sprite
dance whenever you press the space bar, by
flipping it from left to right. A “forever” loop
makes the “if then else” block keep checking
to see if the sprite should flip. when clicked
go to x: 0 y: 0
100
The “if then else” if then
block A condition can
Any code added here
will run only if the
The “if then else” block is a be added here. condition is true.
conditional, but unlike the “if then”
or “repeat until” conditionals, it holds
else
two sets of code. This means you
Any code added here
can ask the computer to do different will only run if the
things depending on if your condition is false.
condition is true or false.
i
Useful tip
Don’t forget to add code to position your sprite
properly at the beginning of each program. If you
don’t, you might have settings left over from
previous code that makes the sprite do
something that you didn’t want.
Your sprite will start by facing to the right, but If the space bar isn’t pressed, the sprite will stay
1 if you press the space bar it will flip to the left. 2 facing the right. If you keep pressing and releasing
the space bar, the sprite will dance on the spot.
101
Events
Up to this point, we’ve only started actions when
the green flag is clicked. However, that is only one
type of event used to trigger a piece of code. You can
use different events to make a game more fun by
starting actions while a program is already running.
Types of event
Event blocks make certain sections of code
run when something special happens. For
many programs, the code runs when the
green flag is clicked, but code can also be
run after a set amount of time, when you
click on a sprite, when you press a certain
key, or when another event happens.
when clicked
When you click on a certain sprite, this event When a timer gets above 10, or another value of your
block runs the code attached to it. choice, the code connected to this block will run.
The code connected to this block will run only This block will run the connected code when the backdrop
when the space bar is pressed. of your program is coded to change to a different picture.
102
This event block starts the dog The program
running around the screen.
You can use more than one event block in
the same program to make different things
when clicked happen. Here’s the code for a game where
you have to catch a running dog or make it
forever bark. Can you figure out how to play?
go to x: 0 y: 0
stop all ▾
103
Input/ Permissions
This program requires the use of a webcam.
output
Scratch needs your permission to use input
from your webcam. When you click on the
Video Sensing option, a pop-up window will
ask you to allow or block it. You will only be
able to try this program if you click allow.
104
2
The script here lets you control the second
sprite with movement. The sprite will move i
in the direction of the motion on the webcam. Useful tip
If you find you’ve written your
Make sure you’ve clicked code for the wrong sprite, don’t
on the Sprite 1 icon before
worry—you don’t have to write it
creating this code.
Sprite 1
all again. Drag the section of code
you’ve already made onto the
correct sprite icon underneath the
when clicked
stage and those blocks will be
go to x: 0 y: 0 copied over to that sprite.
set size to 80 %
Sprite 2 will keep moving around the stage. You Try to make Sprite 1 catch Sprite 2. Once it has,
1 move Sprite 1 by creating motion on the webcam. 2 the program will end.
105
Collisions Make a collision
Collisions are recognized in
Scratch by “touching” condition
blocks. If you want to check
whether two things are colliding
When a sprite runs into the edge of at a specific moment, or if you
the stage, or another sprite, that’s a want to have something happen
any time two things collide, put
collision! A collision happens whenever the “touching” block inside a
conditional block.
two objects touch. In some games, you
touching edge ▾ ?
want two objects to collide, but in
others you want to avoid collisions! This condition asks if
the sprite is touching
the edge of the stage.
106
2
This code lets you control the princess,
but also makes her say “Ouch!” or “Oops!” i
if she collides with a dragon or with the Useful tip
edge of the stage.
In order to select another sprite from the
Select Sprite 2 before drop-down menu in the “touching” blocks,
creating this code. they must already be added to the Sprite List.
Sprite 2 Make sure you add all the sprites you want
before you start writing your code.
when clicked
show
The code sets Sprite 2’s
point in direction 90 Sprite 2 starts on the
left of the stage. size to 80%. You can play
around with that number
go to x: -100 y: 0
to see what size is the
most fun!
set size to 80 %
This line adds gravity
to your game, making
forever Sprite 2 fall a little all
the time.
change y by -3
hide
hide
stop all ▾
Oops!
Variables Variables
Motion Motion
Make a Variable Go to the Make a Variable
Looks my variable
Variables Looks my variable
section and click
Sound set my variable ▾ to 0
on the Make a Sound score
Variable button.
Events change my variable ▾ by 1 Events set my variable ▾ to 0
108
The program when I start as a clone
In this game, we’ll make hearts keep
set size to 30 %
falling down. A variable will make the
score go up if a llama catches them
go to x: pick random -150 to 150 y: 150
or down if the llama misses them.
forever
This code makes a heart sprite Choose Sprite 2 from
go to x: 0 y: -100
point in direction 90
score 5
This code will
move Sprite 2
when right arrow ▾ key pressed right when you
click the right
move 15 steps arrow.
109