Raspberry_Pi_Education_Manual, Lecture notes for Advanced Computer Programming. Università non definita
Massimiliano.Elico
Massimiliano.Elico12 January 2017

Raspberry_Pi_Education_Manual, Lecture notes for Advanced Computer Programming. Università non definita

PDF (3 MB)
137 pages
141Number of visits
Description
The Raspberry Pi Education Manual
20 points
Download points needed to download
this document
Download the document
Preview3 pages / 137

This is only a preview

3 shown on 137 pages

Download the document

This is only a preview

3 shown on 137 pages

Download the document

This is only a preview

3 shown on 137 pages

Download the document

This is only a preview

3 shown on 137 pages

Download the document

Introduction

1

Notes:

®®

®®

The Raspberry Pi Education Manual

1

The Raspberry Pi Education Manual

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Version 1.0 December 2012

2

The Raspberry Pi Education Manual Contents

000 0. Introduction ............................................................................. 5

001 1. A beginner’s guide to Scratch............................................. 7

1.1 Scratch basics ............................................................... 9

1.2 Moving sprites ..............................................................15

1.3 Animation (loops) ..........................................................18

1.4 Maths cat .....................................................................23

1.5 Artificial intelligence ......................................................29

1.6 Control .........................................................................35

1.7 Scratch games .............................................................44

What next? ..........................................................................50

010 2. Greenfoot on the Raspberry Pi ..............................................

Coming soon!

011 3. Experiments in Python .......................................................72

3.1 Getting to grips with Python .........................................73

3.2 MasterPy ......................................................................86

3.3 Roman Numerals & data manipulation ..........................89

3.4 Getting artistic ..............................................................94

3.5 Simulations and games ............................................. 100

3.6 Limited resources - memory & storage ...................... 106

3.7 Accessing the web - providing a weather forecast ..... 108

This is only the beginning - where do we go from here? .... 111

000 001

010

011

3

100 4. Human-computer interfacing ......................................... 113

4.1 Twitter ....................................................................... 115

4.2 Email application ....................................................... 116

4.3 Remote Procedure Call ............................................. 118

4.4 Web applications ....................................................... 120

4.5 General Purpose Input/Output (GPIO) ....................... 125

101 5. GeoGebra: fun with maths! .....................................................

Coming soon!

110

6. The Linux Command Line ............................................... 152

6.1 Commands are just programs ................................... 153

6.2 Command syntax and file structure ........................... 155

6.3 The superuser ........................................................... 161

6.4 Creating and destroying files and directories .............. 163

6.5 Remote access to the Raspberry Pi .......................... 166

111 7. What next? .......................................................................... 169

100

101

110

111 Where are the Greenfoot and GeoGebra chapters?

The Greenfoot and GeoGebra chapters have been left out of this edition of the

manual. These programs rely on software called a Java virtual machine, which

is currently being optimised for the Raspberry Pi to improve performance. You

can look forward to enjoying these chapters once we are happy that your user

experience will be of the same high quality as the chapters themselves!

4

This manual was brought to you by...

This manual is a bit different. It was written entirely by unpaid volunteers, all of

whom are keen to share their expertise and enthusiasm for computing with as

many people as possible.

What all of these contributors have in common, apart from a youth spent

mainly indoors in front of ZX Spectrums and Commodore 64s, is that they’re all

members of the organisation Computing at School (CAS). To find out more

about CAS and its work promoting the teaching of computer science, head over

to http://www.computingatschool.org.uk

Manual Contributors

Introduction by Andrew Hague

A beginner’s guide to Scratch by Graham Hastings

Greenfoot on the Raspberry Pi by Michael Kölling

Experiments in Python by Andrew Hague

Human-computer interfacing by Ben Croston

GeoGebra: fun with maths! by Adrian Oldknow

The Linux Command Line by Brian Lockwood

Where next? by Clive Beale

Manual Production

Karl Wright, Robert Cruse and Paul Kingett of Publicis Blueprint

Digital Contributors

The following people offered contributions not covered in the manual, but available

online and on your SD card.

Scratch Pong by Bruce Nightingale

Caesar Cipher by Brian Starkey

Fly by Alan Holt

Special Thanks

Martin Richards (University of Cambridge)

Simon Humphreys (Computing at Schools)

Alex Bradbury (University of Cambridge/Raspberry Pi Foundation)

Liz Upton (Raspberry Pi Foundation)

Eben Upton (Raspberry Pi Foundation)

Introduction

5

Congratulations! You have in your possession a Raspberry Pi. A small

but powerful computer designed to help you understand and explore

the almost-magical world of computing. Use it wisely; it’s an object of

great power.

Notes:

Hello, Raspberry Pi users Chapter 0

What is the Raspberry Pi?

The Raspberry Pi is a computer, very like the computers with which you’re

already familiar. It uses a different kind of processor, so you can’t install Microsoft

Windows on it. But you can install several versions of the Linux operating system

that look and feel very much like Windows. If you want to, you can use the

Raspberry Pi to surf the internet, send an email or write a letter using a word

processor. But you can also do so much more.

Easy to use but powerful, affordable and (as long as you’re careful) difficult to

break, the Raspberry Pi is the perfect tool for aspiring computer scientists. What

do we mean by computer science? We mean learning how computers work so you

can make them do what you want them to do, not what someone else thinks you

should do with them.

And who do we mean by computer scientists? We mean you. You may

finish this manual and decide you want to be next Tim Berners Lee, but even if you

don’t, we hope you have fun, learn something new and get a feel for how computers

work. Because no matter what you do in life, computers are bound to be part of it.

Introduction

6

Notes: What am I going to learn?

This user manual is different. Don’t expect a dry-as-dust description of how

to plug things in or where to find your serial number. And you certainly won’t learn

how to create a spreadsheet or a presentation. That’s really not computer

science, it’s something else entirely.

Instead, think of this manual, along with your Raspberry Pi, as a

“computer science set”. Have you ever been given a chemistry set? With a

chemistry set, you can make lots of bangs, smells and odd-coloured goop to

learn all about elements, molecules and compounds.

We’re not going to make odd-coloured goop, but we will use experiments

to discover how to program a computer to create your own games and animations,

how to make graphics appear on screen just by typing in the right code (just like

the developers of your favourite games do), how to get a cat to do your maths

homework for you, and much more.

By doing all this, you will learn the basic principles of computer

science. And that’s your first step on the journey to becoming a real computer

programmer, a games developer, an über-hacker just like in the movies (only

cooler and staying strictly within the law) and many other things besides. Exactly

what, depends on you.

Who is this manual for?

When we wrote this manual, our aim was for it to be suitable for most people

of eight years and older. But that doesn’t mean it’s for eight year olds. This book

is for anyone and everyone who is curious to know more about computing and

creating computer programs. If you don’t have computer-programming experience

but you want to get some and you’re looking for a place to start, this is it.

We begin the manual with some relatively easy experiments in computer science.

Things then get progressively more challenging with each successive exercise.

Try to spend time with each experiment and, once you’ve got an exercise doing

what the manual says it should, feel free to change the code to see what happens:

it’s one of the best ways to learn.

Will I break it?

You can’t break your Raspberry Pi by doing any of the experiments in this

book, but you might just surprise yourself with what you can achieve. You will be

working through and learning genuinely difficult but exciting concepts, and laying

the foundations for even more exciting discoveries in the future.

So, without further delay, have everyone in the room stand back: we’re going to do

computer science!

A beginner’s guide to Scratch

7

Scratch is visual programming environment. With it, you can create your

own animations, games and interactive art works. And, while you’re

doing that, you’ll learn some important principles and techniques of

programming without actually having to write your own code. It’s a

great way to get started. To find out more about Scratch, visit the web

address scratch.mit.edu

Notes:

How to use this guide

We have tried to make this guide as straightforward to use as possible. To help

you with the exercises in this chapter, we have already collected some little bits

and pieces you will need, such as backgrounds, costumes for sprites, sound

effects and complete examples of Scratch projects.

These can be found on the Raspberry Pi educational release SD card, in the folder

/usr/share/scratch/RPiScratch. Wherever you see the SD card icon in the margin,

that means we are referring to a file that can be found on your Raspberry Pi

SD card. Go take a look! They can also be downloaded from Google Drive at

http://goo.gl/MpHUv

A beginner’s guide to Scratch Chapter 1

A beginner’s guide to Scratch

8

The Scratch interface

Share

Save

language

Sprite rotation Style

current Sprite info toolbar

tabS This is where

you edit scripts, costumes or

sounds

blocKS palette The blocks of code you’ll use to program your sprites

green flag A way to start scripts

vieW MoDe Change the size

of the Stage

preSentation MoDe Go fullscreen to show

off your projects

Stop Sign Stops your scripts

Stage Where your Scratch projects do their thing

MouSe X-y DiSplay Shows the location of the mouse cursor

neW Sprite buttonS Create or import new sprites

Sprite liSt Find all your sprites here. Click one to select and work with itScriptS area

Drag blocks in, snap them together into scripts

A beginner’s guide to Scratch

9

Lesson 1.1: Scratch basics

Learning objecTive: In this exercise, you will learn how to use the Scratch

graphical user interface (GUI), how to create characters (sprites and costumes)

and stages (backgrounds) for your projects, and how to add scripts.

reSourceS: The sprites “cat” and “roman_cat”, and the background

“roman_stage”.

Have you ever been in a school play? If you have, you’ll know that to put on a play

you need a stage, actors, costumes and a script. Think of Scratch as being a bit

like a play. The actors are called “sprites”.

To make your sprites move and talk, you need to give them instructions. You do

this by writing “scripts” using blocks of code from the Blocks Palette and Scripts

tab on the left of the screen.

That’s enough introductions for now; let’s get to grips with the program itself.

Open Scratch from your Raspberry Pi’s Applications menu. You should now be

looking at the Scratch graphical user interface, or GUI (pronounced “gooey”).

Have a look around and tick the boxes below as you find these items:

Click on the Scripts tab, can you see any instructions for the cat to follow?

You can dress your sprites in “costumes”, and each sprite can have more than

one costume. The “stage” is the area on the screen in which your sprites will

perform the tasks you write for them.

1. The stage (a big white screen)

2. A sprite (clue: it’s a cat)

3. The two costumes that your sprite can wear (click on the Costumes tab)

4. The Scripts tab

Notes:

A beginner’s guide to Scratch

10

Let’s have some fun with the cat

First, let’s give the cat something to say. We’ll start with “Hello, World”. This is

generally the first thing a computer programmer learns to do (don’t ask me why).

As you are now learning a programming language, you’d better start with

“Hello, World”, too.

Making the cat talk

To make the cat say “Hello, World”, we’re going to be working with “blocks”.

These are handy pieces of code, each containing an instruction for your sprite

to follow.

There are eight different types of block. These can be found in the top-left corner

of the Scratch GUI. They are colour-coded, so remember the colours. Find out

what they are and complete their names in the table below:

M... c...

L... S...

S... o...

P... v...

now, follow these simple steps to make your cat talk:

1 Click on the cat sprite in the Sprites List (bottom right) to make sure that it’s selected.

2 Click on the “Looks” button in the Blocks Palette to make the Looks blocks appear.

3 Click on the block labelled “say [Hello] for [2] seconds” and drag it to the Scripts tab.

4 Replace “Hello” with “Hello, World”. Double-click the block and your cat should

say: “Hello, World”.

Notes:

A beginner’s guide to Scratch

11

We have to run a program to make it work. You can do this by just double-clicking

your script, if you only have one script. But if we have more than one script,

we might want to start them at the same time. We can use a “green-flag event”

for this.

To find THe bLock for green-fLag evenTS:

1. Click on the Control button in the Blocks Palette.

2. Find the block labelled “when [picture of a green flag] clicked”.

3. Select it, then drag and drop it to the top of the script you’ve created in the

Scripts tab. Make sure it snaps into place.

You are now ready to run your first Scratch program properly. Just click on the

green flag symbol at the top-right-hand side of the Scratch window, just above the

stage, and watch the cat do its thing.

over to you

QueSTion: For how long did the cat say “Hello, World”? _____ seconds

TaSk: See if you can change the block to make the cat say “Hello, World” for

5 seconds.

Notes:

A beginner’s guide to Scratch

12

changing the way the sprite looks

1 Click on your sprite to select it. In the Scripts area, click on the Costumes tab.

2 We are going to make a third costume for the cat, so click on Copy. A new cat costume

should appear.

3 Select “costume3” and click on Edit. This will open the Paint Editor. Experiment with all the

buttons and tools to find out

what they do.

4 Once you feel at home, draw some clothes on the costume and click on OK. I gave my

sprite a toga to make it look

like a Roman Emperor.

5 Next, select the Scripts tab, click on the Looks button and select the “switch to

costume [ ]” block.

6 Drag it under the Scripts tab and use the drop-down menu to select “costume3”. Double-click

on this block and the cat will

change his costume.

Now you have two blocks under the Scripts tab, one to say “Hello, World” and one

for switching the costume. You can put them together by moving one so that it is

just above or below the other. If a white line appears, the two blocks will snap

together. Two or more blocks stuck together make a “script”.

Notes:

that cat’s right: he looks like he’s lost

in a snow storm. We need to give him a stage on

which to perform.

over to you

QueSTion: Now that we have a script with two blocks, what happens when you

double-click it?

TaSk: See if you can arrange three blocks to make the cat change to his toga

costume, say “Hello, World”, then change back to its normal costume.

A beginner’s guide to Scratch

13

The stage

It’s time to give that cat a stage. We could be lazy and just import a picture to use as

a background, but let’s say that we’re feeling energetic and want to draw our own.

3 Alternatively, you can import a ready-made background. Select Stage, then Backgrounds and then click on the Import button.

4 Have a look at all the available backgrounds before you pick the one that you want. We chose “roman_stage”. Select the background by clicking

on it with your mouse, then click on OK.

1 Click on the stage in the Sprite List (bottom-right of the screen). Now click on the Backgrounds tab for the stage and click on the Edit button.

2 As before, the Paint Editor will open. Draw a stage for your sprite. When you have finished, click on OK. You can make further changes at any time by clicking

on Edit.

Notes:

A beginner’s guide to Scratch

14

Tip...

Use a name that

will help you to

find the project

again. Always use

an _ (underscore)

between words in

filenames – don’t

leave an empty

space.

Notes: Saving your work

This is a good time to save your project. You would be wise to do this every 10

minutes or so, then you can be sure that you won’t lose any of your hard work.

When working on a big project, save it in two places, then you have a backup.

To save your project, click File, then Save – the Save Project window will open.

By default, it will save your work to the Scratch Projects folder. This is a sensible

place to store your work, so type in a new filename, at the bottom. I’ve called mine

“roman_play”, so pick a different file name for your project or you will save yours

over mine! Click on OK to save.

Wow! That is a lot for the first lesson. Have a play with Scratch – experiment with

different blocks of code to find out what they do. Then come back when you have

had a good rest and try Lesson 2.

A beginner’s guide to Scratch

15

Lesson 1.2: Moving sprites

Learning objecTive: In this exercise, you will learn how to move sprites

around the Scratch screen in a controlled way and how to tell a joke.

reSourceS: The sprite “roman_cat” and the background “roman_stage”.

The cat is feeling a bit lonely, so we’d better create some characters for it to play

with. You can either paint your own sprites or import sprites from the Scratch

Costumes folder. Use the New Sprites buttons to do this.

On the right-hand side of the program, just below the stage and above the Sprite

List, you’ll see three buttons: the New Sprite buttons. It’s these we’re going to use.

I want to add a time-travelling boy to my stage. To keep things simple, and to let

us get on with some more programming, we’re just going to import him.

Click on the middle New Sprite button and import the sprite “boy4-walking-c”. But

wait a sec: he’s facing the wrong way! No problem. Go to the Costumes tab and

click on Edit. Use the Flip Horizontally button to make him face to the left.

Notes:

paint neW Sprite

chooSe a neW Sprite froM file

get a SurpriSe Sprite

A beginner’s guide to Scratch

16

There are also buttons to make your sprite bigger, smaller, rotate counter-

clockwise, rotate clockwise, as well as flip horizontally and flip vertically. Try them

out. I have also used the shrink button to make my boy smaller.

Make your sprites tell a joke

Let’s make the sprites tell each other a joke. You can do this using the speech

block from the Looks category.

You could try a simple ‘knock knock’ joke to start with.

But wait! Are you finding that both of your sprites are talking at the same time.

To fix this, from the Control block add the “wait [1] secs” block to the second

sprite, before the “say” block.

use the import Sprite button

to find and import the sprite

“boy4-walking-c”.

from the costumes tab, click edit and

use the flip horizontally tool.

Notes:

A beginner’s guide to Scratch

17

Positioning your sprite

Ok, we’ve told a joke. But this play is looking a bit static, so let’s make our

characters move. The first job is to move our two characters to their start points.

In my play, the cat will come in from the left and the boy from the right.

The coordinates of any point on the stage are shown at its bottom-right-hand

corner. Move your mouse around the screen and watch the numbers change.

TaSk: Use your mouse to find the centre of the screen. Move the mouse pointer

until it’s exactly over the point x: 0 y: 0. Now let’s position our sprites.

1. Select the cat sprite then, in the Blocks Palette, click on the Motion block

labelled “go to x: [0] y: [0]”.

2. Change the values in the block to x: -240 y: -80. This will take the cat to the far

left of the stage.

3. Next place a “wait [1] secs” block into your script. This will give you time to see

your cat before it moves.

4. Now add a second “go to x: [0] y: [0]” block. Use your mouse to work out the

x coordinate just left of centre on the stage, to which we want to move the cat.

Repeat this process for your other sprite, positioning it slightly to the right of

centre stage. Ideally, the two sprites should move from the edges of the screen to

stand face to face, separated by a small gap.

Now you need to make the sprites tell a joke. Remember to leave a short delay

after each sprite speaks, otherwise they’ll talk over each other. Have a look at the

screenshots to see our code (and our fantastic joke).

you may have come across x

and y axes when creating graphs.

the x coordinate

the Y coordinate

Tip...

Use your mouse

pointer to find the

coordinates of a

position on the

stage and make

a note of those

coordinates on

a piece of paper.

Notes:

and here’s our code for the cat sprite. Does your looks the same?

this is what our code for the boy sprite looks like.

A beginner’s guide to Scratch

18

over to you

TaSk: Now add some code to your other character to move it to the right of the

stage and then after a short delay move it into the centre stage.

Well done! You have certainly got the hang of moving sprites about

the screen. Why not add some more characters to your stage and get

them to tell jokes?

If you are having problems, you can load the sample code, “roman_play.sb”, to

see how the program is put together. Feel free to change things and to experiment,

as this is a great way to learn.

Lesson 1.3: Animation (loops)

Learning objecTive: In this exercise, you will learn how to use repeat loops

to create simple animations.

reSourceS: The sprites “bee”, “female_flower” and “male_flower”, and the

background “flower_bed”.

With its animated characters, Scratch is great for telling stories. I have to do a

school science project on pollination, so I have decided to use Scratch to tell the

story of pollination in moving pictures. You can help me by following these

instructions to animate a bee in flight.

First, open the file “bee1” from the “Animals” folder in the Scratch gallery. Next,

import the background “flower_bed”, this time from the “Nature” folder in the

Scratch gallery. Delete the cat sprite; we don’t need it for this project.

Copy “bee1”, then edit “bee2” using the Select and Flip Horizontally tools, to

make its wings point downwards. Together, the two costumes – “bee1” and

“bee2” – will become an animation of a flying bee.

copy your bee, then edit “bee2” so that its wings

point downwards.

Notes:

A beginner’s guide to Scratch

19

We need some script to make the bee look as if it is flying. We do this by switching

from one costume to another and back again, making the bee appear to flap its

wings. As we do this we will also make the bee move forwards.

Now, build your own script to make the bee fly. You will need blocks from Control,

Looks and Motion. If you get stuck, have a look at the screenshot of our code.

You’ll find it further on in the lesson.

this is the code to make your bee fly.

instead of using the green flag to run my

code, i will use a “when Sprite1

clicked” block from control. the code

will run when i click on the bee.

Here are the steps you need to follow:

1. Start with costume “bee1”.

2. Add a “wait [0.2] secs” block, so that the viewer has time to see the costume.

3. Move the bee on 10 steps, before switching to costume “bee2”.

4. Add another “wait [0.2] secs” block, so that the viewer has time to see the

second costume.

5. Move the bee on another 10 steps.

But we need to do this more than once. To make the bee fly across the screen,

we might have to repeat this 20 times.

don’t panic! You are using a computer. Computers are fantastic at doing things

over and over again. They can do this very accurately and never get bored, tired

or fed up.

What we need is a repeat loop.

Notes:

This is what we use to program the computer to repeat something over and over

again. You will find the repeat loop (“repeat [10]”) in the Control blocks.

A beginner’s guide to Scratch

20

It looks a bit different because it has a gap so that we can put code you want

to repeat inside it. Just drag the “repeat [10]” block to sit directly under the

“when Sprite1 clicked” block. It will automatically fit around your block of code,

causing it to repeat itself.

Here is the code we need to animate the bee so that it flies all the way from one

side of the screen to the other.

over to you

QueSTion: Why do you think I have increased the number of times it repeats

from 10 to 20?

TaSk: Some of the Scratch sprites already have two costumes. Check out the

Scratch cat sprite. Use its two costumes and code similar to the example on the

left to make it walk.

using the repeat loop, you can make

the sprite do the same actions over

and over again. So the bee flaps its wings up and down

many times.

Tip...

If your bee flies in

the wrong

direction, check

the sprite to make

sure that it is

facing in the

correct direction.

With the sprite

selected, look at

the bar above

the Scripts tab.

The “forward”

direction of a

sprite is indicated

by a little blue

line. The bee on

the left will move

90° to the vertical

and the bee on

the right will move

60° to the vertical.

You can rotate the

line to change the

move direction of

a sprite.

Notes:

A beginner’s guide to Scratch

21

Hands on: the Pollination Project

This is the storyboard of my pollination project. i added two more

costumes to my bee sprite to show it carrying pollen and i have

drawn a flower sprite with some stamens in blue.

But I didn’t want to stop there. I wanted the bee to visit the second flower –

a female – from the other side of the screen, so I copied all four costumes for the

bee sprite and flipped them horizontally.

I also copied, flipped and edited the male flower to create a female flower sprite.

I have given it two costumes. One shows the stigma without pollen and the other

with pollen. Let’s have a look at the resulting animation.

I also decided that I only wanted one flower on screen at a time. So, I had to add

scripts to make my flowers disappear and appear at the right points in the

animation. I used the Looks blocks “show” and “hide” for this.

1

3

5

4

6

2

Notes:

1. A bee flies towards a male flower. 2. The bee pauses to suck up nectar

and collect pollen.

3. The bee then flies off with pollen

from the flower’s stamens.

4. The bee flies toward the female

flower.

5. The bee sucks nectar and this time

deposits pollen.

6. The bee flies off leaving pollen on

the stigma.

A beginner’s guide to Scratch

22

Here’s the code for all three sprites in the pollination project.

Because I had three different sprites with their own scripts, I used the

“when [green flag] clicked” event to run them all together.

Wow! That is quite a complicated project, but if you break up your

animation into lots of little scenes it makes it easier to plan and

to program.

To see what the whole project looks like once it’s finished, open

RPiScratch/Projects/ pollination.

code for the bee code for the male flower

code for the female flower

Tip...

Import existing

sprites and a

background from

the Scratch

picture folders.

This can save

you a lot of time.

Notes:

A beginner’s guide to Scratch

23

Notes: Lesson 1.4: Maths Cat

Learning objecTive: You will learn how to use variables to store data for

using in a program. You will also learn how to use operators to do simple sums.

reSourceS: The default sprite “cat” on the default white background.

do you find maths difficult?

Can you imagine what it would be like to be able to do millions of sums in seconds

and always get them right? Even the most complicated sums you can think of?

Computers are fantastic at maths. In fact, maths is what they do best. We can

program the Scratch cat to do maths. The cat will ask for some numbers and then

do the sums. So, how are we going to put numbers into the program for the cat

to use?

When we input numbers (put numbers into a computer), the computer has to have

somewhere to store them. Different people might input different numbers, so

these numbers are going to be different each time.

When programming, we store numbers in something called a “variable”. One way

of thinking about a variable is as a box, or container, in which we can store

numbers, letters or words.

We may have more than one variable in a program, so we give them different

names. The name can be as simple as a single letter (or as complicated as you

like!). For example, if it is storing a number, we might call the variable “n”.

in the diagram, we have stored the number “7” in the variable “n”.

So we can now say “n = 7”.

In the example above, we created a variable called “n” and stored the number “7”

in it. In Scratch, you would do this in two steps: first creating the variable “n”, and

then using a block from Variables to set its value to “7”.

7 7 n n

A beginner’s guide to Scratch

24

If you wanted to use a two-word name for your variable, you would separate the

words with an underscore (the “_” character), not a blank space.

using variables in Scratch

Ok, now we’re going to create and use variables. Click on Variables in the Blocks

Palette and create a variable called “game_score”.

Tip...

Give each variable

a name that

reminds you what

is stored in it.

For example,

if you are creating

a game and you

use a variable to

store the score,

then a good name

for the variable

would be

“game_score”.

Notes:

1 Click on the Variables button, then on the button labelled “Make a variable”. This opens the “Variable name?”

dialogue window. Enter the name

“game_score” for your variable

and click OK.

2 Drag “Set [game_score] to [0]” to the Scripts tab. Then, from Control, drag the block “when Sprite1

clicked”. Join the two together

to make a script.

3 The default value for new variables is “0”. Select the block “set [game_score] to [0]” and

change the value to “100”.

4 Finally, from Looks, I have used a “say [Hello] for [2] secs” block but changed it to “say [Great, I’ve got

100 points] for [2] secs”.

comments (0)

no comments were posted

be the one to write the first!

This is only a preview

3 shown on 137 pages

Download the document