Scott YYYY Starting From Scratch

Download as pdf or txt
Download as pdf or txt
You are on page 1of 64

Starting from

An Introduction to Computing Science


by Jeremy Scott

LEARNER NOTES v2
Starting from Scratch An Introduction to Computing Science

Acknowledgements
This resource was partially funded by a grant from Education Scotland. We are also grateful for
the help and support provided by the following contributors:

Cathkin High School


Linlithgow Academy
Perth High School
George Heriot’s School
Stromness Academy
CompEdNet, Scottish Forum for Computing Science Teachers
Computing At School
Council of Professors and Heads of Computing (CPHC)
Professor Hal Abelson, MIT
Mitchel Resnick, MIT
Scottish Informatics and Computer Science Alliance (SICSA)
Edinburgh Napier University School of Computing
Glasgow Caledonian University School of Engineering and Built Environment
Heriot-Watt University School of Mathematical and Computer Sciences
Robert Gordon University School of Computing
University of Edinburgh School of Informatics
University of Aberdeen Department of Computing
University of Dundee School of Computing
University of Glasgow School of Computing Science
University of St Andrews School of Computer Science
University of Stirling Department of Computing Science and Mathematics
University of Strathclyde Department of Computer and Information Sciences
University of the West of Scotland School of Computing
International Olympic Committee
ScotlandIS
Turespaña
4J Studios
Brightsolid
Google
JP Morgan
Microsoft Research
Oracle
O2
RunRev
Sword Ciboodle

Special thanks go to Ian King who assisted with updating these materials for Scratch v2.0. The
contribution of the following individuals who served on the RSE/BCS Project Advisory Group is
also gratefully acknowledged: Professor Sally Brown (chair), Mr David Bethune, Professor Alan
Bundy, Professor Quintin Cutts, Ms Kate Farrell, Mr William Hardie, Dr Fiona McNeill, Professor
Greg Michaelson, Dr Bill Mitchell and Professor Judy Robertson.

Some of the material within this resource is based on existing work from the ScratchEd site,
reproduced and adapted under Creative Commons licence. The author thanks the individuals
concerned for permission to use and adapt their materials.
BCS is a registered charity: No 292786
The Royal Society of Edinburgh. Scotland's National Academy. Scottish Charity No. SC000470

i
Starting from Scratch An Introduction to Computing Science

ii
Starting from Scratch An Introduction to Computing Science

Contents
Introduction .................................................................................................... 1
What is a computer? ...................................................................................... 1
Types of computer ......................................................................................... 2
Parts of a computer ....................................................................................... 5
Hardware ....................................................................................................... 6
Software ......................................................................................................... 7
Programming languages ................................................................................ 8
Programming in Scratch .................................................................................. 9
1: Scratching the Surface ............................................................................... 11
All the world’s a stage .................................................................................. 11
Putting things in order ................................................................................. 13
Lazy or smart? .............................................................................................. 16
2: Story Time ................................................................................................. 17
Bugs .............................................................................................................. 19
Event-driven programming .......................................................................... 22
3: A Mazing Game ......................................................................................... 23
The Importance of Design ............................................................................ 23
4: Get the Picture? ........................................................................................ 31
Nesting ......................................................................................................... 32
5: Forest Archery Game ................................................................................. 41
Variables....................................................................................................... 44
Summary ...................................................................................................... 47
Scratch Project .............................................................................................. 49

iii
Starting from Scratch An Introduction to Computing Science

iv
Starting from Scratch Introduction

Introduction
You have probably already used several computers today without realising it.

If you have sent a text, been driven in a car, or checked your watch then you have used a
computer. The words you are reading now were typed on a computer.

Computers are all around us. Since they affect so many parts of our lives, it is important
to understand how they work.

What is a computer?
A computer is a machine that carries out instructions given to it by a human. Without
instructions, computers wouldn’t be able to do anything.

If this is the case, then what makes them special? Well, computers…

 work faster than humans 1 ;


 are more accurate than humans;
 can store huge amounts of information that they never “forget”.

It might seem that computers can do almost anything. However, here are some other
important things to remember:

 Computers don’t have brains; they are not cleverer than humans.
 Computers don’t have feelings or “common sense”. This means that there are
lots of everyday tasks that humans can perform that computers still cannot.

Activity Write down three everyday tasks that humans perform but computers cannot (or are
not very good at).

1. _____________________________________________________________________

2. _____________________________________________________________________

3. _____________________________________________________________________

1 At the time of writing, a modern personal computer could perform over 100 billion calculations
every second!

Page 1
Starting from Scratch Introduction

Types of computer
Computers come in many shapes and sizes. Computers that most people might
recognise include:

Desktop A desktop PC (Personal Computer) is designed to


sit on top of – or under – a desk and is used by
one person at a time. It is powered by mains
electricity and made up of separate devices.

Laptop Laptop computers combine all the separate devices


of a desktop PC into one unit. This can be carried
around and powered by mains electricity or battery.
Netbooks and ultrabooks are just smaller, lighter
types of laptop.

Tablet This has a large, touch sensitive screen which is


controlled with your finger (or sometimes a special
pen). It is battery-powered and very portable.
Tablets have an on-screen “virtual” keyboard2.

Activity The personal computers shown above appear in order of oldest to newest types.
What does this tell you about the kind of computers people want?

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

2
The word “virtual” is used a lot in Computing. It just means “not real” – it’s something that’s
been recreated on a computer. Can you think of any other virtual things you get on a computer?

Page 2
Starting from Scratch Introduction

Other computers that may not be as well-known or recognised by most people include:

Mainframe This is a large computer which can take


up an entire room. Many users can use
it at the same time, each with their
own keyboard, mouse and monitor.
Mainframes are very expensive and
need a team of people to run them.
They are owned by large organisations
that need to store and process huge
amounts of information.

Server A server is a computer that provides


services for other computers on a
network e.g.

 file server (stores users’ files);


 web server (serves out web pages);
 mail server (provides email services).

Games console Games consoles are also


computers. Most have a disc
drive for loading games and a
powerful processor to create
realistic graphics.
Many games consoles can also connect to the Internet, letting users
buy games online or compete with other gamers around the world.

Page 3
Starting from Scratch Introduction

Embedded Many devices in your home have an embedded


computer – a small silicon chip that carries out
stored instructions. The modern home has over 100
of these “computers”, built into devices like a
toaster, stereo, washing machine, fridge, TV, etc.
A modern car may have another 100 or more embedded computers3.

Activity Write down three devices in your own home that you think might contain an
embedded computer (besides those shown above).

1. _____________________________________________________________________

2. _____________________________________________________________________

3. _____________________________________________________________________

Smartphone “Smart” mobile phones like Android and Apple iPhone


are really pocket computers that can also make phone
calls. Most smartphones use large touch screens.
This is a good example of convergence where
technologies that were previously separate are now
combined in one device.

Activity Write down three technologies that are combined in a modern smartphone.

1. _____________________________________________________________________

2. _____________________________________________________________________

3. _____________________________________________________________________

3
Source: http://embedded-computing.com/guest-blogs/ota-challenges-for-automakers/

Page 4
Starting from Scratch Introduction

Parts of a computer
A computer is a machine that:

 takes in information
 stores this information
 processes this information
 and gives this processed information back out.

INPUT PROCESS OUTPUT

STORAGE

Activity Write down inputs and outputs for the following activities on different types of
computers. When you have finished, create an extra one of your own:

Activity Input(s) Output(s)

Move game controller Character moves


Playing a video game
Click buttons Menu selections made

Surfing the WWW

Making a phone call

Watching TV

Page 5
Starting from Scratch Introduction

There are two main parts to a computer: hardware and software.

Hardware
Hardware means computer equipment. A single piece of hardware
is called a device.

There are four main types of device in a computer:

Input device used to put data into the computer.

Central Processing Unit (CPU) where the computer carries out the instructions
given by the programs. The faster the CPU
(commonly just called the processor), the faster your
computer will work.

Output device used for data coming out of the computer.

Storage device used to store programs and data. It is where you save
your work on to.

Put simply: if you can touch it, it’s hardware!

Activity Decide if the following devices are input, output or storage devices then put each one
into the correct column. The first three have been done for you.

keyboard; hard disc drive; monitor; speaker; scanner; printer; mouse; DVD drive;
microphone; flash drive (memory stick); game controller; touch screen; memory card

Input Device Storage Device Output Device

keyboard hard disc drive monitor

Page 6
Starting from Scratch Introduction

Software
A computer can perform different tasks, depending on the instructions it is given.

A list of instructions is called a program. Without a program to tell it what to do, a


computer would just be a (useless) collection of hardware devices.

Software is the name given to programs and the information they use.

Activity Complete the table below of ten different jobs you can do on a computer and the
name of a software package that lets you do it.

Task Software package

Browse the World Wide Web Google Chrome

Play a game Angry Birds

Edit a movie iMovie

Page 7
Starting from Scratch Introduction

Programming languages
Computers follow instructions given to them by humans. They can solve only the
problems that people tell them to solve. To tell a computer what to do, you must know
what problem you want to solve and have a plan for solving it.

Unfortunately, these instructions can’t just be given to the computer in normal English.
A computer can perform tasks very quickly, but it is not intelligent like we are.

A computer will do:


 only what it is told and
 exactly what it is told.

This means that computer programs have to be written in a very precise way, according
to strict rules. There must be no confusion over what instructions mean.

A set of instructions and rules that a program can be written in is called a programming
language.

Page 8
Starting from Scratch Programming in Scratch

Programming in Scratch
The rest of this course will focus on how to write computer programs.

You will be using Scratch, created by MIT (Massachusetts


Institute of Technology), one of the USA’s leading
universities.

Scratch is a powerful software development package. It


lets you create programs (called projects) that combine
sound, graphics and animation.

You can upload your projects to the Scratch website


and share with other Scratchers around the world. It
really is the cat’s whiskers!

You will learn how to use Scratch through a series of


lessons. At the end of each one, there will be some
questions which will help to check if you have
understood what you have learned.
A Lego ™ model of the
Scratch cat at MIT Media Lab

Page 9
Starting from Scratch Programming in Scratch

Page 10
Starting from Scratch 1: Scratching the Surface

1: Scratching the Surface


This lesson will cover
 The Scratch environment, including
o Sprites & stage
o Properties
 Scripts
 Costumes/backdrops
 Sounds
 Creating a program with animation & sound

Introduction
Watch the video introduction to Scratch. This will introduce you to Scratch and its
screen layout.

https://vimeo.com/80961102

All the world’s a stage


A Scratch program contains sprites (characters) that “perform” on a stage. Sprites and
the stage have three kinds of properties (or settings):
1. Scripts
These are the instructions that control a sprite. Scripts are made from blocks.
There are eight different kinds of blocks – to do with motion, control, looks, etc.
– and over 100 blocks in total. Note that sprites need scripts to perform a task.

2. Costumes/Backdrops
Costumes are “outfits” for a sprite. The same sprite can have several costumes
and so be made to look completely different.
The stage can have different backdrops which can be changed. Backdrops are
just like costumes for the stage.

3. Sounds
These are sounds that sprites or the stage can use. Again, each sprite (or the
stage) can have many different sounds. Scratch lets you import (bring in)
recorded sounds or even record your own using a microphone.

Page 11
Starting from Scratch 1: Scratching the Surface

Task 1: Up on the Catwalk


Watch screencast Catwalk.

This will go over the main elements within Scratch and take you through the task of
creating your first computer program. If you get stuck, go back in the screencast or ask
your partner.

Task 2: Frère Jacques


Watch screencast FrereJacques.

This will show you how to create a simple tune in Scratch. If you get stuck, go back in the
screencast or ask your partner.

Did you know…? Frère Jacques is one of the best-known songs in the world. It is a French song about a
religious monk (“Brother John” in English) who has the job of ringing the morning bell
before the days of alarm clocks. Unfortunately, poor Jacques has overslept! 4

Task 3: My Tunes
Once you have completed Task 2, try creating a program that plays another simple song.
Choose one where lines of the music repeat, so you can use the repeat command.

Congratulations – you have just started your journey to become a computer


programmer!

4
The score shown above is in a different key from the version created in the Task 2 screencast.

Page 12
Starting from Scratch 1: Scratching the Surface

Putting things in order


Blocks in the same script get executed (carried out) in sequence, one after the other.
Blocks in separate scripts can sometimes be executed at the same time. This is called
parallel processing – having the computer do more than one thing at a time.
For example, if you have several scripts, they will all get executed together
when the flag is clicked.

Extension 1: Dance away


Try to make a sprite dance in time to your music, starting the program when the flag is
clicked. There are two ways you could do this:

 create a single script that includes the sprite movement blocks amongst
the play note blocks
 have separate scripts for the same sprite – one script plays the tune whilst the
other makes the sprite dance.

You can find another screencast (Dance Party) to give you some inspiration at
https://scratch.mit.edu/projects/10128067 .

Make sure you create a tune, rather than just use a music loop, though!

Extension 2
Experiment by adding some other blocks to your program, such as the looks blocks e.g.

These let you create some really fun effects!

Page 13
Starting from Scratch 1: Scratching the Surface

Did you understand?

1.1 Look at the section of code opposite that controls a sprite.


Write down what you think the user will see when the flag is
clicked.

__________________________________________________________________

__________________________________________________________________

Why? _____________________________________________________________

__________________________________________________________________

Now try out the code yourself and see if you were right.

1.2 Now add a wait 1 secs block between the two move blocks. Describe what
happens now.

__________________________________________________________________

Explain why this happened ____________________________________________

__________________________________________________________________

1.3 Look at the section of code below that controls a sprite.

Write down what you think the user will see when the flag is clicked.

__________________________________________________________________

__________________________________________________________________

Why? _____________________________________________________________

__________________________________________________________________

Now try out the code yourself and see if you were right.

Page 14
Starting from Scratch 1: Scratching the Surface

1.4 In the stack of blocks below, how many times does the sprite move 10 steps?

__________________________________________________________________

1.5 A programmer wants the cat to dance to some music. However, the cat doesn’t
start dancing until after the music has finished!

Why is this?

__________________________________________________________________

__________________________________________________________________

Page 15
Starting from Scratch 1: Scratching the Surface

1.6 In the example below, a programmer has chosen a piece of music (sound
“Xylo1”) to play during a game. However, when the flag is clicked, the
computer just plays the first note of the music – over and over again!

What mistake has the programmer made?

__________________________________________

__________________________________________

1.7 In Extension 1: Dance Away, you made a sprite dance to a tune you created.
There were two ways you could do this:

 have a single script with the movement blocks amongst the play note blocks
 have separate scripts for the same sprite – one script plays the tune whilst the
other makes the sprite dance.

Why do you think experienced programmers would use separate scripts?

__________________________________________________________________

__________________________________________________________________

1.8 Make up a question like those from 1.1–1.6 and pass it to your neighbour.

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Lazy or smart?
Computer programmers always look for shortcuts to make their lives easier.
A good example is how we used a repeat block in Frère Jacques to repeat the same line
of music instead of having two identical sets of blocks. As well as looking neater, it also
means that you won’t make a mistake when creating a second set of blocks.
Do you think this makes programmers lazy or smart? (Hint: the answer is smart!)

Page 16
Starting from Scratch 2: Story Time

2: Story Time
This lesson will cover
 creating stories and plays
 sequencing instructions
 events

Task 1: A bad joke


Watch screencast BadJoke. This shows how to use Scratch to create a joke or play
between two characters.

Once you have done this, try creating a joke of your own – for example, a “Knock,
Knock” joke – that uses two characters like the one in the example.

Pay attention to when each character (sprite) “speaks” by planning out the code,
including speaking and waiting, like the one below:

Girl Boy
Say “Hey, I’ve got a joke!” for 3 secs Wait 3 secs
Wait 3 secs Say “Okay – let’s hear it!” for 3 secs
Say “My dog’s got no nose” for 3 secs Wait 3 secs
Wait 3 secs Switch to costume of boy shrugging
Say “How does it smell?” for 3 secs
Say “Terrible” for 2 secs Wait 2 secs
Switch to costume of boy laughing
Say “<Groan>” for 3 secs

Write down any problems you had and what you did to overcome them.

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

Page 17
Starting from Scratch 2: Story Time

Task 2: A short play


Write a short story or play. There should be two or three scenes (backdrops) where the
actors (sprites) change costumes.

Keep it simple with only two or three actors (sprites). Write a script on lined paper, with
each actor’s lines side-by-side, as shown in the previous example.

Hint: You can use the broadcast block to let a sprite trigger an event, such as a scene
change e.g.

In the sprite script In the stage script

You can find another screencast (walking_places) to give you some inspiration at
https://scratch.mit.edu/projects/8157/

Extension 1: A walk-on part


Make your characters walk on to the screen and stop at a certain point during the play.

Hint: you will have to start your sprite actors at the edges of the screen and use the
show and hide blocks to make them appear at the correct place every time.

Page 18
Starting from Scratch 2: Story Time

Bugs
A bug is an error which stops your code working as expected. There are two main types
of bug which can occur in a program:
● Syntax error
This happens when the rules of the language have been broken e.g. by mis-
spelling a command. Syntax errors usually stop the code from running.
Languages like Scratch provide code in ready-written blocks, so you won’t make
many syntax errors.
● Logic error
This means your code runs, but doesn’t do what you expect.
Unfortunately, it’s still possible to make logic errors in Scratch!
A third kind of error is also possible:
● Execution (or run-time) error
This means your program crashes (stops running) when it is run (executed).
This may be the result of performing an operation such as division by zero, for
example.
Finding and fixing these errors in a program is known as debugging.

Page 19
Starting from Scratch 2: Story Time

Did you understand?

2.1 The program below shows the scripts for two sprites to tell a joke to each
other. Why would this program not work?

Girl Boy

__________________________________________________________________

__________________________________________________________________

2.2 The program below shows the scripts for two sprites to tell a joke to each
other. Besides being a terrible joke, what is wrong with this program?

__________________________________________________________________

__________________________________________________________________

Page 20
Starting from Scratch 2: Story Time

2.3 The program below shows the scripts for two sprites to tell a joke to
each other. Why would this program not work properly?

Girl Boy

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

2.4 Now make up a “buggy” question of your own and pass it to your neighbour.

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Page 21
Starting from Scratch 2: Story Time

Event-driven programming
Some computer programs just run and continue on their own with no input from the
user e.g. your program to play a tune.
However, many programs react to events (things that happen), such as:
● the click of a mouse or press of a key;
● the tilt of a game controller;
● a swipe of a smartphone screen;
● a body movement detected by a motion-sensing controller such as a Kinect.

In Scratch, event blocks have a curved top (sometimes called a “hat”):

Reacts when the flag is clicked; often used to start


a program.

Reacts when a key is pressed. Click the small black


triangle to select the key you want to detect. Useful
for controlling a sprite, or triggering an action.

Reacts when a sprite is clicked. Useful for controlling


characters in a program.

It is also possible to create your own events in Scratch using the broadcast command.

2.5 Look at the Scratch environment and write down some other events or conditions
that Scratch programs can react to.
Hint: the Control and Sensing blocks are a good place to start.

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Page 22
Starting from Scratch 3: A Mazing Game

3: A Mazing Game
This lesson will cover
 Game creation
 Collision detection

Introduction
You are going to create a simple game where
the player guides an “explorer” character
around a maze using the arrow keys.

The game will end when the explorer rescues its friend in the middle.

Introduction
Watch screencast Maze to learn how to create the Maze game.

Task 1: Setting the scene


Set up the game by importing the stage costume (Maze) and two sprites – an explorer
and a friend for the explorer to rescue. Don’t do any more at this point.

The Importance of Design


Before we make anything – a house, a dress or a computer program – we should start
with a design. Because there are two important parts to most programs – the interface
(how it looks) and the code – we design these separately.
● The easiest way to design the interface is by sketching it out on paper.
● To design the code, write out a list of steps it will have to perform in English.
This is known as an algorithm and is just like the steps in a food recipe.
Solving problems like this is what programming is really about, rather than entering
commands on the computer.
All good programmers design algorithms before starting to code!

Page 23
Starting from Scratch 3: A Mazing Game

Task 2: Designing the solution


Let’s look again at the two main things we need to code in our game:

1. moving the explorer;


2. reaching centre of the maze (and rescuing the explorer’s friend).

The table below shows an algorithm for moving the explorer and Scratch code that does
the same thing.

Algorithm for moving explorer Code

when the flag is clicked

repeat forever

if right arrow key is pressed


point right
move 5 steps

if left arrow key is pressed


point left
move 5 steps

if up arrow key is pressed


point up
move 5 steps

if down arrow key is pressed


point down
move 5 steps

if explorer touches the same


colour as the maze wall
go back to starting position

Algorithms let programmers concentrate on what the program has to do instead of how
to do it on the computer. Once the algorithm is worked out, writing the code is easy!

Notice how an algorithm is indented to show which parts belong inside other parts e.g.

repeat forever
→ if right arrow key is pressed ......... goes inside repeat forever
→ point right .................................... goes inside if right arrow key is pressed
→ move 5 steps ............................... goes inside if right arrow key is pressed

Page 24
Starting from Scratch 3: A Mazing Game

Task 2: Designing the solution (continued)


The table below shows an algorithm for the explorer’s friend sprite.

From this algorithm, see if you can create the code yourself. Remember to put it in the
friend sprite!

Algorithm for reaching centre of maze Code for friend sprite

when the flag is clicked

show sprite

repeat forever

if touching explorer sprite Code this one yourself!

say “Thank you!”

hide sprite

stop all scripts

Now test your game to see if it works.

Extension 1: Getting in tune


Add a background tune to your game (sound “xylo1” seems to suit, but
choose what you think sounds best).

Think about the following:

 Where would be the best place to store this, since it applies to the whole game?
 How will you get the music to keep playing?
 Should you play sound or play sound until done block to play the music?

Extension 2: Add an enemy


Add a sprite that constantly moves back and forth across the stage.
If your explorer touches the enemy, the explorer should go back to
the start.

Hint: set your enemy sprite to move only left & right.
The if on edge, bounce block is useful to bounce back and forth off the
edge of the stage.

Page 25
Starting from Scratch 3: A Mazing Game

Did you understand?

3.1 A programmer creates a maze game like the one you’ve just created.
Unfortunately, her character doesn’t move as expected.

What mistake has she made?

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

Page 26
Starting from Scratch 3: A Mazing Game

3.2 Look at the examples of code below.

Do they perform the same task? ________________________________________

Explain your answer __________________________________________________

__________________________________________________________________

__________________________________________________________________

Page 27
Starting from Scratch 3: A Mazing Game

3.3 The code below controls a sprite going round a maze. If the sprite touches the
side of the maze (the colour blue), it returns to its starting position of -150, 150.

Unfortunately, the sprite sometimes touches the walls of the maze and returns to
the start when the player doesn’t expect.

What mistake has the programmer made?

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

Page 28
Starting from Scratch 3: A Mazing Game

3.4 In this example, the sprite is supposed to return to the centre of the maze when it
touches the sides (coloured blue); however, it only does this sometimes.

What mistake has the programmer made?

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

______________________________

Page 29
Starting from Scratch 3: A Mazing Game

3.5 In this example, the sprite never returns to starting position, even if it touches the
walls of the maze (coloured blue).

What mistake has the programmer made?

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

_____________________________

3.6 Now make up a buggy question of your own and pass it to your neighbour.

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Page 30
Starting from Scratch 4: Get the Picture?

4: Get the Picture?


This lesson will cover
 The Scratch environment
o Sprites
o Code blocks
 Fixed loops
 Programming computer graphics

Introduction
In this lesson, you will write programs to create simple computer graphics using
Scratch’s Pen blocks.

Task 1: Shaping up
Watch screencast Graphics. This demonstrates how to use Scratch to create some
simple computer graphics (pictures).

Complete the table by writing the code to create a heptagon (7 sides) and triangle:

Square Pentagon Hexagon Heptagon Triangle

Now try out your programs. Either double-click on the stacks of blocks or – even better –
make your own block for each shape.

Did your programs work? ___________________________________________________

If not, why not? __________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

Page 31
Starting from Scratch 4: Get the Picture?

The Rule of Turn


Did you spot the pattern here?

In every shape, we turned a full circle (360°). To work out how many degrees we need to
make at each turn, simply…

Divide the total number of degrees turned in the shape by the number of turns taken

So… in a square, we go round 360° in 4 turns, so 360/4 = 90° per turn;


in a pentagon, we go round 360° in 5 turns, so 360/5 = 72° per turn.

Task 2: You’re a star!


Now use the Rule of Turn above to draw a five-pointed star (opposite).
Hint: Pay careful attention to what the rule says!

Task 3: Circle
Create a circle. This is easier than you might think: simply

repeat 36 times
move 5 steps
turn 10 degrees

Task 4: Circular pattern


Make a pattern out of 36 squares arranged in a circle of their own.

repeat 36 times
draw a square .......... put the code to draw a square here
turn 10 degrees

Try changing the shape to squares, triangles or hexagons.

Nesting
In Task 4, we saw one repeat loop inside another – this is called a nested loop.
In this case, the program starts the outer repeat, then enters the inner repeat, which
carries on until it’s finished. The outer repeat then carries on and so on.
Add a wait 0.1 secs command in your code to see this happening more slowly.

Page 32
Starting from Scratch 4: Get the Picture?

Extension 1: The main event


Create your own procedures to draw each of the shapes you have already created
(square, triangle, pentagon, etc.).

Once you have done this, adapt your program for Task 4: Circular pattern that calls up a
square procedure for the repeating shape.

Extension 2: Our house


Draw a house like the one shown opposite.
Write an algorithm – that is, plan the steps out on paper –
before you try to code this!
You will need to use penup and pendown blocks.

Hint: Think about how you could use procedures to reduce


the amount of code you create.

Extension 3: Mmm… doughnuts


Adapt the pattern above to create a multi-coloured doughnut shape.
Write an algorithm before you try to code this!

Hint: There are 36 circles, but the pen moves slightly – with the
pen up – before putting the pen down and drawing the next one.
The program also uses the change pen color by block to make it colourful.

Page 33
Starting from Scratch 4: Get the Picture?

Extension 4: The Olympic Rings5


This is hard! Try to write a program to draw the five
Olympic rings. Write an algorithm before you try to
code this!

Hint: make each circle using a procedure and think about the spacing between the
centre points.

Did you know…? The Olympic flag was flown for the first time at the 1920 Summer Olympics in Antwerp,
Belgium and has been flown at every Olympic Games ever since.
The five rings represent the five continents of America, Africa, Asia, Australasia, Europe.
The colours – blue, yellow, black, green and red on a white background – were chosen
because every nation had at least one of them on its national flag.

5
The Olympic rings symbol is reproduced by kind permission of the International Olympic
Committee. The Olympic rings are the exclusive property of the International Olympic Committee
(IOC). The Olympic rings are protected around the world in the name of the IOC by trademarks or
national legislations and cannot be used without the IOC’s prior written consent.

Page 34
Starting from Scratch 4: Get the Picture?

Did you understand?

4.1 The program from the screencast is shown below. Suggest any way(s) in which it
could be made more efficient.

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Page 35
Starting from Scratch 4: Get the Picture?

4.2 Look at the program below.


Write down the order in which the scripts are carried out after the flag is clicked
(number them in order 1, 2 and 3).

Number Script

Now describe what the code will do.

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Page 36
Starting from Scratch 4: Get the Picture?

4.3 Look at the code examples below.

a) How many times will the sprite move 10 steps? _____

Why? ________________________________________

______________________________________________

______________________________________________

b) How many times will the sprite move 10 steps? _____

Why? ________________________________________

_____________________________________________

_____________________________________________

4.4 Discuss the following “procedures” from real life. Write an “algorithm” for each
one!
a) Getting ready for school b) Making breakfast

Think: In each example, are there steps that could go in separate scripts and get
carried out at the same time?

Page 37
Starting from Scratch 4: Get the Picture?

4.5 In the Storytime activity you used the broadcast event to send a message
between a sprite and the stage.

In this example, a programmer is using a broadcast event instead of a procedure


to create a circular pattern of squares like the one labelled “Correct” below.
Unfortunately, it always goes wrong, displaying the pattern labelled “Wrong”.
Correct Wrong

Look at the programmer’s code opposite. What mistake


has he made?
Hint: it’s something to do with how fast the computer
works.

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

Now enter the code above and run it to see the mistake for yourself. Once you
have done this, create a procedure to draw the square and use it in the first
script instead of the broadcast square block.

What does this tell you about the way that the broadcast command works
compared to a procedure ?

__________________________________________________________________

__________________________________________________________________

Page 38
Starting from Scratch 4: Get the Picture?

4.6 Now make up a “buggy” question of your own and pass it to your neighbour.

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Did you understand? (Extension 3 only)

4.7 A programmer tries to draw a doughnut like the one in Extension 3.


Unfortunately, it just draws lots of circles on top of each other.

What mistake has she made?


Don’t worry if you can’t see it straight away – this
is tricky! If necessary, enter the script into Scratch
and run it to help you understand what’s going
on.

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

Page 39
Starting from Scratch 4: Get the Picture?

Page 40
Starting from Scratch 5: Forest Archery Game

5: Forest Archery Game


This lesson will cover
 Decision statements
 Conditional loops
 Variables
 Random numbers
 Animation
 Sound

Introduction
Watch screencast ForestArchery to see how to create this game.

Task 1: Designing the solution


Let’s look again at the two main things we need to code in our game:

1. moving the target;


2. shooting the target.

Try to code your program from the algorithms given overleaf, rather than looking at
the screencast again.

Algorithm to move target (in Target sprite)


when flag is clicked
repeat forever
glide in 1 second to a random position*

* x is a random number from -240 to 240


y is a random number from -180 to 180

Algorithm to move sight and shoot (in Sight sprite) /…

Page 41
Starting from Scratch 5: Forest Archery Game

Algorithm to move sight and shoot (in Sight sprite)


when flag is clicked
repeat forever
go to mouse location (the mouse x and mouse y positions)
if the mouse button is down (the user has clicked the mouse)
if the sprite is touching the target sprite
add 1 to score variable
play Pop! sound
Say “Hit!” for 0.5 seconds

Task 2: Hit and miss


Change your code to make the program count misses as well as hits (taking off 1 point
from the score):

If touching target
change score by 1
play Pop! sound
Say “Hit!” for 0.5 seconds
else
change score by -1
play sound
say “Miss!” for 0.5 seconds

Task 3: Against the clock


Add a timer variable to your program which makes the game last 30 seconds. Make the
variable appear on the screen as it counts down from 30 to 0.

when flag is clicked


repeat 30 times
wait 1 second
change time by -1
stop all scripts

Page 42
Starting from Scratch 5: Forest Archery Game

Task 4: Bullseye!
Using if and touching colour blocks, change the program so that when the target is
hit, it adds the following to the score:

 White – 1 point
 Black – 2 points
 Blue – 3 points
 Red – 4 points
 Gold – 5 points (and says “Bullseye!”)

Task 5: Stay positive!


Adapt the program so that the user will never get a negative score.
Hint: take off a point only if the score is above zero.

Did you know…? Humans are known to have practised archery for
at least 10,000 years. It was first used for hunting
(see cave painting opposite6), then in warfare.
In medieval England, it was compulsory for all men
to practise archery regularly, so they would be
skilled if required to go to war.
Nowadays, archery is a popular leisure activity
enjoyed by people all around the world.

6
© Instituto de Turismo de España (TURESPAÑA). Image of cave painting from Cova dels Cavalls
remains the exclusive property of Turespaña and cannot be used or reproduced without
Turespaña’s prior written consent.

Page 43
Starting from Scratch 5: Forest Archery Game

Variables
In this game, we introduced the idea of keeping a score using a variable block.
A variable is a space in a computer’s memory where we can hold information used by
our program – just like storing things in a box.
We should always give a variable a sensible name that tells us
what kind of information is stored in it – just like putting a label
on the box to tell us what’s inside.
To create a variable in Scratch, we make a variable block.
Once a variable is created, the information stored inside it can be
set or changed (that is, varied – hence the word “variable”).

Extension 1: A Mazing cool feature


We’re now going to add a new feature to your Maze game from lesson 3 – a timer that
gives the user 30 seconds to finish the game.

To do this, add a variable called time and create a new script that does the following:

when flag is clicked


set variable (time) to 30
repeat until time = 0
wait 1 second
subtract 1 from variable (time)
say “You Lose”
stop all scripts

Before you write this script, think about where might be the best place to put it.
Hint: is it something that applies to a single sprite or the whole game?

Extension 2: A harder maze


Now create a maze of your own which has more than one route to the middle.
Hint: Just create a new stage backdrop for this.

Page 44
Starting from Scratch 5: Forest Archery Game

Extension 3: Do I get a prize?


Create new sprites in your Mazing game to act as bonuses along the way.

These should disappear (hide) when the explorer touches them and add to a score
variable. Be sure to place some of them away from the quickest route around the maze
to make it more challenging!

Extension 4: Now you see it…


Add some code to your Mazing game that shows and hides your bonus sprites after
random times e.g. between 1 and 5 seconds (but experiment to see what works best).

Page 45
Starting from Scratch 5: Forest Archery Game

Did you understand?

5.1 Look at the script below to make a timer variable count down from 30 to 0.

Will it work? ____________________________

Explain your answer ______________________

______________________________________

______________________________________

______________________________________

5.2 Now make up a buggy question of your own and pass it to your neighbour.

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Page 46
Starting from Scratch Summary

Summary

Computing Science concepts


In this course, you have learned about some important ideas within Computing Science:

 What a computer is
 Types of computer
 Hardware
 Software
 Program design, including algorithms
 Bugs

Programming structures/commands
You have also used the following programming features:

 Reacting to events
 Decision-making
o if…then
o if…then…else
 Variables – for example
o scores
o timers
 Loops
o fixed (repeat, forever)
o conditional (forever if)
 Collision detection
o touching sprite
o touching colour

Scratch has many more commands, but you have now learned enough to go on to the
next stage.

Scratch features/…

Page 47
Starting from Scratch Summary

Scratch features
You have also learned about the following features of Scratch:

 Sprites & stage


 Properties
o Scripts
o Costumes/backdrops
o Sounds
 Animation
 Graphics tools

You now have all the skills you need to create some really amazing Scratch projects!

Page 48
Starting from Scratch Scratch Project

Scratch Project
Working in a pair or group, you are now going to create a Scratch project of your own!

You may have some ideas already, but programs are normally created in a series of
stages:

1. Analyse
2. Design
3. Implement
4. Test
5. Document
6. Evaluate
7. Maintain

Or… A Dance In The Dark Every Midnight!

Page 49
Starting from Scratch Scratch Project

Analyse
Working in pairs or small groups, brainstorm three ideas for your project.

Think of the areas you’ve covered so far...


Is it going to be music or graphics-based? A story? A game?
Think of how it might link in with other subject areas you’re studying.

The Scratch gallery at https://scratch.mit.edu/explore/ might give you some ideas.

1. __________________________________________________________________

__________________________________________________________________

2. __________________________________________________________________

__________________________________________________________________

3. __________________________________________________________________

__________________________________________________________________

Now discuss your ideas with your teacher.

Once you have agreed on your project, describe what it will do below.

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

Page 50
Starting from Scratch Scratch Project

Design (Screen)
Make a storyboard of your project.
Your sketch should be labelled to show what is happening and what each sprite does.

Page 51
Starting from Scratch Scratch Project

Design (Code)
Design the steps for your code (algorithm):

 Think about the steps each sprite or the stage will have to perform. Write them in
English.
 Think about variables your project will use.

Sprite/Stage Algorithm

Page 52
Starting from Scratch Scratch Project

Sprite/Stage Algorithm

Page 53
Starting from Scratch Scratch Project

 Think about variables your project will use.

Variable name What it will store

Page 54
Starting from Scratch Scratch Project

Implement
Now create your project!

 Gather the sprites, costumes, sounds and backdrops


Remember to give them sensible names.
 Then create the scripts
Make sure you have your algorithms in front of you!

Test
Test your project to make sure it works.

Let your classmates test it too and note their comments below:

Good points: _____________________________________________________________

_______________________________________________________________________

Bad points: ______________________________________________________________

_______________________________________________________________________

Describe bugs that were found (by you or by testers) and how you fixed them:

Bug: ___________________________________________________________________

Solution: ________________________________________________________________

_______________________________________________________________________

Bug: ___________________________________________________________________

Solution: ________________________________________________________________

_______________________________________________________________________

Page 55
Starting from Scratch Scratch Project

Document

You’re now going to post your project on the Scratch website.

Write down below a brief description (50 words max.) of:

 your project’s main features and

 how to use them.

Remember – you want to get people to try out your project!

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

Once you have written the description, enter it into your project’s Notes and Credits on
your project’s page on the Scratch website (- you will need to be logged into the Scratch
website and click to do so).

Page 56
Starting from Scratch Scratch Project

Evaluate

How did the project turn out compared to how you originally planned it?

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

What mistakes did you make on the way?

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

If you were to start again from the beginning, what would you do differently?

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

Look at your code again.


Is there anywhere you could have taken a shortcut to make it “slicker”?

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

Page 57
Starting from Scratch Scratch Project

Maintain

What additional features would make your project better?

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

Congratulations
You have now completed this introduction to Computing Science in Scratch!

Remember that you can use Scratch at home, so there’s no need for this to be the end
of your time as a programmer.

http://scratch.mit.edu

Page 58

You might also like