Академический Документы
Профессиональный Документы
Культура Документы
asp be r ry P
R ANNUAL 2018 i
80
Pages of fun
Raspberry Pi
stuff for kids!
What’s inside?
MINECRAFT l SCRATCH l PUZZLES l COMIC & MORE!
First published in 2017 by Raspberry Pi Trading Ltd, Station Road, Cambridge, CB1 2JH
ISBN: 978-1-912047-70-3
Printed in Lithuania
Basics
Check off all your kit,
set up your Raspberry
Pi, and install the
software for fun!
Starting page 6
Comic
The evil Doctor
Bluescreen is uploading
all the kids. Can the Pi
Crew save the day?
Starting page 51
den 12
We’ve hid Pi logos
ry
Raspber he book.
aroun d t
spot
Can you ?
the m all
Coding Features
Begin with Scratch
and move on to Python
with our fun Pi coding Getting Started
projects! 6 We help you set up your Raspberry Pi
and install the software you will need
Starting page 14
Scratch: Learn the Basics
14 Use Scratch’s drag-and-drop coding
interface to bring your Pi to life
Pi Personality Test
Test your brains with
our Pi Wordsearch,
48 Test your thoughts on Raspberry Pi
and find out what type you are!
Spot the Differences,
Maze and more! Comic Capers
Starting page 13 51 The Pioneers of Pi come together to
save the kids from Doctor Bluescreen
tt ing
Ge rted…
St a ur Raspberry Pi to life!
ng yo
Bringi
What you will learn
at! There’s a
In this beginners’ feature we will
e rr y P i? G re
Got a Raspb
show you…
n
erf u l wo rld o f computing fu
wond
o u . L e t’s g et started by lH ow to select the right equipm
ent for
waiting for y h a ve a ll the cables your Pi adventure
y o u
making sure o u w il l need, and
l How to connect everything
together
r ie s y
and accesso g them all in.
correctly before you start
u h o w to p lu
showing yo
l How to download and set up
Pi adventure
the
n h a ve y o u r software you will need
We will soo
g…
up and runnin
l need
What you weailch in
explain
We’re going to d
are the bits an
detail, but here ed …
you ne
pieces of tech
lR aspberry Pi
TV
l Monitor or
lH DMI cable
lU SB keyboard
l USB mouse
lP ower supply
rd
l microSD ca
1. The Raspberry Pi
The Raspberry Pi 3 is the third version
of Raspberry Pi. It replaced the
Raspberry Pi 2 Model B in February
2016. New features include…
2. Monitor or TV
A monitor or TV with HDMI in
can be used as a display with
a Raspberry Pi. This is the
quickest and easiest way to se
e
what your Pi is thinking. How
big
a screen you want is up to yo
u!
.
4 USB Keyboard
t
e y b o a rd is u s ed to input tex
A USB k p to p or a Raspbe
rry
m p u te r, la
into a co w il l work, so you
k e y b o a rd
Pi. Any ia l one. Plug-and
-
e d a s p e c
don’t ne ill w ork without an
y
b o a rd s w
play key S im ply plug one
a l d riv e r.
addition y P i and it should
be
b e rr
5. USB Mouse
into the R as p
.
en it starts up
recognised wh
6. Power Supply
it’s recommended that you use a
a Ra sp be rry Pi 3, the n
If you’re using ls ca n be powered using a current
r su pp ly. Ea rlie r mo de
5 V, 2.5 A powe e a po wer supply which you know
Ide all y, yo u wa nt to us
as low as 1.2 A. po we r to the Pi. You can buy
wh ich pr ov ide s en ou gh
to be safe and or yo u can use a 5 V micro USB
be rry Pi po we r su pp ly,
the official Rasp e de vices. You do need to check th
at
d us ed by ma ny mo bil
charger, the kin vo lta ge and current (5 V / 1.2–2.5 A).
ly pr ov ide s the rig ht
your power supp
1 2
6 4
The Raspberry Pi Annual 2018 9
Feature
9. Connecting to the
t
Int
to
e r n et
to your
co n n
Pi
ect your Raspberry following
ly w an
You will probab te rnet . Y o u can use any of th
e
r the in
local network o
…
options to do this
Ethernet
l Connecting via E ther net p o rt, alongside the U
SB
has an int, or
The Raspberry Pi ed clo se to a router, access po
ports. If your Pi is
situ at
wo rk u si n g an E thernet cable.
nnect to a net
switch, you can co
net ca b le in to the Raspberry Pi
ed the Ether aspberry Pi will
Once you’ve plugg ss p oin t, yo u r R
into an acce
and the other end rk.
to m atic ally co n nect to the netwo
au
WiFi
l Connecting via , it h as b uilt-i n w ireless L AN. If
berry Pi 3 ry Pi, then you
If you have a Rasp n o f th e R as p b er
rlier versio gles, when used
you’re using an ea . S o m e W iF i d o n
iFi dongle es.
will need a USB W are si m p le p lug-and-play devic
Pi, be compatible
with the Raspberry rs , an d m ay n o t
ecific drive
Others require sp M ak e su re you read the devic
e
ry P i.
with the Raspber en ta ti o n b ef ore buying one.
o cu m
manufacturer’s d
l Bluetooth speakers
With either the Raspberry Pi 3 or a Bluetooth dongle, you can conn
ect to Bluetooth
speakers or headphones. Your success rate may vary depending
on the dongle
and/or speakers you’re using, so ensure that you read the manufactu
rer’s
documentation before you buy.
l Downloading NOOBS
Using NOOBS is the easiest way to install
Raspbian on your SD card. To get hold of a
copy of NOOBS…
Mad Pi Das
h Who is plugge
d into the
Pi?
Everyone wants a piece of Raspbe
rry Pi! Here are five characters from
of Pi. All of them want to plug in the world
and create a cool project, but whi
connected? Trace the cables to ch one is
find out!
T
BO
BUILDER
HA
MS
T
RATCH CA
SC
TE
R
T IM
PEAK
E
The Raspberry Pi Annual 2018 13
Scratch Coding
B asics of…
n t he
L ear
cratch 2
S
ate stories, games and animations!
is u a l p ro g ra mming tool
Scratch is a v v ery easy-to-u
se
atu res a
which fe
p in te rf a ce . It enables you
drag‑and-dro n c om p uter games,
r o w
to create you s , a n d a nimations us
ing What you will learn
to r ie
interactive s in g te c h niques witho
ut By following the steps in this
m m
some progra write code. T
his feature, you will learn…
h av in g to e
actua lly
h e lp g et y ou started with th
feature will
lW hat all the buttons and too
lbars in the
tch. Scratch window do
basics of Scra l How to use blocks to make
the Scratch
cat move about
l How to change sprites
lH ow to create your own sprites
l need
What you wcril
atch 2.
quires S
This tutorial re
ed a Raspberry
To use it, you ne rsion of
g the latest ve hee-hee!
Pi 2 or 3 runnin em , plus a i can’t wait
erating syst
the Raspbian op d m ou se. to get started
keyboard an
standard USB in scratch!
1. Open up Scratch
You’ll find Scratch 2 in Menu > Programming. Once
opened, you will see a window like this…
2. You will see that the cat has two costumes. Right-click
and select duplicate to make a third costum costume 2
e.
3. Select costume3 and it will appear in the Paint Editor.
Experiment
with all the buttons and tools to find out wha
t they do.
4. Next, draw some clothes on the costume and click OK.
5. To switch between costumes, click on the Scripts tag
. Add the
purple Looks block, switch to costume, to the
other blocks and
connect it.
te.
1. At the top of the Sprites palette are four icons to create a new spri
opens a
2. The first allows you to Choose sprite from library. This
t-in sprites.
window where you can choose one of Scratch’s buil
t Editor. Here you
3. The second icon, Paint new sprite, opens the Painmak e your own
can use shapes, lines, and freehand drawings to
custom characters. Have fun!
4. The third icon lets you Upload sprite from file, to use an existing
image or Scratch sprite.
5. he fourth icon enables you
T
to create a New sprite from
camera (if one is connected).
Done
scratch is
a great
What next?
introduction Now you know the basics of
at
to coding! Scratch, there are lots of gre
projects you can hav e a go at…
Antenna
l Try completing the Robot
k!
Resource to make an LED blin
dy sweets
l Create a button using can
n Game.
with the Sweet Shop Reactio
nd on the
These and more can be fou
rni ng Res our ces
Raspberry Pi Lea
website at…
www.raspberrypi.org
Tactile Push
Button
Breadboard 330R
PIR Sensor Resistor
LED
Piezo Buzzer
1. GPIO pins
One powerful feature of the Ra
spberry Pi
is the row of GPIO pins along
the top edge
of the board. GPIO stands for
General-
Purpose Input/Output. These
pins are a
physical interface between the
Raspberry Pi
and the outside world. At the
simplest level,
you can think of them as switc
hes that you
can turn on or off (input) or th
at the Pi can
turn on or off (output).
2. Lighting an LED
You can test whether your GPIO
pins and LEDs are working by bu
circuit below. You can use any res ilding the
istor over about 50Ω.
1. T he LED is connected directly to the GN
D pin, and the 3V3 pin via the
resistor, and should light up.
2. Be sure to
connect your
LED the correct
way round;
the longer
leg should be
connected to
the 3V3 pin…
Done
4. Consttcruhcting a
program
Scra 5. In the More Blocks section,
you’ll now see two additional
1. Locate the Scratch program by blocks for controlling and
clicking on Menu followed by
Programming, and selecting responding to your Pi GPIO
Scratch 2. pins. You can change the pin
number by selecting the round
2. The familiar field and typing it in.
Scratch
interface will
then load…
3. Click on
Events
from the 6. With set gpio 17 to output
blocks high or low, you can turn on
palette. your LED attached to GPIO
Drag the pin 17 on or off. Using two of
hen green flag clicked block
w these inside a forever block,
onto the scripts area… with wait blocks to add a
delay, you can make the LED
flash continuously…
Done
2. Connect your
button to a
breadboard, then
connect one pin
to a ground pin
and the other
to a numbered
GPIO pin. In this
example pin 2 has
been used…
Done
Done
Done
What next?
ngs you
There are lots of other thi
h your
can control or monitor wit
k at the
Raspberry Pi. Have a loo
ite to see
worksheets on the webs
ne…
how easily this can be do
r
l Using an active buzze
l Making traffic lights
lU sing a PIR sensor
a
step as you go!
Sa nt
De te ctor s E v e!
Christma
an catch on
ho youc
See w how dare
you think you
could ever
catch me!
ping
No more prop
n with
your eyes ope
to try to
matchsticks
his Scratch
catch Santa! T
a low-cost,
program uses
ctor to set
infrared dete
hen Santa
off an alarm w room.
our
sneaks into y
oduction
It’s a great intr ensors
le s
to using simp
erry Pi, and
on the Raspb
d to lots of
can be adapte
cts.
different proje
Hardware
l1 x passive infra-red sensor
l3 x female-to-female jumper leads
Software
lU p-to-date SD card image What you will learn
with
lS cratch 1.4 By creating a Santa detector
rn…
your Raspberry Pi you will lea
e infrared
l How to connect a passiv
y Pi
(PIR) sensor to the Raspberr
r
lH ow to control the flow of you
m by res pon din g to the
Scratch progra
input from the sensor
Connect the top one labelled VCC on the PIR sensor to the 5V
pin on the Raspberry Pi, connect the middle one labelled OUT to
the GPIO 4 pin, and connect the bottom one labelled GND to a
ground pin also marked GND. All shown in the following diagram…
Done
Done
3. Dr
an
a
d
w
ad
a
d
sp
s
r
o
ite
und
draw
es tab and
Done
e C o s tu m
Click on th
. T his w il l be displayed
te
a Santa spri enses movement.
s
when the PIR
o u n ds ta b and import a
S
Click on the E le c tr o nic folder. We
th e .
sound from e n c a lle d Whoop here
s ir
have used a
4 . Program wh at
hen
happens w
the detector ent
spots movem
e h ave a se nso r that reports
Now that w e c an use this value
o n o r o ff, w
when it is .
c o n trol th e fl ow of our program
to
g script…
Build the followin
Done
5. inSetyou
up the detector
r bedroom Disclaimer!
! We cannot guaran
tee that
this alarm will no
l On Christmas Eve, set up your Pi t scare
Santa off so that
you get
with the sensor pointing at your no presents at al
l, not even
a wrinkled satsum
bedroom door old sock! Sorry ab
a in an
out that.
e Diff erence
Spot spber
th
ry Pi?
om the Ra
ssing fr
t’s mi
Wha
2
time in the box below
Picture
The Result!
How quickly did you
manage to solve the
puzzle? Write in your
time below…
F M G O B K L S W M
S I W Y I L T D G H C K J L
B C O M P U T I N G I R S R B
S W A G C O O Z P O A D K
U I S V Y W Y W I T C
M Q F T Y R T Q P C A
P G X O I R E O R H H R F
A S C R O E N O U M Q G D F Q
G R F Z B F R C P L E H W D B
T P J P G U D N L I T M Z X R
P F S F S M H T I R O G L A S
M A A E G Y U H P E B N E R L
R A V R F N F N F M O P O J W
J R P C O I V V H R S D F The Result
J G V E R M T P N X I !
How quickly
did
manage to so you
Find the
answers on page K H O Y N Y A E T T A puzzle? Write
lve the
in your
78
time below…
C R P I S G X
P V M
ding wit h
Co
inecraf t
M amming the game on the Pi!
Progr
What you will learn
By following this project with
popular your Raspberry Pi,
Minecraft is a rl d building
you will learn…
o x o p e n -w o
sandb
version of l How to access Minecraft Pi
game. A free
and create a new world
l need
What you wilPi running
aspberry
You’ll need a R raft
pb ian, w hich includes Minec
R as the latter,
de faul t. If yo u are missing
Pi by from…
n download it
however, you ca /p i
n-us/edition
minecraft.net/e
Tick off each
step as you go!
Key Action
E Inventory
SPACE Jump
Double
SPACE Fly/Fall
ESC Pause/
Game menu
TAB
Release mouse
cursor
Done
1. Find your location
To find your location, type…
pos = mc.player.getPos()
yo ur loc ati on ; ac ce ss ea ch pa rt of the set of coordinates
pos now contains
with pos.x , pos.y and pos.z .
e wa y to ge t the co ord ina tes into separate variables is to
Alternatively, a nic
e…
use Py thon’s unpacking techniqu
x, y, z = mc.player.getPos()
d z
, an d z co nta in ea ch pa rt of your position coordinates. x an
Now x , y
and lef t/right) and y is up/down
.
lki ng dir ec tio ns (fo rw ard /ba ck
are the wa
if you
t ge tPo s() ret urn s the loc ati on of the player at the time, and
Note tha location.
po sit ion yo u ha ve to ca ll the function again or use the stored
move
Air: 0
Grass: 2
Dirt: 3
ght, try
Now with the block in si
else…
changing it to something
2)
mc.setBlock(x+1, y, z,
Stone
You should see the grey
your eyes!
block change in front of Done
4. Block constants
You can use built-in block constants to set
your blocks, if you know their names. You’ll
need another import line first, though. iable
5. Block as vIDarof a block,
from mcpi import block If you know the
l to set it as
it can be usefu
can use the
Now you can write the following to place a variable. You
ger ID.
a block… name or the inte
6. Special blocks
There are some blocks that have extra properties,
such as Wool which
has an extra setting where you can specify the colo
ur. To set this use the
optional four th parameter in set.Block …
wool = 35
mc.setBlock(x, y, z, wool, 1)
2: Magenta
3: Light Blue
4: Yellow
stone = 1
x, y, z = mc.player.getPos()
z+11, stone)
mc.setBlocks(x+1, y+1, z+1, x+11, y+11,
ne.
This will fill in a 10 × 10 × 10 cube of solid Sto
Done
tnt = 46
mc.setBlock(x, y, z, tnt)
tnt = 46
mc.setBlock(x, y, z, tnt, 1)
tnt = 46
mc.setBlocks(x+1, y+1, z+1, x+11, y+11, z+11, tnt, 1)
Done
lN etworked game
from mcpi.minecraft import Minecraft t Raspberry Pis
If multiple people connec
n join the same
from time import sleep to a local network, they ca
together. Players
Minecraft world and play
Minecraft world.
can see each other in the
mc = Minecraft.create()
l API reference
mentation of
x, y, z = mc.player.getPos() For a more extensive docu
block IDs, visit
functions and a full list of
bit.ly/MinecraftAPI
lava = 10
water = 8 lM ake a game
and make a Whac-
Try out another resource
air = 0 a-mole game: Minecraft
Whac-a-Block.
You can
adj ust the sle ep
parameter s to
all ow mo re or
les s lava
to flow Done
S p ot th e
ff er en ce …
Di e a trip to outer space!
Tak
Timed Challenge!se two pictures of the
s between the
There are 12 difference h, then mark all the
space. Set a stopwatc
Raspberry Pi gang in and write in your time.
And then it’s
d on pictur e 2 Picture
1
differences you fin
to see if you got it right!
time to jump to page 78
Raspberry Pi in space!
Did you know there are two
Raspberr y
Pis on the International Sp
ace Station?
Learn more at astro-pi.org
!
Picture
The Result!
How quickly did you
manage to solve the
puzzle? Write in your
Find the time below…
answers on page
78
The Raspberry Pi Annual 2018 41
Babbage versus bugs
b bage
Ba us Bugs
vers ing Space Invaders clone!
ur excit
o
Code
It’s much
ct
Download the proje load’
1.
more rewarding
to write the code
or down
press the green ‘Clone yourself! Press the
Go to goo.gl/PyzcFA,
lect ‘Download ZIP’. menu button, go
link on the right and se
to Programming
Set up the files and select Thonny.
3.
or a text editor. project folder you
and open with Thonny
Right- click on bugs.py t the code, then downloaded with
code listing. Check ou
You’ll find the following to play it! ‘.py’ at the end of
ject (page 45) see how
go to the end of the pro the file name.
bugs.py
from random import randint as rand Set sizes
Let’s set the reso
lution
WIDTH = W = 640 and name our ga
me.
HEIGHT = H = 480
TITLE = "Babbage vs Bugs"
24)]
P = [(0,1,240), (-1,0,112), (0,1,32), (1,0,224), (0,1,32), (-1,0,2
F = [lambda x, y : 0,
lambda x, y : y<1,
lambda x, y : y==1,
lambda x, y : y>1,
lambda x, y : x&1,
lambda x, y : (x^y)&1,
lambda x, y : 1]
s.kind = kind
s.life = kind*2
s.time = 0
def update(s):
s.time -= 1
s.x += P[state.pc0][0]
s.y += P[state.pc0][1]
c = s.collidelist(state.beams[0])
if c >= 0:
state.beams[0][c].h = 1
s.life -= 1
g!
if s.life == 0: t a pesky bu
Yes! We’ve hi li fe
state.score += s.kind*10 remove a
We need to
1
with s.life -=
s.time = 5
if rand(0, 399) == 0:
state.beams[1].append(Beam(s.pos, 3))
class Star(Actor):
def __init_ _(s):
super().__init_ _("star", (rand(0, W-1), rand(0, H-1)))
s.v = rand(1, 3)
def update(s):
s.y -= s.v
if s.y < 0:
s.x = rand(0, W-1)
s.y += H
class Beam(Actor):
def __init_ _(s, pos, v):
super().__init_ _("beam", pos)
s.v = v
s.h = 0
def update(s):
s.y += s.v
class Player(Actor):
def __init_ _(s):
s.time0 = 0
Update me
s.time1 = 0 The update met
hod is
s.life = 5 where we check
for button
presses to move
Babbage left
and right, and co
def update(s): llisions to see
if he’s been hit by
s.time0 -= 1 a bug.
s.time1 -= 1
c = s.collidelist(state.beams[1])
if c >= 0 and s.time0 < 0:
state.beams[1][c].h = 1
s.life -= 1
s.time0 = 5
class State:
def __init_ _(s):
s.bugs = []
s.beams = ([], [])
s.stars = [Star() for s in range(30)]
s.player = Player()
s.score = 0
s.space = 0 Classy
The class Stat
e sets
itialising
s.wave = 0 up our game, in
, our player,
things like bugs
oard.
def update(s): and the scoreb
if len(s.bugs) == 0:
for y in range(3):
for x in range(7):
s.bugs.append(Bug((W/2+x*60-180, y*60-180), 2
if
F[min(s.wave, 6)](x, y) else 1))
s.pc0 = 0
s.pc1 = 0
s.wave += 1
for a in s.all():
a.update()
s.pc1 += 1
if s.pc1 == P[s.pc0][2]:
s.pc0==5 else s.pc0+1
s.pc0 = 2 if
s.pc1 = 0
def all(s):
s[0]+s.beams[1]+s.bugs+[s.player]
return s.stars+s.beam
state = State()
def update():
global state
if state.over():
not state.space:
if keyboard.space and
state = State()
else:
state.update()
e
state.space = keyboard.spac
def draw():
screen.clear()
for a in state.all():
a.draw()
for i in range(state.player.life):
*32, H-26))
screen.blit("life", (6+i
"consola",
bottomright=(W-8, H-3), fontname=
screen.draw.text(str(state.score),
fontsize=20)
if state.over():
(0, 0))
screen.blit("dark", me="consola",
screen.draw.text("GAM E OVER", center=(W/2, H/2), fontna
fontsize=100)
Ga m e
c re dits:
U p to n
,
4. Run the game!
Press the menu button,
Type: cd Downloads/
select Accessories, an
Annual-2018-master/b
d then Terminal.
ugs. Now type
Eben n me you gave your own
file). Enjoy!
u r e n ce va pgzr un bugs.py (or the na
La r en ,
S ome er
ld
S am A
The Raspberry Pi Annual 2018 45
Puzzles
ace Maze
Sp a trip toouter space!
Take
Timed Challenge! Find the
answers on page
!
The Resulut
did yo
How quickly
lve the W?
manage to so
puzzle? Write
in your DID YOU KNO
time below… O PIS ABOARD
TH E
TH E RE ARE TW TIO N!
AL SPACE STA
INTE RNATION AT…
LEARN MORE
PI.ORG
www.ASTRI-
46 The Raspberry Pi Annual 2018
Subscribe
you love
Do pberry Pi?
Ras
’ll love
You aspberry Pi magazine!
f i cialR
f
the o
s gu ides
Beginner’
a n d i n s p iration
Ideas
c t s to ma ke
Fu n p r oje
s t k it r ev iewed
The late
E S T U F F!
FRE for a year and get
ubscribe pberry Pi Zero
S
s
a FREE Ra ories!
s
and acces
er r y pi.o rg/magpi
Visit raspb 2 02 586848
(0 )1
or call +44
pb err y Pi
Ras ality Test
erso n
P n answer to ea
a
c
t
h
m
q
a
u
k
e
e
stion
s you tick!
hoose a find out wh
C to
1.
as Raspberry Pi project?
What are your Christm
rry Pi?
plans for your Raspbe
A. Hamster disco
B. Parent detector
A. Build a Santa detector C. WhooPi Cushion
ur sister
B. Teach it to fire darts at yo rk so
the da
C. Leave it on the floor in
re feet
Dad steps on it in ba
4.
le
y Pi broke. Raspberry Pi nig
Your Raspberr What happened?
htmare.
pen?
How did it hap
A. Killer robots
. m e te d out of a tree
A P
lu m B. Automatic homew
d in to a school dinner o rk-giving
B. Dro p pe machine
t
C. Fell in the toile C. Healthy eating pro
gram, with
lots of vegetable
s
6. What do you
think is the
best name for a
Raspberry Pi rob
ot?
A. Alfonse
B. Mr Fluffypants
C. Murderborg
7. 8.
your
You need to hide
m your
Raspberry Pi fro What robot would
o you
brother. Where d you most like to
conceal it? build with your Pi?
Scores
Each answer you give earns
you points! Answer A = 1 point,
B = 2 points and C = 3 points. Flip
the book over and find out how you
scored at the bottom of the page!
eel of Pi
nd
Wh
andround – find the hidden phrase!
Rou
ed C h a l lenge!
Tim f Pi, and it h
ides a spec
ial
This is the
Wheel o ecipher the
ry other letter to d
ev e it! The
phrase! Skip st 5 minutes to crack
R C
hav e ju
code – you bit!
t help you a
A
m igh
pictures
I
C
O
HEP
FDTE
N
T W
Find the
answers on page
MI I
78 The Result
!
How quickly
did
manage to so you
lve the
puzzle? Write
in your
time below…
Tu r tle y
A m az ing
thon with the Turtle!
atch to Py
rom Scr
f
Move What you will learn
rst
ro je c t y o u w ill take your fi e By making patterns with Python
In this p g ramming lan
guag code, you will learn…
it h th e p ro , and
steps w h a pes, patterns
to d ra w s ed
Python
l To take your first steps with the
Y o u w il l u s e a module nam Python programming language
spirals. w ay you will le
ar n lH ow to draw lines with Python Turtle
A lo n g th e
Turtle. e quences, and
use l How to make turns
th in k in s
how to s e quence. This
is l How to change the pen colour
re p e a t a
loops to sto ne from a vis
ual lH ow to use loops to repeat some
ste p p in g
a great ke Scratch to
instructions and create shapes
la n g u ag e li
programming t of Python.
l How to use more loops to create
d e n v ir o n m e n impressive spiral patterns
the text-base
a t yo u w ill need
Wh erry Pi set
up and learn how
Raspb u have
With your
to control me
tion of Ra spbian, yo using easy
an installa project.
ing you n eed for this python code!
everyth
Note
Make sure you do
n’t call
your Turtle projec
- that’s the name ts turtle.py
of the Python Tick off each
library you’re impo step as you go!
rting!
1. Is it art, maths, or
computer science?
ave a look at the image to the right.
H
How would you describe it? Is is art,
maths, or computer science?
2. Drawing a line
d
ag e ab ov e is m ade up of lines an
The im d, you need to
es ! To g et star te
only lin using a little bit
know h ow to d ra w a lin e Note
h o n co d e. B el o w is some code we You can run Pyth
of Pyt into Python – see
on code
like yo u to w ri te in a browser usin
g online
would s for experimentin
g services like Trin
te b ox fo r o p tio n ket (trinket.io), or
the no e code and then
load up Python on
your Raspberry
yt h o n . Ty p e in th Pi. You will find th
with P e latest versions
happens. in the Programm
Run it to see what
ing menu.
).
turtle = Turtle(
.
.screen
= Screen().
00).
turtle.forward(1
.
g.
u n to se e the code workin
1. C lic k o n R
the
. ch an ging the number in
2 N
o w tr y
rd(100).; click on
line .turtle.forwa
e what happens.
Run again and se Done
3. Turning
You’ve used code to draw a line. Good work!
Now let’s try making
the turtle turn around. To do this you need to
instruct the turtle not
only to move forward, but also to turn right
or left.
.turtle = Turtle().
.screen = Screen()
.
.turtle.forward(100).
.turtle.right(90).
.turtle.forward(100
).
Done
Challenge
Try to complete each of the
challenges below…
.screen = Screen
().
.screen.colormod
e(255).
from turtle import Turtle, Screen
.
.R = 255.
.
.G = 0.
.screen = Screen
().
.screen.colormod
e(255).
.turtle.color((R, G, B))
.R = 255.
.turtle.forward(
100)
.G = 255.
.turtle.right(12
0)
.B = 0.
.turtle.forward(
100).
screen.bgcolor((R, G, B)).
.
Done
5.
True loop. This m
eans that
the code inside th
Repetition the code which is
will repeat foreve
e loop (i.e.
indented)
ep ea ting lin es of code is one of the can try to see wh
r. You
R uite
stes t way s to ge t something done. Q but remember it
at it does,
fa ore
science, it makes m will loop
often in computer an around forever!
es of code rather th
sense to repeat lin r
t of instructions. Fo
write out another se from turtle im
you created earlier port Turtle,
.
instructions four
.
turtle.forward(
yt ho n th er e ar e tw o types of loops 1).
In P d turtle.right(1)
yo u ar e lik el y to use: a while loop an .
that to
r lo op . If yo u w an t a section of code
a fo This type of loop
at fo re ve r, or un til a condition is set, is not going
repe to be very usefu
a w hi le lo op m ig ht be best. If you l for drawing
then shapes with Turt
t to lo op fo r a se t number of times, le where you
wan want to be more
eferable. precise.
then a for loop is pr
instructions
A .for. loop repeats
2. In this example, a set number of times
, in this
a .for. loop loop has
4 5 6 7 case 8 times. A .for.
has been used. 0 1 2 3 e (called
an associated variabl
Press Run to see
.i. here).
In this example, .i.
what happens. creases by
starts from .0. and in
e. Let’s apply this to
Done
.1. each tim
uare…
.from turtle the code to draw a sq
import Turtle,
.Screen.
.from turtle import
.Screen.
.Turtle,
.turtle = Turtle().
.screen = Screen().
turtle = Turtle().
.
turtle.penup().
.
for i in range(4):
.
)
turtle.forward(100
for i in range(8):
.
turtle.right(90)
turtle.write(i)
turtle.forward(20) Done
using a loop, you can repeat the shape l Can you alter the .for. loop so
again and again by putting it inside that it draws a more interesting
another loop. This is a great way to spiral using one of the shapes
you made earlier, like a triangle
draw spirals. Adapt your code by or circle?
making it look like this… l Adding a few extra lines where
you alter the variables .R., .G.,
from turtle import Turtle, .Screen. and .B. would allow you to make
a multicoloured spiral. Have a
.
.for i in range(30):.
for i in range(4):.
turtle.forward(100).
turtle.right(90).
turtle.right(25).
try…
Other things ttoothe
Pi tricks
Take your Raspberry
e ideas!
next level with thes
e functions to draw
l Learn how to us
Turtle
snowflakes using
stories using lists
l Create interactive
Storytime resource
in Py thon with the
berry Pi website
found on the Rasp
ysical
eps controlling ph
l Take your first st sp be rry Pi
and a Ra
objects with Py thon
turtle expert by
l Become a Py thon l
e advanced tutoria
continuing with th
of Turtley Amazing!
Done
Visit…
rg
www.raspberrypi.o
Ma ke a
pP i Cu sh io n
oo
Wh h g u to a i P ! p d te w th i
fart ag
ng t e
Bri Wh a t yo u w i l l need
n farting
very ow
To build your ed…
are going to ne
days before machine you
In the bad old
uters, the
TV and comp l 2 paper plat
es
family
most popular l A washing -u p sponge
t was the
entertainmen itchen foil
hion, a tooty
lK
whoopee cus l Stick y tape
from an e-sided tape
balloon made lG lue or doubl t fit on the
’s bladder. ader wires (tha
unwashed pig l 2
fe m al e he
ated and GPIO pins)
This was infl lengths of th
in, insulated
wire
grandad’s l2
hidden under
e better!)
ea ke r (the louder th
– when A
sp
chair cushion
l
‘PARPED!’
he sat down it im jump
gh
loudly, makin
d his false
into the air an as
It w
teeth fly out.
ever
the best thing
hen
(especially w hee-hee!
t the
the dog caugh
everyone will
about
think grandad
teeth and ran has farted!
and
wearing them roject
is p
grinning). Th
oopee
brings the wh te: no What you will learn
da
cushion up to
eed to
bladder; no n
Apart from learning which
members
d of your family have a sense
blow it up; an humour, you will also learn…
of
PLATE
SPONGE
WIRE
TAPE
FOIL
ares
4. Put the two plates together so that foil squ
Tape
are on the inside and facing each other.
them together.
tes with
5. You now have a ‘cushion’ made of two plawe will
two connecting wires coming out. Later
r
connect these wires to the GPIO pins on you
er
Raspberry Pi: one to a ground pin and the oth
circuit
to a pin that we will use to detect when the
s.
is made. For this we will use the header wire
1. Plug one
header lead (it
doesn’t matter
which) onto a
ground (GND)
pin on the Pi…
WARNING!
4. Download the burp sample with the
following command…
.
4 Write the program in Python
5. Setting it up
l Carefully place your WhooPi
Cushion where your victim will sit
on it (obviously!), but not under a Hint
really heavy cushion where it will Whistle tunelessly and
look around at the ceiling.
squash it straight away. This will make you look
innocent and attract
he tricky bit is setting up the
lT potential victims.
Pi so that it can’t be seen —
remember, you’ll need a plug
socket unless you are using a
battery for your Pi.
ide it, run the program, and wait.
lH Other thin
Take
gs to try…
Done your Raspbe
next level w rry Pi tricks
ith these ide to the
as!
l Use othe
r noise
need to be in s/tunes/samples. The
‘wav’ forma y
l Record y t
our own voic
recording y e. H o
our own voic w about
“Help! You’r e shouting,
e sitting on
me!”
lR ecord you
r parents w
you off and hen they’re
put the Who telling
under their oPi Cushion
pillow in be
sample. No d with the n
te: This may ew
pocket mon lead to loss
ey or worse of
record it an . Especially
d put it on Y if you
ouTube.
this
raspberry pi
project is so
much fun!
Visit…
ake/
www.raspberrypi.org/resources/m
ing
for more ideas and for help on us
Pi.
buttons and input devices on the
you
This is also the first place to go if
ng.
are stuck or something’s not worki
get
You can also ask questions and
help on our forums at…
www.raspberrypi.org/forums
m A crost ic
nag r a
A ble the words!
nscram
U
i m ed C h allenge!
T ese anagra
ms?
rk out th
Can you wo to test
t jus t 10 minutes
You’ve g o ted with
the m are associa u
yourself! A ll o f neral. As yo
P i o r co m puting in ge rs in the
Raspberry rid, the lette
e answ e rs into the g you out…
fill th
e n c o lu m n may help
shaded gre
ENACT FIRM
RENT MAIL
TSK DOPE
RAG ROMP
DO BAKERY
ERRS BOW
SLEW RISE
WOK TERN
NOT HYP
BRIAN SPA
NIL UX
The Result
Zzzap! I’ve !
How quickly
did
scrambled all manage to so you
the words with lve the
puzzle? Write
my raygun! in your
Find the
time below…
answers on page
78
77
learning/python-intro
Page 46
Space Maze
Here’s where you should have gone…
Page 31
Big Pi Wordsearch
How fast did you managed to find the 17
hidden words?
F M G O B K L S W M
S I W Y I L T D G H C K J L
B C O M P U T I N G I R S R B
S W A G C O O Z P O A D K Page 50
U I S V Y W Y W I T C Wheel of Pi
The phrase was: CODE MINECRAFT WITH PI
M Q F T Y R T Q P C A
P G X O I R E O R H H R F
A S C R O E N O U M Q G D F Q
G R F Z B F R C P L E H W D B Page 76
T
P
P
F
J
S
P
F
G
S
U
M
D
H
N
T
L
I
I
R
T
O
M
G
Z
L
X
A
R
S
Anagram Acrostic
M A A E G Y U H P E B N E R L The answers were: Minecraft, Terminal,
R A V R F N F N F M O P O J W Desktop, Program, Keyboard, Browser,
J R P C O I V V H R S D F Wireless, Network, Python, Raspbian, Linux.
J G V E R M T P N X I The secret phrase was Raspberry Pi.
K H O Y N Y A E T T A
C R P I S G X
P V M Page 77
Python Puzzle
Answers: apple, tangerine, orange, blackberry,
mango, apricot, mandarin, raspberry.
THE
sp be r ry
R ANNUAL 2018 Pi
a
GET STARTED
Set-up your Raspberry Pi & use it for the first time
SOLVE PUZZLES
Spot the difference, word searches, mazes & more!
MAKE PROJECTS
WhoopPi cushion, Santa detector & tree lights
LEARN TO CODE
Learn as you go with Scratch & Python projects
HACK MINECRAFT
Code with Minecraft on your Raspberry Pi
WRITE A GAME
Create your own version of Space Invaders!
Price: £7.99
ISBN 978-1-912047-70-3
9 781912 047703