0% found this document useful (0 votes)
40 views28 pages

Intr_CompSci_with_Scratch_Workbook

Uploaded by

Jeffrey Craig
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)
40 views28 pages

Intr_CompSci_with_Scratch_Workbook

Uploaded by

Jeffrey Craig
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/ 28

Key Stage 3 Computing

Introduction to Computer Science


with Scratch
The first section is based on original materials authored by the ITCD Department at California State University,
Monterey Bay and mbrace, the Monterey Bay Regional Academy of Computing Education. For more
information, please visit http://mbrace.csumb.edu/ The second section is based on materials by Professor
David Malan, Harvard University. These materials have been updated to work with Scratch 2.0. You can preview
the project here:

https://scratch.mit.edu/projects/72525766/

For many students, the cryptic syntax of languages like Java tends to get in the way of mastery of relatively simple constructs.
Before tackling a language like Java with its curly braces and semicolons, we can start with Scratch, a "programming language
that lets you create your own animations, games, and interactive art." Although originally developed for children by the
Lifelong Kindergarten research group at the MIT Media Lab, Scratch is useful (and fun) for learning computer science
principles. By representing programs' building blocks with colour-coded blocks (i.e., puzzle pieces), Scratch "lowers the bar" to
programming, allowing for the developmental focus to be centred on problems rather than syntax, to master programmatic
constructs rather than syntax. Syntax, of course, will come later. But, for now, we can focus on programming itself. It just so
happens that programming, for now, will be more like putting together a puzzle than writing Greek.

1|Page Computer Science with Scratch


Key Stage 3 Computing
Getting Started with Scratch

How Scratch Works


In Scratch, we write programs called SCRIPTS. Each SCRIPT controls an object called a SPRITE. The orange cat that you see
when you start up Scratch is an example of a Sprite. Below you can see an example of what a script looks like:

Example Script An Example Sprite

The Scratch Interface


Before we can write a script, we need to know a little bit about the Scratch interface.

The BLOCKS PALATTE contains the blocks that we can use to make our scripts. Each block represents one instruction in our
program.

We build our scripts by dragging blocks from the BLOCKS PALATTE to the SCRIPTS AREA. Above the SCRIPTS AREA is the
CURRENT SPRITE INFO. This is where we can see all of the information about the sprite we are programming.

In the CURRENT SPRITE INFO panel, we also have a set of TABS which allow us to switch between different aspects of our
sprite.

The SPRITE LIST shows all of the sprites that are part of our program.

The STAGE is the background that our program runs on.


2|Page Computer Science with Scratch
Key Stage 3 Computing
Sprites

Each object in our program is a sprite. The default sprite is the orange cat, but you can pick from a library of different
sprites. Let’s get rid of the cat. Right click on the cat and select delete.

Let’s pick a sprite!

click on any sprite - I selected a dinosaur.

3|Page Computer Science with Scratch


Key Stage 3 Computing
Moving Your Sprite
You should now have a new (Dinosaur2?) sprite. The next step is to write a script that will make our sprite move on its own.
We write scripts by dragging blocks from the block palate onto the scripts area. Since we want our sprite to move, we will
use blocks from the motion set (the blue blocks). We’ll first make our sprite move forward 10 steps, so we’ll drag a block
that says move 10 steps onto the scripts area.

If you click on the block, you will see the sprite move on the stage.

If you want the sprite to move further, you can increase the number of steps. Try making the number 100 and see what
happens.

To make the sprite move backwards, you make the number of steps negative by placing a minus sign in front of the number.
Try making the number of steps -100 and see what happens. To make changes to the stage, click on the icon (picture) of the
stage – it looks like a mountain! Change the backdrop by selecting ‘New backdrop’ and then go to the category ‘other’ and
select ‘xy-grid’.

The size of the steps that a sprite makes is based on a coordinate system. Every spot on the stage can be identified by an x,
y coordinate. The x coordinates go from left to write and have values -240 to 240. The y coordinates go from bottom to
top and have values from -180 to 180.

Watch: Programming in Scratch 2.0: The X-Axis and Y-Axis

4|Page Computer Science with Scratch


Key Stage 3 Computing

The direction that your sprite moves in the coordinate system is based on the direction that it is pointing. You can always
see the location of your sprite and its direction by looking at the current sprite info.

Many of the motion blocks change the direction that the sprite is pointing, or move it around the stage’s coordinate system.

Point your sprite in the direction you want it to move. You can specify an absolute direction or
another object on the stage

Move your sprite to a specific location on the stage. You can give coordinates or another
object on the stage. Glide can be used to make the sprite move more slowly.

Change x and y are useful commands to use for moving your sprite around the screen. We will use
these commands in a later lesson.

By combining these blocks, we can make our sprite perform more complex behaviours. Let’s make
our sprite “dance” by combining multiple actions.

5|Page Computer Science with Scratch


Key Stage 3 Computing

To build complex behaviour, snap the blocks together – this is similar to using Lego.

To see what your new dance looks like, click once on the group of blocks and watch what happens on the stage.

When your script runs, the actions on the blocks happen one after the other in the order that you have them connected, this
is called sequential execution. It might be a little hard to see each action individually, because the actions happen very
quickly. We can slow things down a little bit using some of the control blocks. These are on the yellow menu.

Let’s add some yellow wait blocks and see what happens when we run our new script (remember, click
on the group of blocks to run the script).

You should see the sprite ‘wait’ between parts of its new dance. You can change where you put the
wait and you can also change how long you wait by changing the number in the block. [HINT: for a
shorter wait you can use a fraction of a second, 0.5 is half a second].

Every time we click the group of blocks, our sprite will do their dance. We can use another control block to make the sprite
keep dancing.

Drag a forever block into your script. The forever block can go around a group of other
blocks, and we want to arrange it so that it goes around our entire dance script.

Now click on the big group of blocks and see what happens – your sprite should keep
dancing.

This is called LOOPING. The forever block is a loop that repeats a group of blocks over and over again until we tell it to stop.

Inside the loop, the individual actions will still happen sequentially in the order they appear. When the last block finishes,
the loop will start all over with the first one again.

Sometimes, we don’t want to loop forever, but we do want to do a set of actions more than once. Scratch has another kind
of loop that lets us repeat a set of blocks a specific number of times. It is up to you to pick the kind of loop that will work
best in your program. Play around with both the forever loop and the counter loop to see how they work.

To stop a forever block, click the red stop sign above the stage.

Next to the stop sign, there is a green flag button. You can have your program start by
clicking the green flag rather than clicking on the blocks in your script. Once we learn how to
do this, we will use it for most of our scripts.

6|Page Computer Science with Scratch


Key Stage 3 Computing

Drag a ‘when (Green Flag) clicked’ block onto the top of your forever loop. Notice that the green
flag block is shaped differently from the other blocks we have been using. Blocks with a curved
top don’t allow other blocks to attach to the top. These blocks are starting points for our
programs. Later on we’ll learn about some of the other curved blocks as well.
Now, you can click the green flag to start running your script and click the stop sign to stop.

Changing the Background on the Stage

Now that you have a dancing sprite, let’s change the background from the grid pattern. We can change things about the
stage just like we can change our sprite.

Note: You can only modify one sprite or the background at a time.

We can pick a new background from the backdrop library just like we picked a new sprite.

First, click on the Backdrop category

Pick one that you like and click OK to make it the new background on
your stage.

For both sprites and backgrounds, you aren’t limited to the options in the Scratch library. You can also draw your own
sprites, modify existing sprites, or import drawing/photo files from your computer. You will see how to do this in later
lessons.

User Controlled Sprites and Branching

Up until now, all of our sprite’s actions have been controlled by move blocks. However, in a game, we’ll want the user to be
able to move the sprite around using the keyboard. In order to be able to do this, we need to learn a new control block first:

The ‘if –then’ block implements a BRANCHING control structure. Branching lets us choose to do
something, only if certain conditions are met. We do this all of the time in real life. For example, we
might say if it is raining, then take an umbrella outside. This means that when the condition “it is
raining” is true, we should take an umbrella outside with us. We will use branching in programming in
much the same way.
7|Page Computer Science with Scratch
Key Stage 3 Computing

To use an if-then block to control the movement of the sprite, we first have to think about what conditions we want to test.
Let’s use the standard arrow keys as our conditions. This means we want to test the following conditions:

 If the up arrow is pressed, we want the sprite to move up the stage (this is a positive change in the y value of the
sprite’s location)
 If the down arrow is pressed, we want the sprite to move down the stage (this is a negative change in the y value of
the sprite’s location)
 If the left arrow is pressed, we want the sprite to left on the stage (this is a negative change in the x value of the
sprite’s location)
 If the right arrow is pressed, we want the sprite to right on the stage (this is a positive change in the x value of the
sprite’s location)

Let’s look at our if-then block again. Next to the word “if” you’ll see a diamond shaped
opening. This is where the condition will go. The action to do if the condition is true goes
inside the block.
Condition to check goes here

if condition is true, then the action to do goes here (Yes, it’s empty at the moment!)

Where do we get the conditions? There are different types of blocks that can go into the condition spot. All blocks that
can go in a condition spot will be diamond-shaped, just like the opening in the if-then block. One place to get condition
blocks is the sensing menu. Blocks on the sensing menu let us check for events like:

 Mouse events: when the user clicks the mouse (you can also get the location of the mouse arrow on the screen)
 Keyboard events: when the user pushes a button on the keyboard (here we are interested in capturing specific keys –
the arrow keys – but you can capture any key you want)
 When two objects on the screen are touching each other

We will use the sensing menu throughout our game, so it is useful to become familiar with the
available blocks.

The sensing menu contains many blocks that test different conditions. The one that we are
going to use to help control our sprite checks to see if a certain key is pressed. The default key
is the space bar, but we can change it to select any key we want by clicking on the arrow to bring
up a drop down menu.

Here the arrow keys are the ones that we want to use in our condition
since they will tell our sprite which direction to move on the stage.

8|Page Computer Science with Scratch


Key Stage 3 Computing

Now that we know where to find the condition blocks, let’s use if, conditions, and movement blocks to build a user controlled
sprite. What do you think our script will look like?

Here is one example of a script that will work for creating a user-controlled dinosaur. You can write a script that does the
exact same thing that looks very different – in programming there are often lots of
different ways to do the same thing – that is OK. The different ways to approach the
same problem are called algorithms.

Coming up with good algorithms to solve a problem is a big part of what programmers
do.

Costumes and Animation


Now that you have your sprite moving around the screen, let’s improve our program. Let’s have the sprite change the
direction he is facing so that when the user pushes the right arrow or the left arrow, the sprite will face the same direction as
he is moving. We will use costumes to achieve this.

Costumes give you a way to change the way a given sprite looks. You can see the costumes
available to your sprite by clicking on the costumes tab in the current sprite info pane in the
middle of the screen. Right now you can see that our dinosaur had only one costume
available. This costume is named ‘dinosaur2’.

To add more costumes for a sprite, you can copy existing costumes, import new costumes
from the library (if available), make your own costume using drawing tools or a photo, or
edit existing costumes. Here, we want to provide a new costume for our sprite that is
exactly the same as dinosaur2, only we want it to be facing the opposite direction. To do
this we will first copy and then edit the existing costume.

On the costumes tab:


1. Right click ‘dinosaur2’ and select the duplicate option to make a copy of the
dinosaur2 costume
2. Place the new copy of the costume (dinosaur3) onto the stage and right click,
click to select the costume

3. In the top right corner, click the flip left-right button to flip the costume to face
the other direction

Now you should have two costumes for your sprite – one that faces to the left and

9|Page Computer Science with Scratch


Key Stage 3 Computing
one that faces to the right. Look at the names of the costumes to figure out which is which, in my example:
 dinosaur2 faces to the right and
 dinosaur3 faces to the left
You can always rename your costumes (or your sprites) to something that makes more sense. Here I am going to rename
the dinosaur2 costume to be rightDino and the dinosaur3 costume to be leftDino

Now I can modify the script so that the dinosaur always faces the same direction he is moving. I will do
this using the switch to costume block from the looks menu. The looks menu (purple blocks) contains
actions that let us control how the appearance of our sprite changes during a program.

Now, when the right arrow is pressed, our sprite will face to the right and move to the
right

When the left arrow is pressed, the sprite will face to the left and move to the left.

Changing Your Sprite’s Looks

To change the costume of our sprite, we used a block from the looks menu. The looks menu contains blocks that change
things like the colour and size of your sprite.

The looks blocks are the dark purple blocks.

Try out different looks blocks and see what happens. This simple example shows a
change colour effect block. Now the dinosaur will cycle through colour changes
every second.
Other useful blocks are the say and think blocks. These blocks display words in
either thought bubbles or dialogue bubbles.

Using a combination of say and wait blocks, you can create the appearance that multiple sprites are having a conversation.
We will see how this could work when we add more sprites to our game!

10 | P a g e Computer Science with Scratch


Key Stage 3 Computing

Other Actions

Just like we moved our sprite around with the arrow keys, you can have your sprite respond
to other keys with other actions.

See if you can make your sprite jump or fly when another key is pressed!

In this script, I’ve added a new action that will happen when the space bar is pressed?
What do you think this action is? Try it out and see.

Interacting with Other Sprites

So far we have used the if block to let the user control the actions of our sprite, but we can also use the if block to make
different sprites interact with each other. First I need to add a second sprite to my program. Choose a bowl of wotsits – the
American’s call them cheese puffs! Scripts are sprite-specific. Every new sprite will need its own script(s) to tell it how to
behave.

Now I have two sprites – a dinosaur and a bowl of cheesy wotsits.

I can control my dinosaur using the arrow keys on the keyboard using the script I
wrote before.

Let’s try to make the dinosaur eat the cheesy wotsits!

First, we need to move the dinosaur to the cheesy wotsits – that is easy to do
using the arrow keys.

The second step is to write a script so that we know when the dinosaur arrives at
the cheesy wotsits. When the dinosaur gets to the cheesy wotsits, we want to
make them disappear so that it looks like they’ve been eaten.

11 | P a g e Computer Science with Scratch


Key Stage 3 Computing

Let’s write a script for the cheesy wotsits that makes it look like the dinosaur is eating them. First, to detect when the
dinosaur gets to the cheesy wotsits we can use an if block. What should the condition be? Luckily, one of the sensing
blocks will tell us when two sprites are touching each other. Here, we want to pick the condition “if touching Dinosaur2”
since the dinosaur is Dinosaur2.

Check down here to see what each sprite is named. You can change the name of your sprites if you like.

Now we know how to tell when the dinosaur is touching the bowl of cheesy wotsist. What
action do we want to happen when that condition is true? We want the cheesy wotsits to
disappear so that it looks like the dinosaur ate them.

We can use the looks block hide to make the cheesy wotsits disappear. Try to run the program
now. Have the dinosaur eat the cheesy wotsits and see what happens.

When we move the dinosaur over the cheesy wotsits, the wotsits disappear; making it look
like the dinosaur ate them!

When we stop and restart our program, the wotsits are still gone. Why?

Once we hide the wotsits, they don’t reappear until we explicitly tell them to using a show
block. Let’s change our script so that every time we restart the program, the wotsits
reappear. Where should we put the show block?
12 | P a g e Computer Science with Scratch
Key Stage 3 Computing

If we put the show block right after the green flag block, every time we restart our
program, the wotsits will reappear. Try it out to see what happens.

Our game can be improved – the wotsits always reappear at the same spot, let’s change
that. Let’s make it so that every time we restart our game, the wotsits reappear at a
random spot on the screen.

Since show always makes the wotsits reappear at the same spot they were when they were
hidden, we need to move the wotsits to a new spot (a random spot) before we make them
reappear.

To get a random number, we use the pick random block from the Operators menu.
The Operators menu (bright green blocks) contains many mathematical and logical
operations and comparisons that will be very useful in programming.

To use the pick random block, you specify two numbers and then Scratch will randomly
pick a number between those two numbers.

Since we want to randomly position our wotsits on the stage, we want to randomly pick
an x value and a y value to move the wotsits to. What should the range of values be
for randomly picking an x and randomly pick a y?

Remember the coordinate system underlying the stage?


• The x values on the stage range from -240 to 240
• The y values on the stage range from -180 to 180

If we add a go to x: y: block with our pick random blocks included, our cheesy wotsits
will show up at a new location every time we start our program over. Give it a try and
see how it works.

Remember, you still need to stop (click the red stop sign) the program and restart (click the green flag) the program to see the
cheesy wotsits repositioned on the screen.

What would make the game more fun? It would be lots more fun if we didn’t have to restart the program every time we
wanted the wotsits to reappear. How could we change our program so that every time the dinosaur eats the wotsits, they
reappear at a new random location on the screen?

Here is one script that works. To make the wotsits reappear each
time the dinosaur eats them.

Now, let’s add one more thing to our game. Let’s make it harder
for the dinosaur to catch the cheesy wotsits by making the wotsits
move around the screen.

You already know how to do this – you need a script that moves the wotsits around the screen without user interaction like
the very first script we wrote for the dinosaur. You can be creative with how you want the wotsits to move around the
13 | P a g e Computer Science with Scratch
Key Stage 3 Computing
screen – create the behaviour you want by using different combinations of blocks from the motion menu.

Challenge: These are all different ways you can make your wotsits move around the screen:
• Use a combination of random, show and hide to make it look like the wotsits are falling from the sky
• Make the wotsits move back and forth across the stage
• Have the wotsits bounce randomly around the screen

Key questions to think about when creating motion effects are:


• Where should the wotsits be when the game starts? Do you need to have a specific location?
• Keep in mind that at the top of the screen, the y axis value is 180 and at the bottom of the screen the y axis value is -180
• Hint: There is a bounce on edge block that you might find useful!

Once you have the wotsits moving around the screen, you have the start of a fun game!

Variables and Keeping Score

Now that we have the start of a fun game, we want to keep score by letting our dinosaur earn a point for every bowl of
cheesy wotsits that they eat. To do this, we are going to use VARIABLES.

You can think of a variable like a page that can store one number on it. We can read the number off the page or write a new
number on the page, and it will always keep track of the last thing we wrote down.

23
A program can have many variables, each keeping track of a different value. In order
to be able to tell our variables apart, we give each variable a name.
Points:

You always want to give your variable a descriptive name that explains what value it represents. Since we want a variable
that will hold the number of points our dinosaur has, we’ll call our variable “points”

To create a variable in Scratch, go to the Data menu.

Then click on the ‘Make a Variable’.

This will open a new box where we can type the name of our variable button –
call it ‘points’. Then click ok.

After you create a variable, you will see more blocks appear in the variables
menu.

14 | P a g e Computer Science with Scratch


Key Stage 3 Computing
One block allows you to set (or write) a new value to your points variable.

You can also change the value of points (you can add or subtract from the current value).

Show and hide control whether the current value of the variable will be shown on the stage. By default, the value of the
variable is shown.

We want to add one to points every time our dinosaur eats a


bowl of cheesy wotsits. So, we look at our program and
find the spot where a bowl of cheesy wotsits gets eaten.

We insert a block that adds one to points.

Now, every time the dinosaur eats another bowl of cheesy wotsits, our
variable get incremented (one gets added to the current value)

Our variable points always keeps track of how many points the
dinosaur has. You might notice that if you stop the game and then
restart it, the variable points still has the same value. We probably
want points to start over every time we restart our game. Giving a
variable a starting value is called initialization. We can easily
initialize our variable by adding code that sets it back to zero every
time the green flag is clicked.

Look out for the crab!

So far, our dinosaur earns points by eating bowls of cheesy wotsits. Let’s make things a little harder for the dinosaur. Our
dinosaur is afraid of crab’s, so let’s add a crab sprite that moves randomly around the stage. The dinosaur must try to
avoid the crab while he eats cheesy wotsits. If the crab catches the dinosaur, the dinosaur loses two points.

When we want to add a lot to our program all at once, it helps to break it down into smaller steps. Then we can do one
step at a time to make the programming easier. For example:
1. Add a crab sprite
2. Make the crab sprite move randomly around the screen
3. Have the crab sprite recognize when it is touching the dinosaur
4. When the crab touches the dinosaur subtract two points from the dinosaur

There are many different scripts you can write to create this program. Often in programming there is more than one way to
do things. Also, you will notice that many of the steps to program our crab are very similar to steps we took to program the
dinosaur and the wotsits. This is also a characteristic of programming: lots of times you will only have to solve a problem
once and you can reuse the solution. So, you can tell if the dinosaur is touching the crab the same way you checked to see if
the dinosaur was touching the cheesy wotsits!

15 | P a g e Computer Science with Scratch


Key Stage 3 Computing
Here’s some code for a simple crab program.

The code makes the crab move around the screen

This part of the code handles what happens when the crab catches the dinosaur.

Putting it Together: Winning, Losing, and Game Play

Our game starts when we click the green flag and stops when we click the stop sign. To make it more realistic (AND FUN!)
we need to decide how someone wins (or loses) the game. When a player wins the game, we’ll display a new screen that
says “Congratulations – you win!”, when a player loses the game, we will display a different screen that says “I’m sorry, you
lose, game over”.

You can decide how someone should win or lose your game. There are lots of options for winning. You could have winning
being dependent on completion of a specific task, or on gaining a certain number of points. For this example, I am going to
say that a player wins my game if the dinosaur gets 10 points.

We know that the number of points the dinosaur has is held in the point’s variable. We also know that if we want to do
something only when a certain condition is met, that we can use an ‘if then’ block to test that condition. What condition do
we want to test? We want to test if the value of the point’s variable is equal to 10. In order to do this, we need to use a
new block from the Operators (green) menu. In the operators menu, there are blocks that let us test equal to, greater than,
and less than.

To test if the variable points is equal to 10


1. Choose an if-then and forever block
2. From the operators menu, pick the equal to block and drag it to the
condition location in your if block
3. From the Data menu, pick the name of the variable and drag it to one side
of the equal sign
4. Enter the value you want to test (10)

Where you put this block, will depend on what you want to do when the win condition is met. I want the background of my
game to change to display a message that the player won the game. Therefore, I am going to put my block in the script area
for my stage.

Then, click on the scripts tab to build a script for the stage. You can write scripts for the stage just like you would for any
other sprite however your block options are different. For example, there are no motion blocks available for the stage
because it wouldn’t make sense for the stage to move around!

16 | P a g e Computer Science with Scratch


Key Stage 3 Computing

Now we can think about what we want to happen when our win condition is met.

I want to display a new background that says “you win”. That means I first have to create the background that I want.

To create a new background that shows my win message:

1) Switch to the backgrounds tab


2) Make a copy of my existing background
3) Click the edit button on the copy to open the paint editor
4) Inside the paint editor, I add my win message to the new background

Now I have two backgrounds, one that displays the general background for my game, and another that displays the “you win”
message.

17 | P a g e Computer Science with Scratch


Key Stage 3 Computing

You can create whatever win message you like, using the Scratch library of backgrounds, or a background you create yourself.

The last step is to actually have my background change when the dinosaur gets 10 points. I can do this by choosing switch
to background blocks from the looks menu.

Notice that I also set up my background so that when I start the game, I always start with the plain background.

18 | P a g e Computer Science with Scratch


Key Stage 3 Computing
You might have noticed that when the dinosaur gets to 10 points, the background switches to display the win message, but
everything else keeps going (for example, the crab keeps moving). I want to change it so that everything stops when the
player wins i.e. the game ends. I could check if points = 10 for every sprite, but there is a better way to coordinate all of my
sprites. I can broadcast a message telling all of my sprites that the game is over.

To broadcast a message, I pick the broadcast block from the Events menu.

I am going to broadcast my message from my stage since that is where I am checking for the game ending condition.

To send a message, I first need to create it, so I click on the arrow in the broadcast block and choose new to create a new
message. This will pop open a new window.

In the window that opens, I give my new message a name, I am going to call my message “Game Over” because that is what it
will signal to my sprites.

The last step is to tell my sprites what to do when they receive the game over message.

Important: For each sprite, I choose a when I receive block from the Events menu and then a hide block from the looks menu
to have the sprite disappear when the player wins the game.

Since I am hiding all of my sprites when the game ends, I may also need to go back and add a show block when the green flag
is clicked so that the sprite reappears when I restart the game.

19 | P a g e Computer Science with Scratch


Key Stage 3 Computing

Notice that the block for receiving the Game Over message will be the same for all of my sprites. If you have code that you
want to duplicate exactly for multiple sprites, you can drag the block of code over each of the sprites in the sprite list. You
only had to write the code once, and all of your sprites can use it! Reusing code is an important part of programming.

Now I have my winning condition set. How about a losing condition?

Extension activity: I want my player to loose when their score is negative points (less than zero). You can set up the loosing
condition exactly the same way that we set up the winning condition:

1) Create a new background to display a loosing message


2) Use an if-then block to check for a score that is less than zero
3) When the score is less than zero, display my loosing background and broadcast the game over message.

This is just one example of how a player can win or lose a game. Be creative and think of what works best for your game.
For example:
• You could have the win condition be based on capturing a specific item, or completing a specific task.
• You could use two different variables: a points variable to track points and determine if the player wins and a health
variable that decreases and is used to determine if a player loses (when their health variable is zero)

Another option is to use a timer. Let’s say that I only want my player to win if they can score 10 points in less than 100
seconds. I can use Scratch’s built in time to check for this.

The timer is located in the sensing blocks. To use the timer:


1) Click the checkbox next to timer to show the timer value on the stage
2) Choose when to start timing. I want to start my timer when the green flag is clicked (as soon as the game starts).
Since I am handling winning and losing in the scripts for my stage, I will control the timer there as well.
3) Drag a reset timer block to where you want to start the timer – I’ve used the stage

The timer will now be displayed on the stage.


20 | P a g e Computer Science with Scratch
Key Stage 3 Computing

Have you clicked the checkbox next to the timer to make it show up?
Add a reset timer block where you want to start the timer

If you play the game now, you will see that the timer will count the seconds that the game is being played. Now we need to
add a check based on the timer. We are updating our win conditions like this:
• The player WINS if: They score 10 points AND they have been playing for less than 100 seconds
• The player LOSES if: They have negative points OR they have been playing for more than 100 seconds – assuming that you
did the extension activity!

AND and OR are called Boolean expressions in programming and are used a lot when we have more than one ‘thing’ that we
want to check.
• Use an AND when you have multiple conditions and ALL of them must be true
o This is like the case for winning since it must be true that the user has 10 points and it must also be true that they
have been playing for less than 100 seconds
• Use an OR when you have multiple conditions and only ONE of them must be true
o This is like the case for losing since either the player must have a negative points value or they must have been
playing for more than 100 seconds, but both do not have to be true.

and and or blocks are available from the operators menu. Both have two slots for the conditions involved.

Now that we know about and and or blocks, we can update our win and loose conditions for our player.

You can use and and or blocks throughout your program any time you want to check multiple conditions!

Extension activity: You can also use the same blocks we have used to create more challenges for your games.
For example, you can:
• Create multiple levels – advancing to a new level can be dependent on reaching a certain number of points, or any other
condition
• Make your game more challenging as players accumulate more points, for example, I could make the crab move faster once
the dinosaur has 5 points
• Be creative! There is no limit to the kinds of games you can create using Scratch!

21 | P a g e Computer Science with Scratch


Key Stage 3 Computing

Adding Sound to Your Program

You can also add sounds to your program. The sound blocks are bright purple.

To play a sound, you must first add it to your sprite.

There are two ways to add a sound to your sprite. You can either record a sound using a microphone, or you can import a
sound. If you import a sound, you can use the built in sounds from the scratch library, or upload your own sound/music file.

All of the options for sounds are on the sounds tab in the current sprite pane.

22 | P a g e Computer Science with Scratch


Key Stage 3 Computing

Scratch Review
Here are some of the programming basics that we learned in Scratch that apply to programming in general.

 Execution order
o Sequential
o Branching
o Looping
 Variables
o Creating variables
o Giving variables initial values
o Changing the value of a variable
 Using random numbers
 Logical expressions
o And
o Or
o Checking equality
 Using timers
 Capturing user events (e.g. key presses)
 Using libraries of commands
 Importing/using media files (sound, pictures, etc)
 TESTING and DEBUGGING!

Statements
In programming, a statement is simply a directive that tells the computer to do something. Think of it as a command or an
instruction. In Scratch, any block whose label reads like a command is a statement.

One such block instructs a sprite to say something:

Another such block instructs a sprite to go to some location:

Sometimes, you only want a statement to be executed under certain conditions. Such conditions are defined in terms of
Boolean expressions, to which we turn our attention next.

23 | P a g e Computer Science with Scratch


Key Stage 3 Computing
Boolean Expressions
In programming, a Boolean expression is an expression that is either true or false. In Scratch, any block shaped like an
elongated diamond is a Boolean expression.

One such block is:

After all, it is either true that the mouse button is down or it is false.

Another such block is:

After all, it is either true that some number is less than another number or it is false. With Boolean expressions can we
construct conditions, to which we turn our attention next.

Conditions
In programming, a condition is something that must be true in order for something to happen. A condition is thus said to
"evaluate to true" or "evaluate to false." In Scratch, any block whose label says "if," "when," or "until" is a sort of conditional
construct.

One such block is:

The construct above is generally known as an "if – then construct." With it can we instruct a sprite to say hello only if, say, the
user has depressed the mouse button:

A related construct is the "if-then-else construct":

With the above construct can we instruct a sprite to say hello or goodbye, depending on whether the user has depressed the
mouse button:

24 | P a g e Computer Science with Scratch


Key Stage 3 Computing

Realize that these constructs can be nested to allow, for example, for three different conditions:

The above construct could be called an "if-else if-else construct".

Another conditional block is:

Yet another such block is:

Sometimes, you want one or more statements to be executed multiple times in a row. To implement this behaviour, we turn
our attention to loops.

Loops
In programming, a loop can induce multiple executions of statements. In Scratch, any block whose label begins with "forever"
or "repeat" is a looping construct.

One such block is:

This construct allows us, for instance, to instruct a sprite to meow every other second:

25 | P a g e Computer Science with Scratch


Key Stage 3 Computing

Another block allows you to loop a specific number of times:

And another block allows you to loop until some condition is true:

Sometimes, you want execute some statement multiple times, each time varying your behaviour ever so slightly. We thus
turn our attention to variables.

Variables
In programming, a variable is a placeholder for some value, much like x and y are popular variables in algebra. In Scratch,
variables are represented with blocks shaped like elongated circles, uniquely labelled by you. Variables, generally speaking,
can be local or global. In Scratch, a local variable can be used by just one sprite; a global variable can be used by all of your
sprites.

Variables allow us, for instance, to instruct a sprite to count up from 1:

A variable that only takes on a value of true (i.e., 1) or false (i.e., 0), incidentally, is called a Boolean variable.

With statements, Boolean expressions, conditions, loops, and variables now under your belt as building blocks, we can now
explore two higher-level programming constructs, starting with threads.

26 | P a g e Computer Science with Scratch


Key Stage 3 Computing
Threads
In programming, a thread is like a mini-program within a program that can execute at the same time as other threads. A
program with multiple threads, then, can do multiple things at once. In Scratch, any block whose label begins with "when"
essentially demarks the start of a thread; think of what Scratch calls a "script" as a thread. (Technically, scripts run in threads,
but never mind that.)

One such block is:

As the above block's label suggests, this thread begins to execute when the user click's Scratch's green flag. A program with
two such blocks thus has two "threads of execution," both of which start simultaneously when the user clicks Scratch's green
flag.

It's often helpful to use separate threads for conceptually distinct tasks. For instance, you might want to keep track of
whether the user ever presses some key during a program's execution in order to, say, toggle sound on and off:

Notice how, in the above, the left-hand thread handles meowing, if appropriate, whereas the right-hand thread constantly
checks and remembers whether the user has muted or unmuted sound by pressing the space bar.

Related to threads are events, to which we turn our attention next.

Events
In programming, multiple threads can communicate with each other by signalling events and handling events. An event, then,
is like a message from one thread to another. In Scratch, blocks whose labels begin with "broadcast" signal events whereas
blocks whose labels begin with "when" handle events, the latter of which, recall, effectively represent threads themselves.

A block that signals an event is:

A block that handles an event is:

27 | P a g e Computer Science with Scratch


Key Stage 3 Computing

Not only can events be signalled by blocks, they can also be signalled by a user's actions. Clicking Scratch's green flag, for
instance, effectively signals an event that is handled by:

In Scratch, not only do events enable threads to communicate, they also allow sprites to communicate with each other. For
instance, two sprites might want to play Marco Polo with each other, with one sprite's behaviour defined by the leftmost
thread below and the other sprite's behaviour defined by the rightmost thread below:

Out of statements, Boolean expressions, conditions, loops, variables, threads, and events can you construct interesting (and
fun) programs. In fact, let's explore the inner workings of what, on first glance, appears to be a very complex program but,
ultimately, is just an application of these building blocks.

These are among the principles of computing that are core to understanding programming and computer science! The next
steps might be programming using python or App Inventor.

Oscartime

Professor David Malan, Harvard University, wrote a Scratch game based on Sesame street. Play it:

https://scratch.mit.edu/projects/37415/

There are lots of similarities between David’s game and the one that we built. You can find out how David built ‘oscartime’ here:

http://cs.harvard.edu/malan/scratch/oscartime-instructions.php

28 | P a g e Computer Science with Scratch

You might also like