0% found this document useful (0 votes)
187 views42 pages

Teaching Scratch: at Primary School

Scratch

Uploaded by

qemal2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views42 pages

Teaching Scratch: at Primary School

Scratch

Uploaded by

qemal2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

First Edition

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.

In Gothenburg, Thomas started a new education: Software Engi-


neering and Management. This education combines technical

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.

In scratch, pupils can choose their own language. I am doubtful


whether it makes any difference to program in your own lan-
guage or in English, but it may lower the barrier for some pu-
pils.

Web Interface Joining Scratch


We use the web interface scratch.mit.edu to access the pro- Pupils can become registered scratch users by creating a user
gramming language Scratch. account. This has as an advantage that they can save the pro-
gram they build and probably continue with the material later, in
school or at home. But... discuss the risks before you let pupils
join. Moreover, be aware that some pupils have clear instruc-
tions from their parents not to sign up online without explicit per-
mission. Other pupils do not have their own email address,
email is rather old fashion and the youngsters have different
channels to communicate with their peers.

Registration is not necessary for the lecture and I recommend


not to start with this, since it takes quite some time. Even after
writing a program, they can register and save their results.

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.

Sound and Music


Scratch offers a wide variety of sound effects, music and songs.
Music and sounds are very serious elements in computer
games and the creativity and technical skills needed to add
good sound tracks to a game should not be underestimated.
Nevertheless, I have chosen not to include sound effects for the
practical reason that when 20 pupils start playing with different Alternatively, one chooses ‘small stage layout’ from the Edit
sound effects in a class room, it quickly gets very noisy. In menu.
some schools they have for that reason computers without
speakers; pupils bring their headphones. This is a brilliant solu-
tion for individual assignments, but slightly less attractive for
group teaching.

Small stage layout


When presenting scratch in class, one may be challenged by a
projector with relatively low resolution. In those cases it is good
7
Running Beetle

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

Basic • Background and sprite graphics.

• The concept of a program

• Events in the form of key presses and actions taken upon


events

• Control structures: loop and conditional choices

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.

This lesson as an example project to play with and learn from


can be found at scratch.mit.edu/projects/27697024/
Learning objectives
Sprite
This is an introductory lesson that can be taught to pupils com-
Create a new project. It shows as a white background with a cat
pletely new to scratch. It is presupposed that pupils have been
as one and only sprite. We start with choosing a sprite for walk-
using a drawing program before and are familiar with an inter-
ing around the track.
net browser and using the mouse and keyboard.

The learning objective is to make them familiar with the scratch


software development environment. In particular the following
concepts are introduced:

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.

Drawing the background takes 20 seconds, but can easily take


five to ten minutes in class. Partly, this is due to sometimes sur-
prisingly poor quality of pointing devices such as a mouse in
school computers. It is also due to the fact that some pupils
may not have used a drawing program before... whereas others
spend time with an explosion of creativity.

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.

Turning the beetle


We have now been able to let the beetle run itself of the track.
The player should take control and make sure the beetle stays
on the track! We make the beetle react on key press events,
the left and right arrow. When pressing the left arrow, the beetle
turns a bit to the left, when pressing the right arrow, the beetle
turns a bit to the right.

12
Section 2

Extended ished. The beetle, being Swedish, shouts ‘mål’ when getting
over the finish line.

Start in right direction


Restarting the game puts the sprite back in the right position,
but not in the right angle. Scratch does allow you to put your
sprite in one of four directions, 0, 90, 180 and -90 degrees. For
A finish line some designed tracks this may do the job, but for other tracks,
you probably want to start in an angle of 73 degrees. This can
In order to make the game end, we add a finish line. Since the
be done by first turning to 0 and then use ‘turn X degrees’. This
beetle will detect when it goes over this line, we draw the line
should be put in the beginning of the script, where the start posi-
on the background just be-
tion is chosen.
hind the start position of the
beetle. That is, the beetle
should not have to cross
the line when starting.

The extension to the script


is that the beetle should
now detect when it touches
the line. By taking the color
yellow, we can use exactly
the same mechanism for de-
tecting the line as we did for
detecting when the beetle
ran on the grass. When the
beetle touches the yellow line, the game is over, but now we fin-
13
Section 3

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

Results from pupils

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

Basic • Control structures: loop and conditional choices

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.

This lesson as an example project to play with and learn from


can be found at scratch.mit.edu/projects/32369372/

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.

• Sprite rotation styles and bouncing

• Scripts connected to the background as well as scripts con-


nected to sprites

• Communication between scripts

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.

This makes the penguin walk,


row keys. When we press the left arrow, the penguin should go but the poor sprite walks off
left, when we press the right arrow, the penguin should go right. the screen. In particularly no-
ticeable when walking up. A
Demonstrate how the most simplistic solution does not work,
rather simple solution to pre-
the penguin goes to the right, no matter which button we press
vent this is to have the sprite
and when at the end of the field, it gets stuck.
bounce at the edges.

As a side note, notice the difference in layout, four arrow key


events next to each other, or in a layout similar to the buttons
on a joystick. The layout of program text, the elegance of writ-
ing in one or the other way may be pointed out. Programming is
like writing texts, layout does matter.

Now the penguin can walk around on the field.

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.

Surprise! The ball goes in the wrong direction.

We never told the ball in which direction to move. The default


direction was to the right. Thus, it just worked in the beginning.
After bouncing, the direction was automatically reversed and
Demonstrate what happens:
getting behind the ball and kick it made it go to the left. But
eventually, the ball disappears
then, when the ball moved to the left, we can kick it to the right,
from the screen. In addition to
but the ball will move left.
that this is a bit awkward visu-
ally, it also means that the pen- The solution here is slightly
guin looses the ball and cannot advanced. We want to have
kick it back on the field. the ball move in any possible
direction, not only left to
The remedy for the ball running from the screen is already
right, but also in 5 degrees
known from the penguin: bounce at the edge. After bouncing, it
up, or 73 degrees down. The
is possible to approach the ball from the other side, that is kick-
angle is determined by the

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".

Note that we move the ball to the starting position at once,


whereas we glide the penguin to that position.

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-

cept: a variable. A variable provides a name to a certain value.


We select ‘Data’ in the Scripts menu of the background. We
make a new variable score that we can use as a place holder
for the number of goals the player has made.

The value of variables is shown on the screen when it is se-


lected. In this case we want that, if not, unselect it.

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.

If the duck moves too slow, increase the y position by more


than 2.

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

Results from pupils

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

Basic • Working with coordinates x and y

• Communication between scripts

• Control structures: wait until versus repeat until, if-then-else.

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.

This lesson as an example project to play with and learn from


can be found at scratch.mit.edu/projects/41417616/

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:

• Sprite costumes, animating

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

To prevent the dog from walking up-side-down we change its


orientation from free rotation to left-right rotation style. Note that
we also changed that for the bat before hiding it. We have ex-
plored this with the penguin of Chapter 3.

Probably somewhat surprisingly, we need to also change the ro-


tation style of the tree and the ball to rotation style left-right. We
are going to move these objects in two directions and want to

prevent them from turning up-side-down. In particular the tree,


since for the ball, this is less noticeable.

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.

Grab the ball


When the dog is close to the ball, we want it to take the ball in
its mouth and walk. After that, we want the dog to walk away
with the ball, in either direction.

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.

As simple as this looks, it contains a software error. You can


demonstrate this by pressing the space before the dog has the
ball. The ball will move down and the dog will not be able to
reach it. How are we going to solve this? Sure, the if-then-else
is important here.
Now the dog is ready to walk around with the ball in its mouth.

Drop the ball


We would like to be able to have the dog drop the ball and pick
it up again. In that way, we could make it more into a game
where the dog delivers the ball somewhere, which would pro-
vide a reward.

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.

Making it possible to grab the ball again is more difficult than


one would suspect. The reason is that when we drop the ball, it
should return to a position in which it is not touched by the dog,
but can be touched. We leave this to the advanced section.

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.

Depending on the computer and settings, it takes about 6 sec-


onds for the dog to reach the ball and 3 seconds for the bat to
reach it. It would be fair to have the bat come in somewhere be-
tween 2 and 6 seconds. Sometimes it would be hard, some-
The playing sensation is much more realistic now. Indeed, the
times an easy game.
speed which with the bat flies to the ball is constant, independ-
ent of the direction the dog is running. But it has also become
extremely hard for the player to have the dog grab the ball and
run away with it. It depends on the keyboard settings and how
many repeated key presses are produced when one holds the
right arrow pressed down.

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.

Bat disappears with the ball


When the bat reaches the ball, it will take it, no matter whether
the dog got it in its mouth or not. Both the bat and the ball need
to be aware of this. The bat already knows this, as you can see
from the script, since it checks whether it is touching the ball.
Therefore, we make the bat broadcast this fact to the ball. We
make the ball disappear when the bat gets it and we make the

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.

Since the x-position of a sprite seems not to be able to get be-


yond the edge of the stage, we use our own variable to hold the
position. We make a variable x, local to this sprite only that we
use as our x-position.

Tree and ball distance


As we already observed in the basic section of this chapter, the
ball and tree can get arbitrary close to each other. This can be

This x continues counting up or down even when moving of


stage. Now we need to add a check that whenever we are in-

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.

For the ball, we use a similar


change. We add a variable x
Now the dog can walk away from tree and ball and find them
only for that sprite.
back in the same position they have been left.
Then we add a check for visibil-
ity as a block that checks Grabbing the ball again
whether x is between -245 and So far, when the dog has dropped the ball, it cannot get it back
245, which for the ball are the any more. The reason is that we only check that the dog is
edges. And finally we add the touching the ball once. Let us be naive and put a ‘forever’ state-
check inside the events for left ment around the check for touching the dog and see what hap-
and right arrow pressed. pens.

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.

By visualizing the dogs direction and the x position of the ball


(under ‘Motion’ there are some ‘variables’ that one can select in
Such large conditions are hard to read and when teaching this,
both the dog sprite and ball sprite’), we can observe this rela-
many pupils may get lost. Explain it in small steps.
tion.
When the dog is walking to the right, it grabs the ball. The dog
If the dog walks to the right and the ball is around position 27,
can drop it, step once more to the right, grab it again. If dropped
we can imagine that the dog grabs the ball. However, if the dog
and walking turning around, the ball stays where it is. But there

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy