Essentials Scratch v1
Essentials Scratch v1
LEARN TO
CODE WITH
SCRATCH
MAKE SIMPLE GAMES AND APPLICATIONS
ON
YOUR Raspberry Pi
Written by The MagPi Team
WELCOME TO
LEARN TO CODE
WITH SCRATCH
oding doesn’t just have to be about typing
C in line after line of gobbledygook. Created
by the boffins at MIT, Scratch enables
anyone – children and adults alike – to start
programming within minutes, without any prior
knowledge. You simply drag and drop various code
blocks and link them together like jigsaw pieces
to form logical scripts, unobstructed by confusing
jargon and tricky syntax. Even better, Scratch is
included as standard in the Raspbian operating
system for the tiny Raspberry Pi computer. It can
even be used with the Pi’s GPIO pins to interact
with electronic components and sensors.
In this book, we’ll help you start coding with
Scratch, guiding you step by step through the
process of creating all sorts of projects: games,
animations, quizzes, electronics circuits, and more.
It’ll be educational and also a lot of fun.
Phil King
Contributing Editor, The MagPi magazine
EDITORIAL DESIGN
Managing Editor: Russell Barnes Critical Media: criticalmedia.co.uk
[email protected] Head of Design: Dougal Matthews
Contributing Editor: Phil King Designers: Lee Allen, Mike Kay
Sub Editors: Lorna Lynch and Laura Clay
Contributors: Sean McManus, William Bell & Code Club
THE MAGPI SUBSCRIPTIONS
DISTRIBUTION Select Publisher Services Ltd
Seymour Distribution Ltd PO Box 6337, Bournemouth
2 East Poultry Ave, London BH1 9EH | +44 (0)1202 586 848
EC1A 9PT | +44 (0)207 429 4000 magpi.cc/Subs1
In print, this product is made using paper This book is published by Raspberry Pi (Trading) Ltd., Mount Pleasant House, Cambridge, CB3
sourced from sustainable forests and 0RN. The publisher, editor and contributors accept no responsibility in respect of any omissions
the printer operates an environmental or errors relating to goods, products or services referred to or advertised in this product. Except
4 [ management
Chapter One ] which has been
system where otherwise noted, content in this magazine is licensed under a Creative Commons Attribution-
assessed as conforming to ISO 14001. NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0).
ESSENTIALS
ESSENTIALS
[ CHAPTER ONE ]
GET STARTED
WITH SCRATCH
Fancy yourself as Disney or Miyamoto? Whether your
inspiration is Mickey Mouse or Mario, Scratch helps
you to bring your creations to life…
6 [ Chapter One ]
[ LEARN TO CODE WITH SCRATCH ]
Scripts Area: Assemble The Sprite List: Select your sprites The Stage: Watch
your programs here by here, so you can change their scripts or your sprites move
dragging blocks in from costumes. Click the Stage in the Sprite and interact here
the Blocks Palette and List to add scripts to it or change its
joining them together background
Right: Scratch
comes with a
library of sprites
to choose from,
including these
fantasy sprites
8 [ Chapter One ]
[ LEARN
[ LEARNTOTOCODE
CODEWITH
WITHSCRATCH
SCRATCH] ]
Control: Used to describe what happens when, and for making bits
of your program repeat.
Operators: Used for maths, random numbers, and doing things to text.
There are also blocks here for combining the blocks used
in decision making.
>STEP-01
Move 10 steps
When you open Scratch (it’s listed under
Programming in your Start menu), it shows the
Motion blocks in the Blocks Palette. Click the move
10 steps block here and you’ll see the cat move
on the Stage. Each time you click, it only moves
once. That’s because ‘10 steps’ is how far it moves,
and not how many times. You can click on the 10
and type a different number in here to make it go
further or less far with each click. Drag and drop
the move 10 steps block in the Scripts Area.
>STEP-02
Combining blocks
Drag the point in direction 90 block into the
Scripts Area. If you drop it just above the move 10
.01 steps block, they’ll lock together. Look for the white
line that shows they’re about to join before releasing your mouse button. If
[ GET ARTY! ] you click either of the blocks, Scratch will carry out the instructions in order,
Can you add first pointing in direction 90 (facing right) and then moving 10 steps. Click
controls for the Control button above the Blocks Palette. Drag in the when space key
pen up and pen pressed block and join it to the top of your two blocks. Your sprite will move
down so you can
use this program to the right (direction 90) when you press the space bar.
to draw on
the Stage? >STEP-03
Making keyboard controls
Right-click your script and choose Duplicate. Click on an empty space
in the Scripts Area to drop your copied script. Repeat until you have four
identical scripts. Let’s turn them into cursor key controls. Click ‘space’
in the first block to open the menu and choose ‘up arrow’. In the point
in direction block below, click ‘90’ and choose ‘0’ (up). Now when you
press the up arrow, the cat moves up the screen. Edit the other scripts to
add controls for left, right, and down. Listing 1 shows the finished code.
10 [ Chapter One ]
[ LEARN TO CODE WITH SCRATCH ]
ESSENTIALS
[ CHAPTER TWO ]
BOUNCY
HEDGEHOG
Spike the hedgehog loves playing on the trampoline,
but he’s a bit clumsy. Can you move the trampoline
to stop him landing with a bump?
Move the
trampoline left
and right to catch
the hedgehog and
bounce it back up
in the air
>STEP-01
Prepare your artwork
For this Scratch project, you don’t need the cat, so right-click it in the
Sprite List and then choose Delete. To add a new sprite, click the icon
above the Sprite List that shows a folder and a star. Add the trampoline
sprite from the Things folder, then the fantasy11 sprite in the Fantasy
folder. Let’s change the background: click the Stage in the Sprite List
and the Costumes tab changes to a Backgrounds tab. Click the tab and
use the Import button to bring in your choice of background. We’re
using the image atom-playground in the Outdoors folder.
12 [ Chapter Two
One ]
[ LEARN
[ LEARNTOTOCODE
CODEWITH
WITHSCRATCH
SCRATCH] ]
>STEP-02 .01
Adding player controls
Click the trampoline (which should
be Sprite1) in the Sprite List to
select it, and then click the Scripts
tab above the Blocks Palette.
Listing 1 shows the scripts you
need to add to this sprite. Work
your way down them, dragging
the blocks into the Scripts Area
one at a time and joining them up.
Click the white holes in the blocks
and type the right numbers in.
Remember that the colours are a
clue: to find the yellow blocks, click
the yellow Control button above
the Blocks Palette first.
.02
>STEP-03
Set up the hedgehog
Click Sprite2 in the Sprite List
(the hedgehog). Add the script
shown in Listing 2 to it. This puts Below: Right-
click the sprite in
the sprite in the top left when the
the Sprite List to
game begins, and gives the player delete it. Note the
buttons to add a
a chance to spot it before it moves.
sprite above the
cat here too
>STEP-04
Add a repeat loop
We’re going to extend that script
now by adding some more blocks
at the bottom. Listing 3 (overleaf)
shows the entire script, including
the bits you’ve already done.
Click the Control button above
the Blocks Palette. Drag a repeat
until block into the Scripts Area
and join it to your script so far.
[ Bouncy Hedgehog ] 13
ESSENTIALS
Above:
The Operators >STEP-05
blocks include the Make the hedgehog move
block for picking
random numbers, To make the sprite move, add the two Motion blocks shown in Listing 4
and the blocks into the repeat until block in your script. Click the green flag above
for comparing
numbers the Stage to test it so far. You should see the hedgehog go to the top
left, plummet down, and stop when it reaches the bottom.
.03 .04
14 [ Chapter Two
One ]
[ LEARN
[ LEARNTOTOCODE
CODEWITH
WITHSCRATCH
SCRATCH] ]
.05
>STEP-06
Make the trampoline bouncy
We need to make the hedgehog bounce back up again if it touches the
trampoline. Click the Control block and drag an if block into your
script. Be careful with where you put it: it belongs inside your repeat
until bracket, as shown in Listing 5. Click the Sensing button and drag
in a touching block for the diamond hole in your if block. Click the
menu in the touching block to choose Sprite1 (the trampoline). Inside
the bracket of your if block, put a point in direction 90 Motion
block. Instead of putting a number in its hole, this time we’ll use pick
random with values of -45 and 45. You’ll find it in the Operators section
of the Blocks Palette. Now the sprite will point in a random upward
direction (between 45 degrees left and 45 degrees right) if it touches
the trampoline. Finally, add a say block at the end of your script,
outside all the brackets. This is shown when game ends.
[ Bouncy Hedgehog ] 15