100% found this document useful (3 votes)
950 views

Scratch Basics

Scratch is a block-based programming language where code blocks of different colors are dragged into the scripts area and snapped together to create scripts. The blocks are categorized by color based on their function, such as motion, looks, sound, events, control, sensing, operators, variables, and custom blocks called My Blocks. Dragging blocks from the palette into the scripts area builds scripts, and dragging them back deletes them.

Uploaded by

Topson Ngangom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
950 views

Scratch Basics

Scratch is a block-based programming language where code blocks of different colors are dragged into the scripts area and snapped together to create scripts. The blocks are categorized by color based on their function, such as motion, looks, sound, events, control, sensing, operators, variables, and custom blocks called My Blocks. Dragging blocks from the palette into the scripts area builds scripts, and dragging them back deletes them.

Uploaded by

Topson Ngangom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Using Scratch

Scratch is a block-based language that lets you


program by stacking blocks of code together. Find
out here what the different parts of Scratch do.

Change the Menu options


Expert tips language here

Some options are hidden in


menus. Take some time to File Edit Tutorials
explore them so you know
where to find things.
Code Costumes Sounds

Load, save, or Motion


File start a new Motion

project. move 10 steps


Looks

turn 15 degrees when clicked

Restore a project Sound


forever
Edit or turn on “turbo turn 15 degrees
go to mouse pointer ▾
mode” to make Events
go to random position ▾
code run faster. move 10 steps
Control
go to x: 0 y: 0
Learn to make
Sensing
Tutorials amazing things. glide 1 secs to random position ▾

Operators glide 1 secs to x: 0 y: 0 Snap blocks together


in the Scripts Area
Variables point in direction 90 ▾ to make scripts. Use
Add extension
your mouse to move
blocks to the
My Blocks them around.
Blocks Palette.

Drag blocks from the Drag blocks back into


Blocks Palette into the Blocks Palette to
Costumes Edit how your
the Scripts Area to delete them.
sprite looks.
build scripts.

Sounds Add, delete, or


edit sounds.
Add extensions Select different types
here. of coding block from
the Blocks Palette.

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

Preview of current Go and stop


sprite selected buttons

Change the
screen layout.

Make the stage


full screen.

Programs run
on the stage.

Position of the
current sprite
selected on the stage.

Sprite Sprite 1 x 0 y 0 Stage


Preview of current
Size 100 Direction 90 backdrop selected.

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.

move 10 steps say Hello! for 2 seconds


My super senses tell
Motion
Motion Looks me this is an event!
Blocks that make your sprite move, Purple Looks blocks affect the way a
Looks
turn, glide, or point in one direction are sprite appears. This includes adding
blue, and found in the Motion section. speech bubbles and costume changes.
Sound

Events
play sound meow ▾ until done when clicked wait 1 second

Control Sound Events Control


Sound effects and volume blocks Event blocks are yellow and have a Loops, stops, pauses, and clones
Sensing are pink and listed as part of the domed top. They start code running. are all Control blocks, and they
Sound category. are orange.
Operators

Variables touching mouse-pointer ▾ ? pick random 1 to 10 set my variable ▾ to 0

Sensing Operators Variables


My Blocks
Sensing blocks are blocks that check Green Operator blocks perform Variables are dark orange. You
what your sprites are doing. They are operations such as calculations, or can create Variables according
turquoise and pointed at the ends. combine Sensing blocks. to what you need.

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.

Select Music to add Click on Pen to be able Select Video Sensing


music to your code. to make your sprite draw. to use your webcam.

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.

when clicked The first when clicked

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

The Scratch stage is divided into


y
square pixels. Under the Motion
category, you’ll see blocks that The “y” location tells
have a spot for both x and y Scratch where to
locations. If you want to move place something
from top to bottom.
a sprite across by one pixel, you
put “go to x:1.” If you want to In Scratch, ”0, 0”
move a sprite up by one pixel, is in the middle.

you put “go to y:1.” Negative x


values move your sprite left or
The ”x” location tells
down. The center of the stage Scratch where to place
is where x is 0 and y is 0. something from side
to side.

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

more interesting. Size 100 Direction 90

Delete a sprite by
clicking on this cross.
cake

What are sprites?


Sprites are the main characters of every
Scratch project. They can be programmed
to move, interact with other sprites, and
change appearance. Scratch provides lots
of ready-made sprites, but you can also
draw your own. Here are some ideas! Add sprites
You can add more sprites to your project using
I can hide and reappear!
the menu below, which appears in the bottom
right of the Sprite List. Choose a sprite from
the Scratch library, create one yourself, or have
the program choose one for you at random.

Upload a sprite from


I can dance. your computer.

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

Each sprite can have 1


Costume cake-a
multiple “costumes” that it Group Ungroup Forward Backward Front Back

can switch between. These cake-a


Fill Outline 2
Copy Paste Delete Flip Flip
costumes might show the 100X100 horizontal vertical

sprite in a changed position, 2


or make it look completely
different. However, it will cake-b
keep the same name. You 100X100

can create new costumes for


sprites using the Paint tool.
T

The two costumes here make


the candles on the cake look
like they have been blown out.

Each sprite can be as wide Convert to Bitmap


and high as the stage—
480 pixels by 360 pixels.

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.

Sprite cake x 0 y 0 Stage


Your backdrops
will appear in the
Size 100 Direction 90 Stage List.

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.

Getting the order right


You might know what you want a program to do, but if you
put the steps in the wrong order, it won’t work. Take a look
at this example to see how the sequence can be important
to an algorithm. What order would we put these instructions
in if we want our cat to walk, turn around, come back, then
tell us that it’s tired?

All of your instructions Time for my walk!


should happen before
your algorithm ends.

End Cat walks


forward
Cat turns
around Cat walks
forward

Cat says “I’m Start


tired from all
that walking!”

The start to your program


is the event that begins
When in the sequence the code running.
do you want the cat to
say this?

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

Cat says “I’m The first thing the


Cat walks
tired from all cat does is walk.
forward
that walking!”

The algorithm The cat needs to


Cat walks should start with Cat turns turn around before
forward the cat walking. around it walks back.

Now the cat has


Cat walks walked too far in Cat walks
forward one direction. forward

The cat turns around The algorithm ends


Cat says “I’m after the cat has
Cat turns at the end, but
doesn’t get a chance tired from all walked forward and
around that walking!” back, then says
to come back.
that it’s tired.

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.

Cat Cat Cat flips


Cat walks
Start waits for waits for from left
200 steps
1 second 1 second to right

Think about how to get the


cat positioned properly at If the cat didn’t wait for a
the start of the program. second, the code would run
so fast that you wouldn’t
see it move.

The program
Once you have planned out your algorithm, you need
to find the right blocks to make it happen.

when clicked

go to x: -100 y: 0 The first two blue blocks


make the cat start in the
point in direction 90 same position and point
in the right direction every
time the code runs.
set rotation style left-right ▾

wait 1 seconds

move 200 steps


This block will
wait 1 seconds
make the cat
face to the left.
point in direction -90

wait 1 seconds Here you can set


how long you want
the cat to talk.
move 200 steps

say I’m tired from all that walking! for 2 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.

Cat Cat says “I’m tired


Cat walks
waits for from all that walking!” End
200 steps
1 second for 2 seconds

The algorithm
finishes once all
the code has run.

The speech bubble will


appear for 2 seconds
and then disappear.

I’m tired from all


that walking!

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!

Type your phrase In Python, you must tell


into the “say” block the program to print your
and Scratch will have command, then it will
your character say it! show up on-screen.

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!

a lert (“He l l o, Wor l d !”) ; p u t s “ H e l l o , Wo r ld ! ”

This command will get This command is just


JavaScript to show your like “print” in Python.
phrase in a pop-up box It tells Ruby to show
in your browser. your phrase on-screen.

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.

You have already learned that an


algorithm is a list of steps that tells
you to do something. Programming
is “translating” that algorithm into
code. A clear algorithm will help
you prepare for writing your This tells us where our
program starts. If it’s
first program. easier, you can stack
your instructions from
top to bottom instead
of left to right.

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

say Let’s walk, matey. for 2 seconds

move 300 steps 1 The pirate says “Ahoy!” for 2 seconds.

86
Can you work out
what block will let
the pirate say Let’s walk,
Ahoy! something for matey.
2 seconds?

How will we get


the pirate to
walk this way?

Figure out the steps


2 Next, you need to figure out what steps are needed
to make your plan happen. One of the easiest ways
is to draw this out using a flowchart. You should
Link each of your instructions
think about how long you want the pirate’s words to the action that happens
to show for and how far you want him to move. just before and the action
that should happen after.

Say “Lets
Say “Ahoy!” Move Move
walk, matey.” End
for 2 seconds 50 steps 300 steps
for 2 seconds

Let’s walk, matey. The last instruction moves


the pirate so far that he
almost leaves the stage!

2 Next, the pirate walks 50 steps, then says


“Let’s walk, matey.” for 2 seconds.
3 Finally, the pirate walks 300 steps, to the other
side of the stage.

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.

when clicked when clicked

turn 15 degrees We don’t want move 1000 steps


the sprite to turn.
We want it to
go to random position ▾ go to random position ▾
move forward, so
this block needs
hide hide
to be changed. You can add wait
blocks after the
show show
sprite has moved.

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.

when clicked when clicked

move 1000 steps move 100 steps Reducing the number


of steps the sprite
wait 2 seconds wait 2 seconds takes will keep it on
the stage.
go to random position ▾ hide

wait 2 seconds wait 2 seconds

hide The sprite is hiding at go to random position ▾


the end, rather than after
show it has moved forward. show

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.

A color change of 25 will This block tells


completely change the the computer
color of the sprite. that everything
when clicked when clicked inside it should
keep happening,
change color ▾ effect by 25 forever forever.

wait 1 seconds change color ▾ effect by 25

change color ▾ effect by 25 wait 1 seconds

wait 1 seconds

change color ▾ effect by 25


When the computer gets to
this part, it knows to go back
wait 1 seconds Don’t forget the wait
to the beginning of the loop
block. Without it, you
and start again.
won’t get to see the
individual color changes.

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.

This loop looks a lot like


when clicked the first one, but it will
stop after the color has
repeat 3 changed 3 times.

change color ▾ effect by 25

wait 1 seconds

The colors of the sprites will


change by the same amount
If you want a loop to keep repeating until an event happens, each time, but the number
2 you can use a repeat until loop. Here, the monster sprite will of times the color changes
keep changing color until the space bar is pressed. depends on the loop you use.

when clicked

repeat until key space▾ pressed?

change color ▾ effect by 25 The loop will stop when this


condition of the space bar
wait 1 seconds being pressed is true.

Now try... repeat 10


repeat 4
What happens when you put
a repeat loop inside another
repeat loop? Play with it and
see! Can you make a sprite keep
jumping to new positions but
have it change color a few
times before it jumps again?
91
The program
The program below turns this unicorn sprite
into a pen! Build the code below and see what
you can create. Can you draw a square?

when clicked

set rotation style all around ▾


Remember “0, 0” is
erase all always in the middle.

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

point toward mouse-pointer ▾ Put your mouse


here and the sprite
will turn toward
go to mouse-pointer ▾
that point and
move there.
This block makes the
sprite move to the cursor.
pen up You will need to select
“mouse-pointer” from the
drop-down menu.

Your sprite should turn toward your cursor


2 and draw a straight line in that direction.

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.

Get creative and use your sprite to draw different shapes.


3 How many shapes can you make with the program?

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?

If you can change the


program to let you
draw a hexagon, you
can draw a star as well.

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

play drum (13) Bongo ▾ for 0.25 beats

play drum (13) Bongo ▾ for 0.25 beats

Scratch lets you choose


Bongos from 18 different drums.
This code uses bongos.

when clicked

repeat 8

play drum (1) Snare Drum ▾ for 0.25 beats

play drum (1) Snare Drum ▾ for 0.25 beats

rest for 0.25 beats


No sound will play during the
Snare rests. These sections will be
drum rest for 0.25 beats silent while the other drums play.

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

play drum ▾ for 0.25 beats

rest for 0.25 beats You could alternate


beats and rests.

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.

set instrument to (1) Piano ▾ Scratch has 21


instruments to choose
from on the “set
play note 60 for 0.25 beats instruments” block,
including a piano.

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.

your code won’t always work perfectly the


first time you run it. You might have to Replace the code
inside Sprite 1
debug it several times before it does Sprite 1 each time.

what you want it to, but that’s okay.


when clicked
Just keep trying!
set size to 90 %

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

2... Flip Flip


Copy Paste Delete horizontal vertical

T Move or draw the sprite


so the circle with a
cross is at the sprite’s
bottom center point.

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

2 Can you figure out how far the sprite


should turn each time? 3 shrinks as it spins! Can you re-create this
effect? Keep persisting until you can!

when clicked

when clicked set size to 90 %

set size to 90 % erase all


If you want 12 sprite
erase all go to x: 0 y: 0 stamps in each circle,
how many repeats do
go to x: 0 y: 0 repeat ? you need overall?

repeat 12 Keep trying until you stamp


figure out what this
stamp number should be.
turn ? degrees

turn ? degrees ? What do you think


this block should be?
The color will give
you a clue.

The sprite makes


three full circles
as it spins.

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.

key space ▾ pressed? touching edge▾ ?


The “key pressed” condition is true if certain The “touching” condition is true if the sprite is
keys on the keyboard are pressed. touching the cursor, another sprite, or the edge
of the screen.

touching color ? mouse down?

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.

This condition means the


code in the conditional will
when clicked keep running until the
space bar is pressed.
go to x: 0 y: 0

repeat until key space ▾ pressed?

go to x: pick random -200 to 200 y: pick random -200 to 200

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!

1 The sprite will move randomly around the stage


until you press the space bar. 2 If you stop the loop while the sprite is touching
the edge, 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

The “forever” loop around the point in direction 90


“if then else” block makes the The “if then else” block
program keep checking whether set rotation style left-right ▾ checks if the condition
the space bar is pressed. “key space pressed?” is
true or false.
forever

if key space ▾ pressed? then


If the “if then else”
block finds the “key space
pressed?” condition true, the point in direction -90
sprite will face to the left.
else
If the “if then else”
point in direction 90
block finds the “key
space pressed?”
condition false, the
sprite will face to
the right.

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

This block lets you start running the code


connected to it by clicking the green flag
on top of the stage.

when this sprite clicked when timer ▾ > 10

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.

when space ▾ key pressed when backdrop switches to backdrop1 ▾

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?

wait 0.5 seconds

go to x: pick random -100 to 100 y: pick random -100 to 100

The dog will appear


in random positions
around the stage.

This event block will make the dog


bark when you press the space bar.

when space ▾ key pressed


Woof!
say Woof! for 2 seconds

This event block makes the dog say


“You got me!” when you click on it.

when this sprite clicked


The sprite will run around until you press the space
say You got me! for 2 seconds 1 bar. This will make it bark.

go to x: 0 y: 0

stop all ▾

Now try... You got me!


Once you figure out how the game works,
change the speed that the sprite moves around
to make it harder. Make the game extra special
by adding code so the background changes If you click on the sprite, it will say “You got me!”
when the up arrow is pressed! 2 and the game will end.

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.

When you click on a sprite


x
using your mouse, or press a Scratch.mit.edu wants to
key on your keyboard, you are Use your camera

providing your computer with


Block Allow
input. These inputs affect the
outputs from your screen or
speakers. You can even use
input from a webcam to make
The program
your programs interactive! In this program, you try to catch Sprite 2
using Sprite 1, but you control the sprite
by waving your arms around!

This script will make a sprite move

1 around the screen until it touches


the other sprite.

This will change the size of your


sprite. You can adjust it to make
when clicked it bigger or smaller.
Sprite 2
set size to 80 % Once you have created a
Make sure you’ve clicked second sprite, there will be
on the Sprite 2 icon go to x: -200 y: -200 an option in the drop-down
before creating menu here to select Sprite 1.
this code. repeat until touching Sprite 1 ▾ ?

turn pick random -2 to 6 degrees

move 8 steps This block will make


your sprite move
around the stage.

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 %

turn video on ▾ This block will let you see yourself


on the stage, but slightly faded so
you can still see the sprites, too.
set video transparency to 50

repeat until touching Sprite 2 ▾ ?

point in direction video direction ▾ on sprite ▾

move 5 steps This block makes


Sprite 2 turn toward
any movement on
the stage.
say You got it! for 2 seconds

Don’t forget to add code to


turn video off ▾
turn the camera off once
Sprite 1 catches Sprite 2.

You got it!

Sprite 2 will move


around the stage
constantly.

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.

The program This code makes a dragon


In this game, a princess has to avoid a horde of
dragons! Can you help her escape them? First, we 1 move across the screen until
it hits the edge of the stage.
need to make a flying dragon that copies, or “clones,” The dragon then deletes
itself when it goes off the edge of the stage. That itself, but makes a new
way we don’t have to keep making new sprites. dragon start at the beginning.

Make sure you select Sprite 1


before you create this code. If you The code attached to this
draw your own, it might be useful when I start as a clone control block will run for all
Sprite 1
to label it “Dragon” so it will be the clones of the dragon.
easier to select later. show

go to x: 200 y: pick random -100 to 100

when clicked forever These blocks will make each


sprite start at a random height
hide on the right of the stage.
move -6 steps
create clone of myself ▾
if touching edge ▾ ? then
This block will create
a clone of Sprite 1. create clone of myself ▾

delete this clone

This block deletes the clones


that fly away so that they don’t
pile up at 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

if touching Sprite 1 ▾ ? then Ouch!

say Ouch! for 1 seconds

hide

stop all ▾ If the princess collides


with the dragon, the
game is over.

if touching edge ▾ ? then

say Oops! for 1 seconds

hide

stop all ▾

Oops!

The game will also


end if the princess
when space ▾ key pressed collides with any of
the stage’s edges.
change y by 40 This bit of code moves
Sprite 2 toward the top
of the screen. You need
to keep pressing the space
bar to stop her from falling.
107
Variables
Sometimes you need to keep track of things that
change in a program or game. You can do this using
variables. A variable is a placeholder that can be
used for a value, even if the value changes while
the program is running. Variables are really useful
for keeping score in a game.

Create a variable block


You can create your own variables in Scratch. Here’s how
to create a variable to keep track of the score in a game.
You’ll find what you need in the Variables section.

Code Costumes Sounds Code Costumes Sounds

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

show variable my variable ▾ change my variable ▾ by 1


Control Control

hide variable my variable ▾


Sensing
Your new variable will
now appear in the list. You can set the value of your
Operators
variable using the “set to”
New Variable block. Select your variable
Variables from the drop-down menu.
New variable name:
My Blocks A box will pop up and
score you will need to name set score ▾ to 0
your variable “score.”
For all sprites For this sprite only
Make sure “For all
sprites” is selected.
my variable
More Options ▾
score
Rename variable
Cancel OK
Delete the “score“ variable

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

1 move across the stage. It will


add 1 to the score if it touches
change y by -8 the drop-down menu.

if touching Sprite 2 ▾ ? then


the llama or -1 if the heart gets
to the bottom of the stage.
change score ▾ by 1 This block will add
a value of 1 to your
Sprite 1 create clone of myself ▾ variable every time
Sprite 2 touches
When you delete this clone Sprite 1.
start the
when clicked program, the
score will be
set score ▾ to 0 set to 0.
if touching edge ▾ ? then
show
change score ▾ by -1 This block will
create clone of myself ▾ minus a value of 1
create clone of myself ▾ from your variable
hide every time Sprite 2
delete this clone misses Sprite 1.

These blocks of code let you

2 move the llama left and right.

when clicked Your variable “score” will


Sprite 2
appear in the top left of
set size to 55 % the stage.

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.

This code will


move Sprite 2
when left arrow ▾ key pressed left when you
click the left
move -15 steps arrow.

109

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy