Coding Games From Scratch
Coding Games From Scratch
SCR ATC
by Rachel Ziter
FROM
SCRATCH
All rights reserved. No part of this publication may be reproduced in whole or in part, or stored in a retrieval system, or transmitted in
any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission of the publisher.
Photo Credits
Shutterstock: AlexZaitsev, Cover, Kotkoa, Cover, Phil’s Mommy, 6
“Scratch is a trademark of Massachusetts Institute of Technology, which does not sponsor, endorse, or authorize this content.
See scratch.mit.edu for more information.”
Introduction
What Is Coding? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What Is Scratch? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Space Muffins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Coding Extras
Read More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Makerspace Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Internet Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Coding Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3
What Is Coding?
Playing with an app on your smartphone. Clicking through a website. Without even
realizing it, you’re using coding. Coding is the language used to communicate with a
computer. By creating a set of code, you’re writing directions in a language that the
computer can follow. Although computers may seem super smart, that’s not the case!
The only reason computers know how to do anything is because they have been
coded to do it. A computer’s code—the very specific directions given by a person—
allows it to be the super-smart device we all know and love. The reality is, anyone
can learn to code. In this book we’ll be creating projects using one coding language
in particular: Scratch.
What Is Scratch?
Scratch is an online coding platform that uses colorful coding blocks to create
everything from games to presentations to animation. The colored blocks are sorted
into categories like Motion, Looks, and Sound. By connecting the colorful blocks,
you can start coding whatever comes to mind. For example, if you want to code a
character to move around and make noise, you would start with an Events block, then
add a Motion block, and finish with a Sound block. (You can also use a Control block
to make the events repeat as many times as you’d like.)
Scratch runs on Adobe Flash Player, so make sure your software is up-to-date.
To download and install Flash, go to: https://get.adobe.com/flashplayer/
TI P:
iculty.
The projects in this book build in diff
with the
If you’ve never coded before, start
thr ough. If
first project and work your way
a later
something doesn’t make sense in
jects
project, try going back to earlier pro
to find the answer.
4
Creating a
Scratch Account
To create the projects in this book, you will need a Scratch account. To
get started, go to: www.scratch.mit.edu. In the upper right corner, click
the Join Scratch button.
A window will pop up and ask you to create a Scratch username and
password. Pick a password you can remember.
The next window will ask for your birth month/year. This is just to make sure
you are old enough to use Scratch. If you are younger than 12, you’ll need
a parent’s email to get permission.
5
The next window will ask for an email address. Scratch will send one email—to
confirm your email address—when you sign up. After that, you’ll only get emails
if you need to reset your password.
6
When you click create, your screen will look like this:
TOOLS
These tools are found at the top of the screen. They are helpful for creating new projects. Click on the
tool you want to use—it will turn blue and the mouse will turn into the tool. Then click on the item you’d
like to duplicate, cut, grow, or shrink.
stamp—The stamp is used to duplicate anything in your project. To use this tool, click
on the icon so the cursor turns into the stamp, then click whatever you’d like to copy.
You can click on a premade character or even a set of code.
outward arrows—The arrows facing outward are used to grow characters. Continue
clicking on the character until it is the desired size.
inward arrows—The arrows facing inward are used to shrink characters. Continue
clicking on the character until it is the desired size.
7
WHAT IS A SPRITE?
A sprite is any moveable character or object used in a project. Sprites
can be selected through the Scratch Library, created using drawing tools,
or uploaded from the computer. Scratch Cat is an example of a sprite!
Sprite Library
8
SPRITE TOOLS
alien head—Click to open the Sprite Library and select a sprite. All
sprites are sorted alphabetically. You can choose anything from a
dinosaur sprite to cheesy puffs to an airplane.
If a sprite is flipping
upside down, change
its rotation style here.
9
When you have selected a sprite, you will see three tabs in the top right corner:
Scripts, Costumes, and Sounds.
SCRIPTS TAB:
The Scripts tab is where you will create the code for all your projects. When you click on the
Scripts tab, you will have access to the different code blocks needed to create projects.
Motion:
These blocks are used to create movement. Using these blocks, you can tell your
sprite to move around the screen, go to a particular place, turn, and more.
Looks: Here you will find the code needed to make your sprite or project change colors,
grow, shrink, swap backgrounds, switch costumes, and much more! You can even
code your sprite to say or think certain things. (When you code it with a say block, a
speech bubble will appear above the sprite. The say and think blocks are here rather
than in Sound because your sprite won’t actually make any noise with these two
blocks.)
Sound: Turn up the volume! The blocks in this category add sound to your sprites and/or
background.
Pen: These blocks allow your sprites to draw lines wherever they move. (For example, if
your sprite moves, then turns 90 degrees four times, you can create a square.) The
size, color, and shade of the pen can also be programmed here.
Data: Here you can create variables to use within a project. A variable is a value that can
be changed throughout the course of a project. (For example, you can use a variable
for the number of lives a sprite has in a game.)
10
Events: These are your start commands. All code has a start command. This tells the
program when it needs to start. These blocks will be the first piece used in any code
you write. The most commonly used start command in this book will be the green flag.
Control: These blocks control how long certain things happen and if one thing causes
another to start. There are repeat loops, wait commands, cloning blocks, and if then
statements called conditional statements. (For example, if a sprite touches a certain
color, then it needs to react in a certain way.) The if then conditional block will be one
of the most used in this book.
Sensing: These blocks are used to detect things—like touching a certain sprite or color—in
your code. They are often paired with the if then conditional block from Control. (For
example, “If touching color blue, then the sprite jumps three times.”)
Operators: These code blocks are used to combine codes or set a random range for something
within a set of code. They will always be combined with other code blocks when used.
More Blocks: You won’t see any blocks in this category at first—that’s because you must create any
blocks that go here. It can be helpful to create a block when you need to use a big
piece of code repeatedly in a set of commands.
In Scratch, code blocks snap together like puzzle pieces. Simply drag the blocks
together to make them attach. The code you create will run in whatever order
you place the blocks. To take the blocks apart, pull from the bottom and down. If
you remove a single piece, all the blocks attached below will stay connected to
that piece. (You must pull each one off from the bottom.) To throw away a block
you no longer want or need, drag it back to the category you originally selected
it from and let go.
The code block on the right
starts with the green flag
being clicked. (This is the start
Events Events
command.) Next the sprite will
Looks
Motion say “Hello!” for two seconds.
Motion
Once the two seconds have
passed, the sprite will move
10 steps.
11
COSTUMES:
Here you can edit a sprite’s appearance. You can also create your own sprite, or
add a new costume to an existing sprite. Different costumes can be used to make
it look like a sprite is moving. (Some sprites—like Scratch Cat—automatically
come with more than one costume.) Multiple costumes are key to making your
sprite look animated. Keep in mind that while you may have multiple costumes,
there is still only one sprite!
You can name your When you open the Costumes tab, you will see
costumes here. tools you can use to customize your sprite.
12
SOUNDS:
Once you’ve started a project, you can add sounds to your creation. To add a sound
to a project, first select the sound from the library. You will later add it into the
project through coding.
Each sprite comes with its own sound. Scratch Cat’s sound is meow. Other sprites
usually come with simple sounds like pop. Sprites that are imported or created using
the graphic design tools do not have any sounds attached. To add a sound from the
library, click on the speaker button.
SOUND LIBRARY:
You can search for sounds easily by using the categories on the left
side. The sounds within the library are sorted alphabetically to make
them easier to find.
13
BACKDROPS:
Just like with sprites, there are lots of ways to access backdrops in Scratch
and make them your own. You can select, create, upload, or snap a picture. The
buttons used to create a new backdrop can be found on the bottom left corner of
your screen, under the sprites section. There are four buttons you will use:
camera—This icon lets you take a picture from your computer and
use it as a background. (Note: When you click the camera, a pop-up
box will ask to access the camera. Press allow to use the camera to
create a backdrop.)
14
Race Game
HOW TO PLAY
Move your car sprites until one of them reaches the colored finish line at the end. You’ll
also learn how to edit your sprites’ color and create your own background.
TI P:
Don’t forget to name
and save your project!
STEP 2: Click on the paintbrush icon under New backdrop to OPTION: You can change the
create your race backdrop. Name it race in the upper color of your race car using
left-hand corner (on the opposite side of the screen). the paint bucket icon on the
Costumes tab. Select the color
you want from the color grid at
the bottom of the page.
paintbrush
15
STEP 3: Use the drawing tools to create a
race track with three lanes. When
finished, it should look like this:
First use the paint bucket to fill in the background with green.
Use the line tool to add one yellow line down the middle
of each lane. Hold shift to make the line straight. (You can
change the width so the line is thicker.)
Use the rectangle tool to create a red finish line at the end of
the race. (Select the filled-in option and add it to the screen
vertically, in the opposite direction of your lanes.)
Use the text tool to type Finish Line and add the text box to your rectangle at
the end. (Make sure your font is a different color than your finish line.) You will
need to rotate the text box at the top so it sits vertically on top of the finish line.
16
Last, but not least, use the paint bucket to make the sky above your race track blue.
STEP 4: N
ow that your car sprite and race track are created, you can
start adding code. Click on the car sprite, then add the code
shown here to its Scripts tab.
TOUCHING COLOR
To select the desired code for the color-sensing block, click inside the square. A small white
finger cursor will appear. When the finger cursor appears, move the cursor to the color
you’d like to select and click on it. The color you selected should now fill the square in the
color-sensing block.
17
STEP 5: Duplicate your car sprite twice using the stamp tool at the top of the
screen. At the end of this step, you should have two exact copies of
your sprite, along with the code that you programmed onto it.
STEP 6: G
o to the Costumes tab for the two new sprites you created and
change their colors so they’re no longer identical. (You want three
separate cars so you can see which one wins the race!)
T IP :
get closer to
Use the zoom symbol to
ke it easier to fill
the sprite. This will ma
door handle.
in small areas, like the
18
STEP 7: Arrange the sprites on the background to the left of your screen.
Then click the green flag to play and see which car wins!
T IP :
Click on the blue square
in the upper
left corner of your scre
en to see your
finished game in full-scr
een mode.
19
Monkey Jumping
on the Bed!
HOW TO PLAY
For this game you’ll be working with the animal kingdom! Create and code a monkey
sprite to move around randomly until he is caught. If he’s caught, the backdrop will
switch to a game over screen.
STEP 2: In your backdrop panel, click on the mountain landscape icon and
choose the bedroom background. This will automatically be saved
as bedroom1.
landscape
STEP 3: Go to your backdrop tools and use the paintbrush to create a new
backdrop. (This will be your game over screen.)
paintbrush
20
Use the paint bucket tool and, on the bottom right, select
two colors to fill in the background with the dual-color
effects. (Click on your blank backdrop to fill it with color.)
STEP 4: T
ime to add sound! Select your monkey sprite. Then go to
the Sounds tab.
Click on the speaker icon to open the Sound Library and select the
rattle sound effect. (Remember, sounds are sorted alphabetically to
make it easier to find them.)
tab, along with pop—the sound sound on one sprite doesn’t mean it
most sprites. If the rattle sound sprite in your projects. Sounds must
isn’t there, go back into the be selected for each sprite that
21
STEP 5: Under the Scripts tab, add this code to your monkey sprite.
If you can’t find a background called will play, and the game will stop.
winner, go back to your backdrops tab
and be sure that you named it correctly.
22
Save Scratch Cat!
HOW TO PLAY
Code Scratch Cat to fall from a random spot in the sky in hopes that a bat will catch
him and carry him to safety. If Scratch Cat falls without the bat catching him, the
game is over. If the bat catches Scratch Cat and brings him to the safe button, he
is saved!
paintbrush
23
STEP 2: Use the paintbrush to create two new backdrops. You’ll
need a winner backdrop and a game over backdrop.
Use the paint bucket to fill in both backdrops with your desired color(s).
Use the text tool to type your winning and losing messages on the
correct backdrops.
STEP 3: L eave Scratch Cat on the project; you will be using him as a
sprite. Open the Sprite Library and select a bat sprite and a
button sprite. (You can find these in the Animals and Things
categories.) You should now have three sprites. In the button
sprite’s information section, name it safe button. Sprite Library
24
STEP 4: Click on the button sprite, then open the Costumes tab to customize
it. Use the text tool to add the word safe to the button sprite. Feel
free to change the button’s color using the paint bucket. Once you’re
finished, drag it to the safe spot of your choice in your game!
STEP 5: Click on the Scripts tab and add the below code to the
button sprite. Remember to match the code blocks to the
correct category using their color.
Events
Looks
Events
Looks
25
STEP 6: Select the bat sprite, and add the four code blocks
you see below to its Scripts tab.
26
Imagine your Scratch work
STEP 7: Finish the bat by adding
space is a big coordinate plane.
these last few blocks of
code to its Scripts tab. y
x x
Events
Looks
(-y, -x) (-y, +x)
y
Looks
Changing the X and Y in the Motion block refers to
the X and Y axis of a coordinate plane. If you have a
positive Y coordinate, it will be found on the upper
half of the plane. If you have a negative Y coordinate,
Looks
it will be found on the lower half. If you have a positive
X coordinate, it will be found on the right side. If you
These codes make the bat have a negative X coordinate, it will be found on the
27
STEP 8: C
lick on your Scratch Cat sprite
and add this code to his Scripts tab.
Events
Looks
Motion
Motion / Operators
Control
Motion
Control / Sensing
Motion
Control / Sensing
Looks
Control
Control /
Operators /
Sensing
Looks
Control
28
When the green flag is clicked, Scratch Cat will appear and the background will change
to the woods backdrop.
Scratch Cat will set his Y coordinate to 180 so he starts at the top of the screen.
(The Y coordinate is the position along the Y axis, which runs up and down. 180 is
the highest Y position on the Scratch grid.)
Scratch Cat’s X coordinate is set to random -240 to 240 so he falls from a different
spot on the X axis each time the game is played. (The X coordinate is the position
along the X axis, which runs left to right. -240 to 240 is the X coordinate range for
the Scratch window.) The bottom section of code is held inside a forever loop. That
means it will repeat continuously throughout the game. The Y coordinate will change
by -1, causing Scratch Cat to fall downward. (To make him fall faster, change the
number to a more negative number, like -2 or -3.)
There are three different conditions—if thens—that can happen to Scratch Cat.
To code what happens if a condition is met, the code blocks go inside the loop of
the if then block.
1. The first condition tells Scratch Cat that if he touches the bat, then he should
move wherever the bat does. (This Motion block will originally say go to mouse
pointer; select bat1 in the drop-down bar.)
2. The second condition says that if Scratch Cat touches the pink rectangle at the
bottom of the screen, then the screen will switch to game over, Scratch Cat will
hide, and everything will stop.
3. The final condition says that if the bat reaches the safe button with Scratch Cat,
then the background will switch to the winner backdrop and stop all. (To select both
the safe button and the bat, you will need to select the and block from Operators
and both sprite blocks from Sensing. Try building this block separately before
dropping it into your Control block.)
T IP :
See page 17 for a remi
nder Use this link to check out the finished game
on how to select the co
rrect and try your luck at saving Scratch Cat:
color in your Sensing blo
ck. https://scratch.mit.edu/projects/158576405/
29
How Did the Crab
Cross the Road?
HOW TO PLAY
Use the arrow keys to move your crab across a busy street without getting hit
by a car. If the crab is hit, you’ll be sent back to the beginning. If the crab makes
it safely across the street, you win!
STEP 2: C
lick on the paintbrush icon in the backdrop tools to
draw your own road background that looks like
what you see here:
paintbrush
30
STEP 3: A
dd the code blocks you see below to your
crab sprite.
Events
Motion
31
STEP 4: A
dd the following code to the car sprite. This will
create a screen scrolling effect in your game.
Motion
Looks
Motion
Looks
Events
32
These two blocks will make the car move at a certain speed and to the left.
That is the direction the screen scrolling code is also going. (Use a number
larger than five in the first block to make the car go faster.)
This conditional statement is activated when the car sprite touches the
crab sprite. Whenever this happens you broadcast—send a message—to
the crab telling it that it has been hit. (We will code what happens to the
crab in another step.)
STEP 5: C
hange the rotation style of the car sprite so it doesn’t flip upside
down when it moves. To do this, click on the small letter to open the
sprite’s information. Then click the side-to-side arrow in rotation style.
33
STEP 6: O
nce you have changed the rotation style, use the stamp tool to
duplicate the car sprite twice. (You should now have a total of
three cars.) In the Costumes tab, use the paint bucket to change
the cars’ colors so they don’t all look the same. (Go back to Race
Game if you need help.)
STEP 7: A
dd the below code to your crab sprite. This tells
the crab to react to the hit message it will receive
when the car touches it. (Use the drop-down menu
in the Events block to select the correct option.)
Events
Motion
STEP 8: Arrange the sprites on the screen as shown on the next page.
(You may need to use the shrink tool to fit the sprites on the screen.)
34
Stagger the car
sprites along the
lanes on the road
so they move
across the screen
at different times.
T IP :
You do not need to chan
To change the car speeds, edit ge the numbers on
the Motion block when
you drag it out from
the Motion block in the code. the selection area. The
coordinates will
update when you move
the sprite. (It’s okay
if exact location of your
crab is different from
what you see here. Your
s may be placed at
a different location than
the one shown.)
Click on See project page to play. Use the arrow keys to move your
crab across the road. Don’t get hit! Use this link to check out the
finished game: https://scratch.mit.edu/projects/174141026/
35
Space Muffins
HOW TO PLAY
Use the up and down arrows to control a spaceship that is trying to destroy
invading dinosaurs. To destroy the dinosaurs, click the space bar to blast muffins
at them!
paintbrush
36
STEP 3: In the spaceships’ Costumes tab, delete
the spaceship-b costume by clicking the x
in the upper right corner. Then duplicate the
spaceship-a costume by clicking on the stamp
tool at the top of your screen, then on the
spaceship-a costume.
Change the first costume’s name to spaceship. Then convert the second
costume to bitmap mode by clicking Convert to bitmap at the bottom right of
the screen. Use the grabber tool in the toolbar on the left to pull apart pieces
of the ship. (You need to draw a box around each part of the spaceship you
want to break apart and then drag it.) Name the second costume crash.
grabber tool
37
STEP 4: Arrange the spaceship sprite on the background as shown below.
Be sure to shrink the sprites to the right sizes using the shrink tool.
(The muffin should be about half the size of the spaceship.)
shrink tool
38
STEP 5: Go to the Sounds tab for the spaceship sprite and open
the Sound Library. Select the cymbal crash effect. Then
add the below code to your spaceship sprite’s Scripts tab.
Events
This will tell the spaceship to move
Motion
up or down with the arrow keys.
39
WHAT IS THE CLONING CODE BLOCK?
Before we go further, let’s talk about the cloning block. You’ll need it for the muffin and
dinosaur sprites. If you preview the game using the link at the end, you’ll notice that multiple
dinosaurs invade and multiple muffins are blasted out. But so far, we have only one dinosaur
and one muffin.
So how do you get from here to there? That’s where the cloning block comes in. A clone
is a copy of an existing sprite. Using code, you can tell your sprite(s) when to make a clone
and give specifics on what you need that clone to do. The cloning blocks can be found in the
Control category.
40
STEP 6: Click on your muffin sprite, then open the Sounds tab. Open the Sound
Library and select laser2. Then add the code blocks below to the muffin
sprite.
41
STEP 7: Add the below code blocks to the dinosaur sprite.
Looks
Control The original sprite will hide, and the dinosaur
will create a clone every 1–7 seconds. (This will
happen forever thanks to the forever loop.) The
wait allows the dinosaurs to come out at random
Operators
times throughout the game.
Control
Motion
Looks / Operators
Motion / Operators
Looks
Motion
Control / Sensing /
Operators / Motion
Motion / Operators
Control
42
The clone must then set its Y coordinate to random location between -175
and 175. (This means the dinosaur will enter the screen at a random spot
on the Y axis.) Then it will show and point to the left of the screen (-90). To
create the set Y to random block, use the set Y block from Motion and the
pick random block from Operators.
43
STEP 8: C
lick on your backdrop and open the Sounds tab. Select the
space ripple sound. Then go to the Scripts tab and add this
final code.
Events
Control
Click on the green flag to play your game. Use the arrow keys to move your
spaceship and avoid the dinosaurs. Use the space bar to blast muffins at the
dinosaurs before they destroy your ship! Use this link to see the finished game:
https://scratch.mit.edu/projects/178325240/
44
Read More
Wainewright, Max. Code Your Own Games!: 20 Games to Create with Scratch.
New York, NY. Sterling Children’s Books, 2017.
Makerspace Tips
Download tips and tricks for using this book and others in a library makerspace.
Visit www.capstonepub.com/dabblelabresources
Internet Sites
Use Facthound to find Internet sites related to this book.
Visit www.facthound.com
45
Coding Glossary
Bitmap mode: The drawing tools in this mode make it easy to fill in
backgrounds and shapes. If you are making a quick shape or basic
background, bitmap mode is a good choice. (Keep in mind that if you
need to go back and resize a shape later, bitmap mode won’t allow it.)
To change between bitmap and vector mode, use the buttons on the
bottom right of the design screen.
Broadcast: These code blocks can be found in the Events category of the
Scripts tab. A broadcast is like sending a message.
Loop: Loops are used in coding when something needs to happen more than
once. Loops can be used with one piece of code or many. The code inside
the loop will run (on repeat) in the order it’s placed in,
46
Origin: The origin is the middle point of a coordinate plane. This is where the
X-coordinate and Y-coordinate both equal zero and the two axes cross.
Vector mode: The drawing tools in this mode are similar to tools in bitmap
mode. However, in vector mode you can create another shape and still
go back to a previous one and move it. In this mode, you can also reshape
objects that you have made.
X-axis: The X-axis is the axis that runs horizontally (side to side) in a
coordinate plane.
Y-axis: The Y-axis is the axis that runs vertically (up and down) in a
coordinate plane.
47
ABOUT THE AUTHOR
Rachel Ziter was raised in Las Vegas, Nevada. She earned a
Bachelor of Science in Education and her teaching credentials
from Florida Southern College. She has also completed graduate
coursework in computer science education at St. Scholastica,
as well as professional development in fablab project-based
learning at NuVu. Rachel currently works at the Adelson
Educational Campus in Las Vegas and is a member of the Tech
Team, where she teaches STEM curriculum and instruction,
mentors students, and teaches coding and engineering.
48