Professional Documents
Culture Documents
Max Wainewright 25 Scratch 3 Games For Kids A Playful Guide To Coding No Starch Press 2019
Max Wainewright 25 Scratch 3 Games For Kids A Playful Guide To Coding No Starch Press 2019
Scratch
Games
for Kids
Max Wainewright
25 SCRATCH 3 GAMES FOR KIDS. Copyright © 2019 by Max Wainewright
All rights reserved. No part of this work may be reproduced or transmitted in any
or by any information storage or retrieval system, without the prior written permis-
ISBN-10: 1-59327-990-6
ISBN-13: 978-1-59327-990-5
www.nostarch.com
No Starch Press and the No Starch Press logo are registered trademarks of No
Starch Press, Inc. Other product and company names mentioned herein may be
the trademarks of their respective owners. Rather than use a trademark symbol
with every occurrence of a trademarked name, we are using the names only in an
editorial fashion and to the benefit of the trademark owner, with no intention of
The information in this book is distributed on an “As Is” basis, without warranty.
While every precaution has been taken in the preparation of this work, neither the
author nor No Starch Press, Inc. shall have any liability to any person or entity with
contents
acknowledgements vii
3: WHAT’S THE SCORE?
introducing scratch 3 viii
Shark Diver 40
Cactus Jumper 64
2: DRAWING BACKGROUNDS AND SPRITES Tower Builder 68
Robot Power Up 72
Down the River 18
Invasion of the Donuts 76
Monster Maze 20
Challenges 82
Penguin Bobsled Cross 22
Color Bounce 24
Racetrack 5: MAKING YOUR OWN BLOCKS
28
Challenges 32
Cave of Stars 86
Space Tacos 90
Two-Player Soccer 94
Challenges 112
Ackowledgements
fantastic website.
scratch 3
own games with Scratch 3. You’ll use But before you start
coding, let’s learn a little
different blocks to create code. You’ll also
about the website we’ll
learn how to add objects called sprites and
be using.
use code to tell them what to do. At the end
The Scratch Editor is where you create projects in Scratch. Here are its main parts:
Coding Area Drag and Start and Stop Click Stage Size Use these
Menus Use the File drop the blocks here and the green flag to start buttons to make the stage
menu to save or snap them together to your code, or the stop smaller, set it to normal
load your projects. create your program. sign to stop it. size, or fill the screen.
The Stage
Your program
on the stage.
Block Categories
Sprite Info
groups. Click a
sprite, such
category to scroll
as how big
to the blocks in
it is.
that category.
Code Blocks The code Tutorials Some Sprite List All the Choose a Sprite Choose a
blocks in the current help videos are sprites in your Add a new sprite Backdrop Add a
group are shown here. shown here. Click project are shown to your project. new background
Scroll down if you can’t the white X to close here. The selected to your project.
find the block you want. this panel. sprite has a blue
viii
Scratch has so Don’t panic—you’ll
many code blocks, learn how to use
sprites, and sounds them in no time!
to learn about.
You’ll need to switch between the three main areas of the Scratch Editor while you’re creating your games.
These areas let you code, change how your sprites look, or add sound to a sprite.
Fill Use this to set the color Undo Click this if you make a
draw on your
can have more than
sprite.
one look. Scratch calls
costumes to make
Zoom Tools
simple animations.
Use these to
zoom in and
add detail to
your sprite
costume.
New Costume
ix
When you’re drawing on the backdrop, the Costume tab changes
to the Backdrops tab. The drawing tools for backdrops work in just
the same way.
straight line.
or rotate.
x
COLOR SLIDERS
0 67 0 0 0
100 1 0 0 100
0 57 84 100 100
11 16 40 40 62
88 61 74 88 11
32 100 81 78 100
xi
game on!
use input blocks to make code run when the player does something,
MOTION BLOCK
2
FOREVER BLOCK
animations.
You can combine two blocks to make code that runs when one sprite bumps into another.
3
Cat on the Move
started!
scratch.mit.edu
Find the Code tab near the top-left corner of
the screen.
code blocks.
Enter on the keyboard. Click Create on the main page
to get started.
3. START CODING
Coding Area.
4
4. SET THE KEY
In games, we switch between slightly different
pictures to make it look like sprites are walking.
In movies, these images are called frames .
You want to make some
costume1 costume2
5. GET MOVING!
Drag the following code blocks into the Coding Area. Use their colors to help you find the blocks.
Click the green flag at the top of the screen to test your code. Choose the key that will
Press the right arrow key and watch the cat walk! make each section of
code run. (See step 4.)
Click the green flag to test your code. Use the arrow keys to move the cat around! to make the Dog on the
For more help visit maxw.com .
Move game on page 14.
5
Cat and Mouse
1. START SCRATCH
scratch.mit.edu
2. START CODING
Drag the following code blocks into the Coding Area. Use their colors to help you find the blocks.
Click the green flag to test your code. Each time you press the
6
Look near the bottom right of the
screen to find the button. Make
3. ADD ANOTHER SPRITE
sure you click the correct one!
Sprite button.
Scroll through the sprites until you find a Click it to choose the image for your Your new sprite
Sprite List.
Add this code to make the mouse move when the Z key is pressed: Find a friend to race against. Drag your sprites
Z key is pressed.
and then go!
10 steps forward.
to see who wins!
Switch costumes to
7
Fish in the Sea
button.
Scroll through the sprites until you Click it to choose the image for your
8
5. ADD CODE
Click the green flag to test your code. The fish should start moving forward slowly. It keeps moving
until it reaches the edge of the Stage because the move code block is inside the forever block.
6. CHANGE DIRECTION
You need to be able to steer the direction of the fish by pressing keys. Drag in two more sections of code, shown here:
is pressed.
is pressed.
15 degrees
15 degrees
counterclockwise.
clockwise.
Drag the fish to the left-hand side of the Stage, and then click the green flag to test your code.
Use the left and right arrow keys to steer the fish around.
Backdrop button.
8. CHOOSE UNDERWATER
9
Bat in a Flap
Sprite button.
corner of the cat sprite
a bat.
Drag this code into the Coding Area to make the bat fly around the Stage:
Set the direction of the bat so it will move toward the mouse.
Click the green flag to test your code. The bat should fly toward the mouse
pointer. Move your mouse to practice making the bat fly around the Stage.
10
5. ADD A SPRITE 6. CHOOSE THE ORANGE
picture of an image.
orange.
orange.
Backdrop
find Woods . the backdrop.
button.
Test your code. Use the mouse pointer to guide the bat around the Stage until the bat eats up all the oranges!
In this game, you used the hide and show blocks to make it look as if a sprite got eaten. You
used the wait until and touching code blocks to tell when one sprite has touched another.
Finally, you learned how to duplicate a sprite to create multiple targets to collect. When
you’re ready for a challenge, turn to page 15 and try creating the Dinosaur Diner game!
11
Kiran and the
Magic Crystals
3. CHOOSE KIRAN
Make Kiran fly by dragging this code into the Coding Area:
Click the green flag to test your code. The code should make Kiran fly toward the
mouse pointer. Practice making her fly around the Stage by moving your mouse.
12
5. ADD A SPRITE 6. FIND THE CRYSTAL
sprite.
Drag this code into the Coding Area to control the crystal:
Repeat the next two code blocks until Kiran collects the crystal.
9. WE NEED STARS!
Backdrop
find Stars . the backdrop.
button.
Test your code. Use the mouse pointer to guide Kiran around the Stage and collect the magic crystals!
In this game, you made the crystals disappear when Kiran touched them. You used a repeat
until touching loop to keep running some code until each crystal was found. This code created
an animation makes the game look more advanced and fun to play. Turn to page 15 and try
creating the Ripley and the Aliens game when you’re ready for a challenge!
13
chapter 1
challenges
sprites look.
14
CHALLENGE 3 BIRD IN THE SKY
game work.
15
Drawing Backgrounds
and Sprites
detect when one sprite hits another one, and you’ll use
the sliders:
16
SET TOUCHING COLOR BLOCKS
change y blocks to
move a sprite.
(-240,180) (200,120)
x
(0,0)
(240,-90)
(-50,-100)
17
Down the River
of the screen.)
Click the blue X in the corner
3. START DRAWING
Choose the Choose a blue color. Make the brush very thick
Drawing Area.
18
If you make a
5. DRAW THE RIVER
mistake while
drawing, click the
Use the Brush tool to draw a river. Make sure the river is wide enough for the sailboat you’ll be adding.
Undo button and try
(If the boat gets stuck too easily, come back and make the river wider by drawing more water.)
again!
new sprite.
Make the Sailboat move by dragging this code into the Coding Area:
Keep repeating the next code block until the boat hits some land.
Now drag in two more scripts to steer the boat when the left or right arrow key is pressed:
to check for.
to rotate.
This game showed you how to draw your own backdrops. You used the touching color
block to check whether a sprite was bumping into part of your background picture. You’ll
be using this technique in lots of ways throughout this book. Ready for a challenge? Turn to
19
Monster Maze
1. NO CATS
2. SELECT THE BACKDROP
Turn to page 16
for help with
setting colors.
3. START DRAWING
Choose the Choose a blue color. Make the line thick by typing
20
5. ADD SOME WALLS
to gradually build
up the maze.
new sprite.
Make Gobo the monster move by dragging this code into the Coding Area:
Shrink the sprite down to 40 percent of its size so it fits in the maze.
check for.
to move.
In this game you created your own maze backdrop. You used the same technique as
in Down the River to check for collisions—the color touching block. But the code to
move the sprite used some new ideas, making it go back when it hit a wall. Ready for a
challenge? Turn to page 32 and try to build the Magic Maze game.
21
Penguin
Bobsled Cross
X to delete
the cat.
3. START DRAWING
drag to rectangle
rectangle at to be the
tool.
Click the Code tab and then drag this code into the Coding Area:
Click the
button.
Stop the penguin from fully rotating.
the Stage.
22
6. SET A CHANGE OF DIRECTION
Now add more code to make the penguin change direction when one of the arrow keys is pressed:
check for.
7. ADD A BOBSLED
costume showing
Click the Code tab and then drag this code into the Coding Area:
change direction.
crossing game.
23
Color Bounce
1. NO CATS
In the sprites
Stage icon.
3. START DRAWING
Area.
24
If you make a mistake,
click the Undo button
and try again.
4. DRAW A PATH
Select the Paint option Bitmap button. Rectangle tool. red color.
25
7. ADD CODE TO MAKE THE SQUARES MOVE
Click the Code tab and then drag the following code into the Coding Area:
Drag the square into place. Click the green flag to test
8. DUPLICATE SQUARES
On the Stage,
make them move left and right, Let your mouse hover over
26
11. DRAW THE PLAYER SPRITE
It should be roughly
Click the Code tab and then drag the following code into the Coding Area:
27
RaceTrack
In the sprites
Bitmap button. Fill tool. and choose a green color. Color the
Drawing Area.
Draw a series of rectangles to make a track for your game. You can always make the track thicker or thinner later.
28
5. ADD A CAR SPRITE
If you make a
Find the Paint mistake, click
option. Click Paint . the Undo button and
Let your mouse hover try again!
over the Choose a
Sprite button.
Use a much
tool to add a
add a box at the
yellow driver’s
front. This will
for collisions.
29
7. CODE THE CAR
Click the Code tab at the top left of the screen, and then drag this code into the Coding Area:
Click the green flag to test your code. Press the up arrow to start the car moving. Use the left and right arrows to
steer it around the track. If the car hits anything green, it should stop, until you press the up arrow again.
30
8. ADD ANOTHER CAR
delete
Change: to:
right-click the
Change: to:
car icon.
Change: to:
Click the Costumes tab. color of the second car. (Make sure to leave
31
chapter 2
challenges
finds them.
32
CHALLENGE 3 CROSS THE ROAD
33
What’s the Score?
RANDOM NUMBERS
96
23 45
1
77
34
SHOWING THE TIMER
You can display the timer on the Stage to show how long a program has been running.
VARIABLES
35
Penguin Ski
School
the Stage.
Click the X to
Penguin 2 .
Sprite button.
button.
36
5. ADD SOME SKIS 6. ADD A HAT
Use the Line tool to draw two skis for the penguin.
If you make
a mistake
Let your mouse
drawing, click the
hover over the
Undo button and
Choose a Sprite
Click the Paint option. try again!
button.
8. DRAW A TREE
Click the Fill in the tree Choose a Select the Use the mouse
Fill tool. with green. brown color. Rectangle tool. to draw the
tree trunk.
37
9. CODE THE TREE
Click the Code tab at the top left and then drag this code into the Coding Area:
Click the green flag to test your code so far. Only the tree should move, traveling up the
Stage. Once it reaches the top, it should start again from a random place at the bottom.
38
11. CODE THE PENGUIN
Set the penguin image to flip left and right instead of rotating.
Repeat the following code until the skiing penguin hits the tree.
tree icon.
delete
Click the green flag to test your code. Press the left and right arrow
keys to steer the penguin left and right as you avoid the trees.
This game shows you how to use random numbers to make your games
more exciting. You could try using this technique with some of the games
you made in the previous chapters. When you’re ready for the next challenge,
39
Shark Diver
score 7
1. NO CATS
the cat.
2. UNDERWATER
Backdrop button.
Sprite button.
Diver 2 .
Find Underwater 2 and then click it
40
4. CODE THE DIVER
Click the green flag to test your code. The diver should swim
a Sprite button.
Click the
score will appear at
Variables button.
the top-left corner of
the Stage.
41
7. IMPORT A SOUND EFFECT
Click the Code tab and then drag this code into the Coding Area:
Repeat the next code block until the diver gets to the treasure.
9. ADD A SHARK
Click the
Choose a
Scroll through the sprites
Sprite button.
and click Shark .
42
10. CODE THE SHARK
If the shark hits the screen edge, flip it around so it swims back the other way.
Click duplicate .
duplicate
in different places.
right-click the Shark .
43
Fruit Catcher
score 12
timer 27
Variable button.
Click OK .
Click the
Variables button.
Click the
click its checkbox.
Sensing button.
44
3. CODE THE CAT
Drag this code into the Coding Area to make the cat move:
Stop the cat from fully rotating—just let it flip left or right.
Show the next cat costume to make the cat look as though it’s walking.
Stop any other code from running, like the falling fruit.
4. COOL IT DOWN
Click the
Refrigerator .
Backdrop button.
45
5. ADD A SPRITE
costumes and
click Bananas .
Costume button at
a Costume . a Costume .
46
8. CODE THE FRUIT
Click the Code tab and then drag this code into the Coding Area:
Show the next costume. (These blocks make sure the fruit has a
Move the fruit down to the bottom of the Stage so you can break out
duplicate
delete
47
Brick Tricks score 9
Click the X to
the Stage icon.
3. START DRAWING
Color the
Drawing Area.
48
4. DRAW THE FLOOR
Select the
Rectangle tool.
Choose a
red color.
Use the mouse to draw a thin, wide rectangle at the bottom of the Drawing Area.
Choose a Sprite
Draw a small
red rectangle
in the center of
red one.
49
Now that you have your
backdrop and sprite ready, 7. START CODING
it’s time to add some code!
8. MAKE A VARIABLE
Type in
score
score .
Choose Click the Make a
Click OK .
50
10. MAIN CODE
Keep repeating the following code until there’s no room for any more bricks.
Keep repeating the following code until the brick hits the floor or
another brick.
Now that the brick has hit something, you draw it on the backdrop in that
exact location. Then you move the brick up to the top of the Stage.
is pressed:
Click the green flag to test your code.
Rotate the brick Use the arrow keys to guide the bricks
Although this game has only one sprite, the Pen extension code lets you use it
over and over again as long as the game runs. By using the stamp command, the
location the brick stops becomes part of the backdrop. When you’re ready to take
this to the next level, turn to page 57 and try the Mega Brick Builder challenge!
51
Colored Walls
score 25
Click the Convert to Bitmap Choose the Click the Color drop-down
52
4. ADD THE WALL SPRITE
Choose a Sprite
Draw a thin
rectangle about
one-quarter of
Click the Convert to Choose the Choose red. the width of the
the center.
6. MAKE A VARIABLE
Type in
score
score .
Choose Click the Make a
Click OK .
53
7. CODE IT
Click the Code tab and then drag this code into the Coding Area:
Increase the size of the wall by the value of the score. As each
Repeat the next line of code 160 times. (This will move the wall
160 × 2 = 320 steps, which is just less than the screen’s height.)
move down.)
of the screen.
Click the green flag to test your code so far. A series of red rectangles should slowly move down the screen from
the top to the bottom. Each rectangle should be slightly bigger than the previous one. Now let’s add the player.
54
8. ADD THE PLAYER SPRITE
Choose a Sprite
Draw a very
small square in
the center.
tool.
10. CODE IT
the xcoordinate of
Set the score to 0.
the mouse. The score
Increasing by 5 makes
Make the player appear to roll as it moves.
game is over.
Colored Walls 2 game on
page 57.
Click the green flag to play your game. How many points can you score?
55
chapter 3
challenges
56
CHALLENGE 3 MYSTERY CATCH
fruit that fell down the screen. Use the same ideas
will fall down? How many points will you get for
the game.
57
Math Matters
MATH SYMBOLS
To combine blocks, drag All coding languages, including Scratch, use special symbols for multiplication and division:
You can use math to create special sound effects. In the Robot Power Up game, you’ll
combine calculations to change the pitch of the sound as the robot moves up and down.
Start by dragging a
multiplication block.
Drop a y position
first, as if it was in
brackets.
58
USING MATH TO MAKE A GAME GET HARDER The longer you play the
game, the faster the
donuts appear!
In Invasion of the Donuts on page 76, you’ll use
between 0.5 and the value of the variable. This makes the
BROADCASTING MESSAGES
math required!
You’ve used the duplicate option to make copies of your sprites. But there’s
a better way to do this using code. To clone sprites, use the create clone of
myself block:
Now, to make each clone do something, use the when I start as a clone block:
59
Super Snake score 27
corner of the
(It’s at the bottom right.)
Color the
Click the Convert to Choose the Click the Fill drop-down
screen.
Bitmap button. Fill tool. and select red.
Select the
Rectangle tool.
Use the mouse to draw a large yellow
Choose yellow.
The border must be exactly the same color as the snake. You’ll be using this to check for collisions.
60
4. ADD THE SNAKE SPRITE IIf you make a
mistake, click the
Undo button and try again!
Sprite button.
Find the Paint option
circle to be a section
to create another
tool to draw
a small gap between it
two eyes.
and the head.
61
9. START CODING
Type in
score score .
Click the
Variables button.
Click the Make a Variable button.
Click OK .
62
12. CODE THE BODY
Add these two sections of code to control the body of the snake:
Wait a moment.
When the clone of the snake’s head is created, run this code.
Increase the wait time. The greater the score, the longer the wait
before each clone gets deleted. This makes the snake get longer
By now, another snake segment will have been created, so it’s time
Click the green flag to test your code. Use the left and right arrow keys to
steer your snake around the screen. Avoid bumping into anything!
63
Cactus Jumper score 32
score speed
Click Make a Variable .
Click Make a Variable
again.
Click Variables .
Drag this code into the Coding Area to make it seem like the cat is running in place:
Move the cat to the left side of the Stage, near the bottom.
64
4. IMPORT A SOUND EFFECT
Click the Sounds Click the Choose a Scroll through the sounds
Press the spacebar and watch the cat jump in the air!
rectangle at the
bottom of the
screen to be the
Choose a
Click the Convert to Choose the
ground.
green color.
Bitmap button. Rectangle
tool.
65
8. DRAW THE SKY
Click to fill in a
gradient color
Choose the Set the left color Set the right
gradient .
Drag these code blocks into the Coding Area to make the cloud drift slowly across the Stage:
a Sprite button.
of the Stage.
Sprite button.
called Bonk . (See step 4
Find the Paint option and click it.
for help.)
66
14. CODE THE CACTUS
The first section of code creates the cactus clones. Click the Code tab and then drag this code into the Coding Area:
Make it invisible.
Wait a moment.
Every few seconds, this
code will create one to
three clones of the cactus.
Waiting for 0.05 seconds
The second section of code makes each clone move toward the cat:
means they won’t be too
close together.
Make it visible.
If the cat bumps into the cactus, then run this code.
Once the cactus clone has reached the edge, delete it.
67
Tower Builder
Backdrop button.
Choose a Sprite
button.
68
4. DRAW A FLOOR OF THE TOWER
Draw a red
rectangle in the
center. It should
Click out of the red Choose black. Choose the Line Add details
into place.
69
7. NAME THE COSTUME
First, set a name for the makes it easier to organize your code.
current costume.
A new costume
should appear.
duplicate
Draw two lines to outline the top of the tower. Click to fill in the roof.
score tower x
Click Make a Click Make a
Click the Sounds Click the Choose a Scroll through the sounds to
tab (top left). Sound button. find the Dance Celebrate icon.
70
13. THE COMPLETE CODE
71
Robot Power Up score 12
timer 27
button.
The Music
Extension adds
3. ADD A ROBOT 4. ADD AN EXTENSION extra code blocks
to make and
control sounds.
Variable button.
Click the block and click the
button. button.
score .
72
7. CODE THE ROBOT
Control the robot by dragging this code into the Coding Area:
Move the robot over to the left side of the screen, in the middle.
But if the spacebar isn’t pressed, then run this code instead.
73
8. MAKE SOME NOISE
Click the green flag to test your code. The robot should slowly move down. Pressing the spacebar
will move it up. You should hear a sound effect that changes its pitch as the robot moves.
Control the fuel cell by dragging this code into the Coding Area:
Hide the original fuel cell since you’ll be making a copy of it.
74
11. CODE THE FUEL CELL
Play a sound effect to let players know they caught a fuel cell.
Delete the clone—it’s reached the edge of the Stage or has been caught.
75
Invasion
1. NO CATS! 2. STARS
3. ADD AN EXTENSION
Score
Score .
Lives
Variable button.
Click the Make a
Lives .
Type in Click OK .
Delay .
76
7. HIDE OR SHOW
Sprite button.
Click the Convert to Bitmap button. Choose the Line tool. Choose a green color.
77
10. START CODING
Click the Code tab at the top left of the screen and then drag this code into the Coding Area:
Sprite button.
78
13. START CODING THE LASER
Choose the
invisible.
Shrink it to 20 percent
of its size.
Once the ball clone hits the edge of the Stage, delete it.
Sprite button.
79
15. START CODING THE DONUT
Now you need to clone the donut and control it. Drag the following code into the Coding Area:
If the delay is more than half a second, then run this code.
Reduce the delay by 0.1 seconds. (These code blocks will keep reducing
the delay before a new donut appearing until it gets to 0.5 seconds.)
Tell the other sprites that the ship has been hit.
Delete the donut clone—it has reached the edge of the screen.
80
16. MORE DONUT CODE
the message.
Click the Sounds Click the Choose Scroll through the sounds to
tab (top left of a Sound button. find the Boom Cloud icon.
Add some code to decide what happens when the spaceship is hit. Drag the following code into the Coding Area:
Show a message.
Click the green flag to play the game. How long can
81
chapter 4
challenges
for help.)
82
CHALLENGE 3 SUPER TOWER
for help.)
lives will you have, and how many points for each
burger destroyed?
83
Making Your
Own Blocks
CREATING A FUNCTION
block name
Type in the name of
sure to choose a
Click OK .
Some programming
languages call functions
procedures or methods .
84
DEFINING THE FUNCTION
Follow these steps to tell Scratch what your function block will do:
function is used.
To use the function you created, just drag it into your code.
different things.
85
Cave of Stars
robot goes.
Choose black.
red. Brush tool.
extremely thick by
typing in 100 .
86
If you make a mistake
drawing, click the Undo
button and try again!
5. DRAW A PATH
tool to draw a
path through
the cave.
Draw a second
path thicker.
6. ADD A STAR
7. ADD A ROBOT
a Sprite button.
a Sprite button
8. MAKE A BLOCK
Make a Block
87
You’ll run this code when the
game starts. This makes sure
the robot is in the correct place.
moveRobot
Click OK .
forward.
is used.
Click OK .
pointer.
88
So far, you’ve defined
Now you need to define a
blocks to start the
block for when you win!
game and to move
and steer the robot. . . . and when
you lose. Ouch!
showCompleted
Make a Block
type in showCompleted
as a function name.
Click the Make a Block
lostGame
Click OK .
as a function name.
Show a message.
Now that you’ve defined the blocks you need, let’s use them! You’ll combine them with other
simple control and sensing blocks to complete the code. Drag in these blocks:
function.
Keep repeating this code until the robot hits the cave.
function.
function.
Ready for a
challenge? Turn to
The game is over! Run the code in the lostGame function. page 112 and try
Click the green flag to test your code. Try guiding the robot to the star.
Underground game.
89
Space Tacos red
set it as the
Start by deleting
Backdrop button.
3. ADD A SPRITE
Sprite button.
Click OK .
Click Variables .
90
5. MAKE A BLOCK
Make a Block
startGame
Type startGame .
startGame should
Click My Blocks .
appear in the Coding
Area.
block is used.
Click OK .
91
Now let’s code by using the
blocks you just made.
You’ve defined the blocks you need to start the game and move the dog. Now you’ll join them together with other code.
Click the green flag to test your code! The dog should move slowly toward your mouse pointer.
9. ADD A TACO
You need a function block that will move the taco to a random place.
Make a Block
block repositionTaco .
Click OK .
Sprite button.
92
11. CHECK WHETHER THE TACO HAS BEEN EATEN
Click Make a Block and type in Drag in this code to check whether the taco has been eaten by Dot the dog:
Make a Block
then do this.
Click OK .
You’ve defined the blocks you need to reposition the taco and to check whether it’s been eaten.
Now let’s put them together with other code. Drag in these blocks:
Run the block that checks whether the taco has been eaten.
Click the green flag to test your code! Guide Dot around the screen to eat up the tacos.
93
Two-Player
to adapt.
3. START DRAWING
5 Carefully draw a
thickness to 5 .
Click the Convert to Click the Choose white. Area (look for the
Outlined .
Click the
an exact circle.
94
To make it easier to see the
6. ADD A SPRITE goal, you’ll draw it in black
first and then change it to
white at the end.
Let your mouse hover
Sprite button.
Click the Paint option.
7. DRAW A GOAL
Draw a short
vertical line in
see the line better. Use the mouse to draw the rest of the goal net.
duplicate
blue goal
delete
Click inside the Sprite In the Sprite List, right-click Click duplicate .
Chose the
Click in the Sprite
Select tool.
The goal name box, type
red goal
should now red goal , and
Click Flip
look like this. then press Enter . The Sprite List should
Horizontal .
now look like this.
95
12. ADD A SPRITE 13. DRAW A SIMPLE PLAYER
Sprite button.
draw a small blue
rectangle about
the goal.
Click the Code tab and then drag this code into the Coding Area:
14. RENAME IT Run the following code when the green flag is clicked.
blue player
duplicate
Change: to:
In the Sprite Click the Click the Choose Click inside the
Press Enter .
96
19. ADD A BALL 20. IMPORT SOUND EFFECTS
a Sprite button.
Select the
Click the Click the Choose Scroll to find the Click Choose a Scroll to find the
Soccer
Sounds tab. a Sound button. Goal Cheer icon. Sound again. Kick Drum icon.
Ball .
Click it to import Click it to import
21. MAKE A VARIABLE FOR RED 22. MAKE ONE FOR BLUE
red blue
Click
Code tab. Variable . Code tab. Variable .
Type red . Click OK . Type blue . Click OK .
Variables .
Make a Block
A pink block called
Coding Area.
Click OK .
Click My Blocks .
24. ADD CODE TO DEFINE THE BLOCK 25. MAKE AND DEFINE ANOTHER
Make a Block
Drag in the following blocks to define the startGame code block:
Click Make a Block and
a function name.
startGame block is used.
Click OK .
You’ll run this code when the If the ball hits the edge of the
97
26. MAKE AND DEFINE TAKECENTER
Click Make a Block and then Drag in this code to define the takeCenter function:
function name.
takeCenter
or the right.
Click OK .
Click Make a Block and then Drag in this code to check whether either player has scored a goal.
function name.
Make a Block
checkForGoals
If the ball hits the red goal, then run this.
98
28. HOW TO CHECK WHEN A PLAYER KICKS THE BALL
Click Make a Block and Drag in this code to check whether either player has kicked the ball. If the player has kicked
type in checkForKicks the ball, the code needs to change the direction of the ball.
as a function name.
checkForKicks
Click OK .
Play a sound effect.
This code will run the other functions that you defined:
Check to see whether any goals have been scored. third challenge on
Chapter 3 games by
99
Rocket Racers
3. ADD A SPRITE
Sprite button.
Click the Paint option.
4. DRAW AN ASTEROID
Draw an asteroid by using the Line tool. It should be about half the height of the Drawing Area.
Choose the
Fill in the asteroid
Fill tool.
with green.
100
5. MAKE A SPEED VARIABLE
speed
Type in speed .
button.
6. MAKE A BLOCK
Make a Block
Type createAsteroid .
createAsteroid
101
8. DEFINE A BLOCK TO MOVE THE ASTEROID
moveAsteroid
moveAsteroid as a Run the following code
Now that you’ve defined all the blocks, let’s put them together using other code. Drag in these blocks:
Repeat the following code until the asteroid reaches the left edge
of the Stage.
Delete the asteroid clone after it has reached the left edge of the Stage.
102
10. ADD A SPRITE
a Sprite .
Click Variables .
Make a Block
prepareRocket
Click OK .
Start the rocket on the left side.
as a function name.
Run this code when the handleKeys
block is used.
Make a Block
handleKeys
Click OK .
103
14. DEFINE THE CODE TO MOVE THE ROCKET
Click Make a Block and Drag in this code to move the rocket:
type in moveRocket as
Make a Block
moveRocket
If the rocket hits an asteroid, then run this.
You’ve defined all the rocket code blocks. Now let’s combine them with other code. Drag in these blocks:
Keep repeating this code until the rocket reaches the right edge of the Stage.
Use the moveRocket code block to move the rocket left or right.
104
16. ADD ANOTHER ROCKET
should appear.
Rocketship icon. delete
You’ll control the new rocket by using different keys, and you’ll make it a different color. Edit the code like this:
block definition:
block definition:
Change: to:
try Challenge 4.
105
Catch the
Snowflakes
Rename the
as game .
4. RENAME IT
Choose a Backdrop
button.
5. ADD A TITLE
Choose the Click in the Drawing Area and then Adjust the size and position of the text.
Click the Text tool. Pixel font. type in the game title.
106
6. ADD MORE TEXT 7. DRAW ARROW KEYS
8. ADD A STAR
Pico Walking
sprite.
Click the Choose
score
a Sprite button.
Click Make a Variable .
Click
Variables .
Scroll to find
the Techno
icon. Click it
Check the box
Click the
to import that
next to the timer
Click the Click Choose
Code tab. Click the Sensing
sound.
code block.
Sounds tab. a Sound .
button.
type in showInstructions
has been running.
as a function name.
Make a Block
Click My Blocks .
A pink block called
showInstructions should
showInstructions
appear in the Coding Area.
107
13. ADD CODE TO DEFINE THE BLOCK
Click Make a Block and then type in Drag in this code to get things ready for the start of the game:
Make a Block
startGame
page 59 to review how to create a new message.
Click OK .
Click Make a Block and type in Drag in this code to program the player’s movement when the arrow keys are pressed:
Make a Block
block is used.
handleKeys
then run this code.
the right.
108
16. DEFINE THE GAMEOVER BLOCK
Click Make a Block and then type in Drag in this code to define what happens at the end of the game:
Click OK .
You’ve defined the blocks you need to start the game and move Pico. Now let’s
put them all together with other code. Drag in these blocks:
Show the instructions and wait for the player to start the game by
109
18. ADD SOUND AND ANIMATION
Test your code! You should see the instruction screen appear.
Press the left and right arrow keys to try moving the player around.
Click the
Snowflake
in startSnowflake as a
startSnowflake block is used.
function name.
Click Make a Block and then Drag in this code to get things ready for the start of the game:
type in checkIfCaught as
Make a Block
If the snowflake is caught by Pico,
then do this.
checkIfCaught
Start the snowflake in a new place.
Click OK .
Play a sound effect.
110
23. COMBINE THE SNOWFLAKE CODE Now let’s use the
blocks you made.
Drag in these blocks to bring the snowflake code together:
Repeat the following code until the snowflake reaches the edge of the Stage.
Rotate it slowly.
duplicate
duplicate
delete
delete
In the Sprite List, right-click Click duplicate . Duplicate another to make three
Click the green flag to test your code. Use the left and right arrow keys to collect as many snowflakes as you can.
111
chapter 5
challenges
112
CHALLENGE 3 FANTASTIC FUNCTIONS
CHALLENGE 5 SPLASHDOWN
113
updates
Visit https://nostarch.com/25scratchgames
M
The Official Scratch Coding Cards
ISBN: 978-1-59327-976-9
Super Scratch
Programming Adventure!
ISBN: 978-1-7185-0012-9
by Anna Anthropy
ISBN-13: 978-1-59327-936-3
Mission Python
by Sean McManus
ISBN: 978-1-59327-857-1