0% found this document useful (0 votes)
533 views12 pages

Essentials Scratch v1

This document provides an introduction to coding with Scratch on the Raspberry Pi. Scratch allows users to code by dragging and dropping blocks representing commands, avoiding complicated syntax. It comes preinstalled on the Raspberry Pi and can be used to create games, animations, and interact with electronics components. The screen is divided into areas for sprites, scripts, blocks, and the stage. Motion, looks, sound, pen, control, sensing, operators, and variables blocks allow coding sprite behaviors and interactions.
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
0% found this document useful (0 votes)
533 views12 pages

Essentials Scratch v1

This document provides an introduction to coding with Scratch on the Raspberry Pi. Scratch allows users to code by dragging and dropping blocks representing commands, avoiding complicated syntax. It comes preinstalled on the Raspberry Pi and can be used to create games, animations, and interact with electronics components. The screen is divided into areas for sprites, scripts, blocks, and the stage. Motion, looks, sound, pen, control, sensing, operators, and variables blocks allow coding sprite behaviors and interactions.
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/ 12

ESSENTIALS

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

FIND US ONLINE raspberrypi.org/magpi GET IN TOUCH [email protected]

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 ]

Tabs: Click the tabs


to choose between
changing a sprite’s
scripts, costumes,
or sounds

The Blocks Palette:


This is where you
find the commands
to control your
sprites. Click the
rounded buttons
at the top to
switch between
the different types
of blocks

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

et things moving with Scratch! In a matter of minutes, you can


G build your first program to move the Scratch cat around the
screen using the up, down, left, and right cursor keys. When
you learn more later, you’ll be able to develop this simple program into
an art package with the cat as the pen, a game (where should the cat
go?), or anything else that needs keyboard-controlled movement. As
you work through this chapter, you’ll learn how the Scratch screen is
carved up, so you can easily find what you need as you build the other
[ KEEP UP projects in this book.
TO DATE ] If you’re itching to write your own games or start building your own
Get the latest electronics projects, Scratch is the perfect place to start.
version of
Its simplicity comes from the way you select commands from a menu
Scratch by
updating your and join them together like jigsaw pieces. Because Scratch comes with
operating system a collection of images and sounds, you can start making your first
using: sudo apt-
program in minutes.
get update &&
sudo apt-get Scratch’s power comes from the many creative ways in which you
upgrade can combine the commands to make your own program.

[ Get Started with Scratch ] 7


ESSENTIALS

Finding your way around


The screen is divided into a number of panes, highlighted in our
diagram on the previous pages.
Images that you can control in Scratch are called sprites. You can
[ WHICH
make them move, draw on the screen, respond to clicks, change their
VERSION? ]
appearance, and interact with each other. A space game might have
If you’re using
online tutorials,
an alien sprite, a space ship sprite, and a missile sprite, for example.
check they’re Many projects have more than one sprite, and you can choose between
compatible with them by clicking them in the Sprite List, in the bottom right. Every
Scratch 1.4. The
newer Scratch
new Scratch project includes the Scratch cat.
2.0 for PCs and When you test your program, you’ll watch your sprites on the Stage,
Macs is based on in the top-right of the screen. Your games are more enjoyable when
Flash and won’t
they fill the screen, though, so when you’re ready to play properly,
work on the Pi.
click the easel icon on the right above the Stage to zoom in.
To make your sprites do something, you have to give them
instructions that tell them precisely what to do and when. Those
instructions come in the form of blocks that join together. The blocks
are sorted into eight categories:

Right: Scratch
comes with a
library of sprites
to choose from,
including these
fantasy sprites

8 [ Chapter One ]
[ LEARN
[ LEARNTOTOCODE
CODEWITH
WITHSCRATCH
SCRATCH] ]

Motion: Used for moving sprites around the Stage.

Looks: Used for animating sprites, giving them speech bubbles,


and changing their size and appearance.

Sound: Used for playing recordings or musical notes.

Pen: Used to draw as a sprite moves around the Stage. Great


for making random art, and for special effects in games.

Control: Used to describe what happens when, and for making bits
of your program repeat.

Sensing: Used to test whether your sprite is touching another sprite


or another color, or to get information about other sprites.
You can also use the sensor value blocks in your own
electronics projects on the Raspberry Pi.

Operators: Used for maths, random numbers, and doing things to text.
There are also blocks here for combining the blocks used
in decision making.

Variables: Used to remember information, such as scores, timer


values, or player names.
[ HAT
You can find all the blocks in
BLOCKS ]
the Blocks Palette on the left The blocks with
a curved top, like
of the screen. The blocks are when space
colour coded, so when you’re key pressed,
copying programs from books or are called hat
blocks. They can
magazines you can find the blocks only join at the
you need more easily. top of a script.
In the middle of the screen
is the Scripts Area. This is Left: The hat
where you make your lists blocks in the
Control part of the
of instructions (or ‘scripts’) Blocks Palette can
for your sprites. be used to start
your scripts

[ Get Started with Scratch ] 9


ESSENTIALS

Making your first Scratch script


We promised you could make your first Scratch
script in minutes, so here we go!

>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?

[ Bouncy[[ High fliers ]


Hedgehog
High Fliers 11
ESSENTIALS

There are some


great fantasy
sprites included in
Scratch, including
this purple
hedgehog-like
creature!

Move the
trampoline left
and right to catch
the hedgehog and
bounce it back up
in the air

n this chapter, you’ll make your first Scratch game, in which


I you use the cursor keys to move the trampoline left and right
to catch a bouncing target. This project shows you how to
bring in new sprites and backgrounds, and how to use the bracket
blocks and diamond blocks in your projects. You’ll find these skills
useful as you build the other projects in this book. Start a new Scratch
project, and get ready to bounce! Remember you can refer back to the
last chapter if you need help finding your way around the screen.

>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

(Make sure you don’t use the


repeat block with a number in it).
Next, you need to drop a < block
into the diamond-shaped hole.
Click the Operators button above
the Blocks Palette to find it. Type
-120 into the box on the right.
Finally, click the Motion button
and drag the y position block into
the left box. Now, whatever we put
inside the repeat until bracket
will be repeated until the sprite’s
y position (how far up or down the
screen it is), is less than -120. In
our game, that means it’s missed
the trampoline and hit the floor.

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

You might also like