Teaching Scratch: at Primary School
Teaching Scratch: at Primary School
Teaching
Scratch
at primary school
Thomas Arts
Preface
Therefore, it is advisable to tell the pupils at the beginning of
Teaching
each lesson that such deviations are allowed, but that the more
The course material in this binder is developed for children in
they do so, the harder it gets for them.
the age 9 to 13. It is written for teachers and parents that want
to support children in learning how to program. Each lesson has a basic, extended and advanced part. In an
hour, one should be able to teach the basic part for younger pu-
The underlying ambition is to make those pupils feel program-
pils and get up to the extended material for older pupils. The ad-
ming is a cool activity and to give them the confidence to con-
vanced part is provided for pupils that want to do a bit more
tinue the projects at home. The teaching style I have used is
themselves or classes that are experienced from earlier pro-
demonstration in front of the class, where after showing a small
gramming exposure and finish earlier than expected.
step, the pupils perform the same step on their own computer.
The lessons are based around simple games. Games are cho-
Notably, such a simple step like: ‘change the color of your back-
sen for motivational purposes, but also because I like the chil-
ground’ takes up to 5 minutes in a class with twenty pupils new
dren to understand the labour and creativity that has gone into
to the material. Some can do this quickly, others need more
the games they are playing. I would like them to appreciate the
help. The challenge therefore is to make steps that are doable
work of programmers and game developers and create interest
for all, but can be done more ambitious by those pupils that
for the profession.
grasp the idea quickly.
Although, I have been teaching this material at Swedish primary
Lessons take about 60 minutes. The intention is that the
schools, I present it in English. Neither of those is my native lan-
teacher builds the program from the beginning and that the pu-
guage. Help in translating this material to Swedish or different
pils repeat the steps. During this repetition, the teacher walks
languages that I am less proficient in is much appreciated.
around and helps those that are in need of help. The pupils love
to get freedom in which colors they choose, what sprite they Thomas Arts
may use and such. The course material allows for this freedom.
It is a great experience to have 20 different solutions at the end
of an hour teaching. However, the more freedom the pupils are
offered, the more difficult they will experience the lessons are.
ii
skills with managerial skills. The need for such a program had
arisen by the observation that the IT industry needs more peo-
The Author ple than only a few very technically skilled people. The society
When he was 9 years old Thomas Arts started programming needs cross-disciplinary people that can bridge technology,
himself on an Apple ][ in Basic. At a very early age he won na- business, art, medicine, and sciences.
tional programming competitions. To nobodies surprise, he
As a professor at Chalmers University of Technology, Thomas
started studying the brand new computer science education in
collaborated with many smart scientists and with one of them,
Nijmegen, the Netherlands in combination with a masters in
he started a company to market and sell one of their inventions:
Mathematics. In those days, parents were not sure there would
QuickCheck. This is a program that automatically generates
be a living in just learning computer science. Mathematics at
test cases, instead of having people write them by hand. Quick-
least gave some security on the job market.
Check became a success and is now widely used to test tele-
After a double masters, Thomas started a PhD education at communication software, automotive software and also internet
Utrecht University in theoretical computer science, far closer to applications and games.
mathematics than to programming games. Although his PhD
Thomas’ two children made him interested in teaching scratch.
work was very successful, gave hundreds of scientific citations
Textual programming languages are simply not that attractive
and even today has a community of researchers continuing that
for young people; too much effort for too little result. With
work, Thomas decided to make a change to industry. He
scratch, programming is fun and gives results in a few minutes.
started working for Ericsson’s computer science laboratory in
Stockholm. Ericsson invented the, at that time, new language
Erlang. Thomas joined this lab of creative researchers and
worked on software that tests and verifies other software. Many
scientific publications and inventions later, Thomas returned to
academia at the university in Gothenburg.
iii
© Thomas Arts, 2015
This material is copyrighted by Thomas Arts. It may be spread and distributed freely
as long as it is unchanged. The programs referred to in this book can be copied
freely and used for teaching with the only restriction that the origin of the work is ac-
knowledged.
This copy is created for Kodcentrum (www.kodcentrum.se) and may be freely distrib-
uted by this organization. Kodcentrum uses this and other material to teach children
scratch in Sweden.
iv
Scratch
1
This chapter describes the
setting in which the
programming language
Scratch is used in this
book.
Chapter 1
Scratch At the beginning of each lecture, provide the pupils with this ad-
dress on a piece of paper or write it on the whiteboard.
After being registered, one can share a project with the world.
There is no restricted sharing, it always is with the world. Peo-
ple can leave comments to your shared projects. This opens a
communication channel that can be misused for bullying, get-
6
ting in touch via additional channels and more. Since one pro- to be aware the button that reduces the size of the left two win-
vides age and gender, being in the range 9-13 this way of open- dows and increases the area in which the program is con-
ing up requires a maturity in internet presence. structed. There is a little triangle next to the coordinates of the
screen that shrink and grow the different areas.
Joining scratch is necessary for enthusiastic users. However, it
should be a weighted choice that parents are aware of, or it
should be done via accounts provided by the course provider. It
is a good opportunity to talk about risks of being visible on the
internet, but that would take a large part of the lecture. For the
lectures it is not needed to be registered.
2
In the first lecture we
develop a little game in
which a beetle runs around
on a track steered by the
player.
The goal for the player is to
bring the beetle over the
finish line.
Section 1
Introduction
Start the lecture by showing the final game, such that the pupils
know what they aim for. Guide the beetle over the finish line by
using the arrow keys, but also show what happens if the beetle
gets off track.
9
Show how one removes the cat: click the scissors in the menu have to feel when it leaves the track by observing the color it
bar and then cat. walks on.
Show how to choose a new sprite: click the sprite logo. In the bottom left corner the stage is visualized. Each different
background is called a backdrop. We choose for an oval to
Reduce the size of the sprite: click the shrink button on the right
make programming simple; square shapes or a laying 8 would
of the scissors in the menu bar. (Reducing the size makes it far
work as well, but may be too advanced for the first lecture.
easier to draw a track and to stay on the track when playing.)
Use the fill color icon and color green to make the picture
Background green.
We create an oval track on a green background. The back-
Select the brush icon, select the grey color, increase the size of
ground color should contrast from the track, since the beetle will
the pencil with the slider and draw an oval.
Do not draw the finish line, that's part of the extended material.
Having a beetle run in circles is already a challenge for the
youngest.
Pay attention to the fact that pupils make the track broad
enough for the sprite and that all sides are surrounded by the
contrast color.
10
bump into the wall of the screen. Discuss the relation between
The first script
the speed and the 10, show that when you lower the 10 to 1,
Now that the background is in place, we can start to make the
the beetle moves much slower. After that, let the pupils write
first program. In this case, the script belongs to the beetle. It is
their first script.
natural, but also important to have pupils understand that
scripts belong to certain objects. Select the beetle sprite, put it
Detecting the grass
on the drawn track if it isn't already there and show the script
Sprites can detect the color they touch, thus the beetle, walking
page. The goal is now to make pupils aware that the computer
over the grey surface can detect departing that part by touching
can control the beetle. After that, we give the player additional
the color of the surrounding; in this case green.
control over the beetle. But, the fact that their script controls the
beetle is the important concept to convey. This is where the conditional control structure comes in, check
whether the color is green. If the sprite touches the green color,
Start with moving the beetle 10 steps and the flag event to start
then the game should be over. We notify this to the player by
the program. Every time you click the flag, the beetle goes 10
having the beetle say ‘game over’.
steps forward. Then explain that you do not want to click, but
that the computer should do so. This introduces the first control
structure forever. Demonstrate that this quickly let the beetle
11
When demoing this, we quickly find out that the beetle does not
move to its starting point when we press the green flag to start
the program. Move the beetle manually (with the mouse) to its
starting position and select the motion control go to x: y:. The x
and y coordinates are already those of the present position of
the sprite.
Note that many pupils in the age 9-13 may have difficulties un-
derstanding a coordinate system. Try not to focus on this, since
it is not too important for the exercise. Taking over the math les-
sons by presenting coordinate systems does not make the pro- Go ahead and play the game for a while! This is what pupils
gramming experience more positive. like, to play a game. It's probably the first they have made them-
There are several different colors green. Selecting the back- selves. Some will experiment and make it go slightly faster. Sev-
ground color is done by clicking the color in the touching color eral want it to be able to finish and wish a finish line.
statement. A little hand shows up with which one can then touch
the color on the background to select the preferred color.
12
Section 2
Extended ished. The beetle, being Swedish, shouts ‘mål’ when getting
over the finish line.
Advanced Hint: use a second finish line, almost the same color that is
stuck behind the first finish line.
Creating a maze
Without changing any script, one can change the game from
running a track to moving in a maze. Add a second background
and experiment with it!
Second player In the maze, the game is over if the beetle hits a wall. The
If the track is broad enough, the beetle can turn and finish in the player can win by touching one of the yellow lines, which are a
wrong direction. Clearly this should not count as winning the bit hidden. Alternatively one could add any yellow object that
game. How can you prevent the beetle from winning in this the beetle should reach, such as an apple or banana.
way?
14
Section 4
Below you can see some screens of work that pupils have
made during a class. Children are very creative and come up
with many variants of the same game. Letting them repeat the
same program with different sprites and backgrounds improves
their learning a lot.
15
Playing Soccer
3
The program developed in
this lesson is a little game
in which a penguin kicks a
ball on a soccer field.
The goal for the player is to
kick as many balls in the
goal as possible in a given
time limit.
Section 1
Introduction
Start the lecture by showing the final game, such that the pupils
know what they aim for. Guide the penguin to the ball using the
arrow keys and kick a goal. This is an appetizer, the game is
build up slowly and there are many subtle parts that may cause
trouble for individual pupils.
Sprite
Create a new project and choose two sprites, the penguin and
Learning objectives ball. Resize the penguin to have it slightly smaller than the ball.
This lesson requires some previous scratch experience if one The actual size will turn out to be rather important for the game.
wants to teach it in a 60 minute lecture. The learning objective If the ball is too large or too small, it easily gets stuck in corners
is to make pupils familiar with the scratch software development without the penguin being able to remove it from there. Pupils
environment. In particular the following concepts are intro- will find out for themselves later, that's part of the learning expe-
duced: rience.
17
Another important part is their direction. Most sprites have the
Background
default direction ‘rotate’ when they are chosen, so also our pen-
The background is a kind of soccer field. Once again the learn-
guin. This causes problems when walking around. For a longer
ing goal is not drawing excellent graphics. The field is kept su-
lesson, one can demonstrate what happens with a penguin in
per simple. It need not be green, it need not be soccer, could be
a basket ball field as well. What is important here is the use of
two slightly different colors of white for the goal and circle. The
rotation style, just follow the course material without changing underlying idea is to point out that the computer recognizes col-
direction and the penguin will start walking up-side-down. ors differently from the human eye. Even if white is more or less
the same for humans, it is different for the program. The ball will
later on notice when it touches the goal color.
18
The reason is that the penguin should turn in the direction we
Moving the penguin
make it walk by the arrow keys. In order to do so, we must
The plan is to move the penguin around the field using the ar-
make the sprite turn. There is a special motion command for
that, making the sprite point in a certain direction. There are
four possible directions to choose from and if pupils do not feel
comfortable with degrees, they can read up, down, left, and
right next to the numbers.
19
The ball being kicked
The ball just sits and waits until it is being kicked. Kicking can
be seen as the penguin touching the ball. We use the ‘Sensing’
option in which a sprite can sense what other sprite it is touch-
ing. If we touch the penguin, we move away with a speed larger
than the walking speed of the penguin. In this case 50.
ing it to the left, away from the goal. One can then walk around
the ball and try to kick it towards the goal.
20
direction the penguin comes from. The ball should fly away know? The ball should tell the penguin by sending a message.
from the direction in which the penguin comes. In order to Messages are a special form of ‘Events’.
achieve this easily, we point the ball first towards the penguin
and then 180 degrees away from it. The commands to do so
are in the ‘Motion’ collection.
Let the pupils play. They will find out that the penguin can kick
the ball with the head and actually move it into the goal... al-
though it never really goes in. We can make the penguin celebrate the goal even more by hav-
ing it lifting its left wing while shouting ‘mål’. Each sprite can
Making a goal have a number of costumes, the penguin has two, the one that
If the ball roles into the goal, we want the penguin to shout ‘Mål’ we see and the one with one wing up. We make that visible by
(it's a Swedish penguin). The ball can sense when it touches changing costume.
The basis for a soccer game is created. It might be that one can-
not get much further than this in a one hour lecture, but even at
this point, pupils find it fun to play and explore.
the goal, we have seen that before. But how does the penguin
21
Section 2
Extended the message ‘mål’ to the penguin. We stop the script as soon
as the first message has been sent, the role of the ball is over.
Now we change the game by having the ball and penguin move
to their starting positions as soon as a goal has been made. For
that to work, first use the mouse to place the sprites in the posi-
tion you want them to start and then add these starting posi-
tions at the beginning of the script. Now, since we do not want
Starting again
The game so far has no good ending. When the ball is kicked in
the goal, the penguin is exited, but the ball remains in the goal
and the game does not stop. We want the ball to stop sending
messages, since it will remain in the goal and constantly send
to start the game by the green flag each goal that is made, we
change the green flag start by a message "new game".
The thing left to do is to send the message to both ball and pen-
guin that the game can (re)start. We choose this to be done by
the background. Scripts cannot only be connected to sprites,
also the background can have scripts. It is often good practice
to have the background orchestrate what happens in the game.
See the background as the director of the orchestra, or the arbi-
ter on the soccer field.
22
The background script is the only thing started when the player Since we programmed the penguin to take 1 second to glide
starts the game. After that, it sends ‘new game’ to all sprites. back to its start position, a simple solution is to wait a second
before we move the ball back to the beginning position.
Another strange behavior shows when one starts the game for
the first time. One observes that when playing immediately, the
penguin runs to the ball and is drawn back. At first, one may
think this is due to the fact that one cannot kick the ball during
the first second. After all, we have written the script saying that
Whenever a goal is made, the background will demand a new the ball should wait one second before moving to the right posi-
game to be started. tion. But that second is only a delay in reacting on touching the
penguin.
Unfair play
The penguin is wobbling, because it is gliding to its start posi-
Did you notice that the penguin is playing unfair when a goal is
tion during one second. One way to solve it is to trick the player
made. The ball moves back to the begin position, the penguin
in not going to walk, which can be obtained by not presenting
walks back as well and sometimes kicks the ball when doing so
the ball until it can be kicked. We simply hide the ball for a sec-
before the game actually has started. We have introduced a
software error. We did construct a script that violates the rules
of the game.
23
ond and when we show it again, it is ready to be used. Show-
ing and hiding are part of ‘Looks’.
Presenting a score
Now that we can restart kicking balls, we want to add a counter
for the number of goals made. For this we introduce a new con-
24
Section 3
Advanced
A goalkeeper
A match of finite length So far, it is easy to make a goal, since there is no goalkeeper.
Surely, a soccer match is over after a while. Someone starts a We will add one!
stopwatch at the start of the game and after a fixed time the
game ends. We want our player to play for 60 seconds and af-
ter that, the ball should disappear. We use the build in timer,
which can be found under ‘Sensing’ to start the stopwatch and
wait until the timer reaches the 60 seconds. After these 60 sec-
onds, a message ‘stop game’ is send and the when the ball re-
ceives this message, it hides and stops all scripts associated
with the ball.
25
Use any sprite you like and place the sprite before the goal. We
just make the sprite go up and down. We use the existing mes-
sages ‘start game’ and ‘stop game’ to control the goalkeeper.
Playing the game will quickly show that the ball moves through
the goalkeeper. The poor duck is not even noticed by the ball.
We will change that by chang-
ing the ball script and make it
aware of the duck as much as
it is aware of the penguin.
26
Section 4
Below you can see some screens of work that pupils have con-
structed during a class. Expectedly, they use their fantasy.
27
Palm Beach
4
A dog walks along palm
beach. It tries to grab a ball
that is laying on the beach.
But a bat shows up that
also wants that ball. The
only hope for the dog is to
reach a palm to hide
behind.
Section 1
Introduction
Start the lecture by showing the final game, such that the pupils
know what they aim for. The goal of the game is to have the
dog pick up the ball from the beach before the bat can do so.
The only way to prevent the bat to get the ball is to hide behind
the tree. When the bat bumps into the tree, it falls down and is
gone. The most complex code is that of the bat. The basic part
consists of the dog picking up a ball and walking away with it.
Learning objectives
Sprite
This lesson requires some previous scratch experience if one
Create a new project and choose four sprites, the dog and ball,
wants to teach it in a 60 minute lecture. The learning objective
a palm tree and a bat. Resize all sprites such that they have the
is to make pupils understand how computer games trick the
player in believing that their object walks, whereas in reality the
surroundings moves. In particular the following concepts are in-
troduced:
29
right shape. Hide the bat for the moment, we will only use it we need for walking, but this dog has three costumes to start
only in the extended part. with. Show the pupils how to delete one dog costume.
Each sprite can have one or more costumes. We will make the
dog walk by switching between two of its costumes. We can
make a simpler program if the sprite only has the two costumes
30
Background Moving the dog by its surroundings
This time the background is kept extremely simple, the bottom Whenever the dog moves towards the tree, it should get closer
part is colored yellow, the beach! to the tree. Instead of moving the dog to the tree, we can move
the tree to the dog. Since the key press events go to all the
Walking dog sprites at the same time, we can react on the same key
We make the dog walk by using its two costumes. Whenever
we press the left or right arrow key, the dogs costume is
changed to its next costume. Since we have only two cos-
tumes, the next one is the one not shown.
Surely, the dog should also point in the direction we walk in.
Thus, if we press the left arrow, it should point its nose to the
left and vice versa for the right arrow key.
Let the pupils play with this and have them realize that the dog
moves on the spot without actually making progress. The temp-
tation will be to add a ‘move 10 steps’ command to the scripts. presses. Note that moving to the left is done by a positive num-
The learning objective is to see that instead of moving the dog, ber of steps!
we can now move the tree and the ball.
At the beginning of the game, the tree is moved to its start posi-
tion. For the ball, we can re-use the same scripts, with a differ-
ent start position, of course.
31
Now the dog can walk to the tree and to the ball. This looks Setting the variable in itself has no effect. The effect comes
pretty good until the dog walks pass the ball. The tree stays at from the position of the ball. In the mouth is a different position
the left side of the screen, whereas the ball is still moving to the than on the beach. Now the amazing thing is that the ball stays
left. When the dog turns around and walks towards the ball in the same position when the dog has it in its mouth. The dog
again, the distance between tree and ball is shorter than before. has a fixed position, so has its mouth, so has the ball.
We will address this in the advanced section.
Move the ball to the mouth to record its position. Then replace
the action associated to the right arrow press with an if-then-
else statement. In the case we had so far, the variable ‘in
mouth’ was false and we do what we have done before, move
to the ball to the left. In the case the dog has the ball in the
mouth, we move the ball to that position. Note that we do this
In order for this to work, we introduce a variable that indicates from then on at each right arrow press. Although this seems ex-
whether the ball should be in the mouth on the dog or on the tra work, it makes sense when the dog switches direction, the
beach. We know that when we start, the ball is on the beach. ball should move with the dog’s mouth. Demonstrate this.
The ball waits until it touches the dog, if so, the ball is in the We replace the actions for pressing the left arrow key in a simi-
mouth. lar way by an if-then-else statement that moves the ball in the
mouth of the left facing dog.
32
by moving the ball down. We do not need to know the x position
of the ball, we only need to change the y position. At the same
time, we need to set the variable ‘ball’ to ‘on beach’ again, other-
wise any left or right arrow press will put the ball back in the
dog’s mouth.
Let us use the space bar to have the dog drop the ball. We add
a little action as an effect of pressing space. Dropping is done
33
Now the dog can only drop the ball when it got it in its mouth.
But, when dropped, the dog cannot get it again. Why is that?
Well, the ball only waited for touching the dog once. After that,
that part of the script set the variable to ‘in mouth’ and termi-
nates.
34
Section 2
Extended we want to have it hidden and show it only when it starts flying,
coming in from the right of the screen. We let the bat fly to-
wards the ball until we touch it. The wings of the bat are moved
by choosing the next costume after each move.
The result is a bit disappointing, the bat flaps its wings like a
hummingbird: far too fast. We slow the bat down by a little de-
lay.
A bat flies in
This section deals with the bat that is keen to get the ball and
prepared to take it from the dog. The bat flies, even if the dog is
not moving, in fact, we want the bat to fly faster than the dog
walks. Since the dog walks with a speed of 2 pixels per key
press, we choose 10 pixels for the bat.
The bat flies much more convincing now, but there’s something
strange when playing. When the bat comes in, it has a certain
speed towards the ball, but as soon as the dog runs to the ball,
it is as if the bat goes backward, the distance between bat and
dog increases! Let the pupils play with this and have them expe-
rience the speed difference.
First we move the bat to its start position, then we show it. Re-
member that we had hidden it in the previous section. In fact,
35
Moving the bat Random waiting time
The ball and tree move when left or right arrow key is pressed. We want to make it a surprise when the bat comes in. We must
Similarly, in addition to its normal move, the bat should move a give the player the time to get to the ball, but not too much time.
bit extra when one of these keys is pressed. But the direction of
How much time does the dog need to get to the ball? Start the
the bat is determined by the ball, the bat flies towards the ball.
game and count... or let the computer do the counting. Under
Therefore, we do not use the ‘move’ command, but change the
‘Sensing’ there is a timer that you can make visible. When you
x position of the bat.
start, the timer is on the screen.
We can make the bat go slower in two ways, less than 10 steps
in a move or waiting less than 0.2 seconds. If we reduce the
seconds too much, the bat starts flapping like a hummingbird.
When we reduce the steps too much, the bat will not be able to
fly faster than the dog when the dog runs away with the ball.
36
bat fly away. Surely, the ball is no longer in the mouth of the
A palm in the way
dog.
The dog is scared by the bat, but cannot get away, the bat con-
tinues to follow the dog. We add the palm tree as a rescue. The
dog can get behind the tree, but the bat will bump into it.
At the end, we hide the bat and move it to its start position. The
latter is needed, since if we leave the bat under the tree, the
next time we start, it will touch the palm tree again and never
start flying.
37
Section 3
Advanced We can hide the ball or tree when it reaches an edge. The chal-
lenge is, however, to show the ball again at the right moment.
demonstrated by switching off the bat and walk with the dog to
the left. The ball stays within the screen when reaching the bor-
der and the tree moves closer and closer.
38
side the visible area, for the palm on our screen between -300
and 300, we show the palm, otherwise, we hide it.
Since we need the same check twice, we make our own code
block that checks whether the object is visible and if so, shows
it, if not hides it. Both left and right arrow events use this code
block to show or hide the tree.
39
walks in the other direction with that ball position, it cannot be
grabbed.
We do our experiment by deactivating (taking away the flag)
from the bat, such that bat is not interfering with what we do.
As soon as the dog drops the ball and turns around, it hits the
ball with its back leg and therewith touches it. The ball moves
from the back of the dog in its mouth. A weird view. Somehow, Similarly, when the dog walks to the left and the ball has posi-
the direction of the dog and the position of the ball are more im- tion -47, it is just under the dog’s mouth and can be grabbed.
portant to whether or not the dog can grab the ball, then actu-
ally touching it.
Thus, instead of the check whether the dog touches the ball, we
want the dog to grab the ball if either of those conditions hold.
40
is one software error. If the dog walks away with the ball to the But another tricky issue shows up. When we now drop the ball,
left and drops it, then the first step left thereafter, the ball jumps we cannot grab it any more. When we drop the ball, we put it on
to the tail of the dog. Why is that? an even position, viz. -40. By moving 2 steps each time, we can
never reach x to be -47. We should rather change the position
Keep position synchronized in the mouth, such that when the ball is dropped, it stays on a
When the ball is grabbed, its position is 27, thus the variable x reachable position.
set to 27. We then move
the ball into the mouth of
the dog. Since the ball is
no longer on the beach,
the x position is not up-
dated. Not even when we
turn the dog from right to
left. On the screen, the po-
sition is now -40, but the
variable x is still 27.
The solution is to also up- Moreover, we want the ball to be dropped slightly in front of the
date x when we turn the dog. This is a design decision. If the dog drops the ball and con-
dog. tinues walking, it automatically grabs the ball again. As a conse-
quence, the only way for the dog to leave the ball behind is to
turn around, drop the ball, turn back again and move away.
41