Oxford University Press: Visual Programming Through Scratch
Oxford University Press: Visual Programming Through Scratch
s
Programming is the art of writing instructions (or implementing algorithms) that tells the computer
es
what to do. Scratch is a simple visual programming language that provides an ideal learning
Pr
environment for writing programs. It was developed in the USA, Massachusetts Institute of
Technology. It has color coded blocks of code that snap together like a jigsaw. Scratch not only
ity
eliminates typing errors that are common in text-based programming languages but also makes
rs
development of games, animations and interactive stories very simple. Seeing the utility of this
ve
programming language, many schools have started teaching Scratch to class 3-4 students (8 to 14
ni
years).
U
Scratch can be freely downloaded from the Internet for any operating system.
d
or
Scratch is a great tool for learning programming as it supports almost every concept of modern
programming languages. To use Scratch, first download from the internet and install it. When you
open the Scratch program, you will get the window as shown below.
the programmer. A program can have more than one sprite. The default sprite is a cat, which the
ni
user can change by either creating a new one or by selecting one from the sprites library. The sprite
U
is initially located in the center of the stage (the white area in the Scratch window where the sprite
moves), but it can be dragged to any location.
d
or
The background of the stage known as Backdrop is by default white in color. A program can have
xf
multiple backdrops that can be chosen from the Select Backdrop options.
O
The Block palette has ten different types of blocks that are color-
coded. When you select a block category, its corresponding
blocks are then displayed under the blocks palette. These blocks
are the instructions or commands that fit together like puzzle’s
Change Sprite pieces. Table describes different blocks in the Block Palette.
A program also known as a script in Scratch is created using blocks. Note that blocks in a particular
category are colored with exactly the same color as that of its block in the Block palette. For
The blocks in the script are executed sequentially (one after the other).
The resize toolbar has options to shrink, grow, delete and duplicate
the sprite.
BLOCK USE
Motion Contains blocks for the movement of Sprite such as move up, down, left, right,
turn, set position etc.
Event Defines the motion of Sprite when some event is occurred like Green flag is
s
es
clicked, a key is pressed, etc.
Pr
Control Have blocks to repeat certain actions or to wait for certain time, or to perform the
next action only if a condition is met.
Looks ity
Have blocks to control the appearance of the sprite or to make the sprite to say,
rs
think, show, hide, or change the costume and backdrops.
ve
Sensing Has blocks to create animation by sensing actions. It also helps to make the
program responsive
ni
Sound Blocks to play different musical nodes, stop the sound and control volume
U
Operators Consists of blocks for various arithmetic and logical operators for handling
d
scripts.
or
Pen Allows the sprite to draw as it moves over the stage. For this it has blocks like
xf
Data Contain blocks to hold numerical (in digits) and string (textual or alphabet)
values.
More Has options to create your own blocks.
Before implementing the programming concepts, let us first have a look at different key concepts
that we will be frequently using in our programs.
Iteration: Iteration means repeating certain instructions. In Scratch, this is done by using the repeat,
repeat until or forever blocks in the Control Block.
Conditions: Scratch has if and if-else blocks in the Control block that checks for a condition. If the
condition is met, instructions are performed else ignored.
Variable: A variable is a placeholder that stores specific information. For example, while playing
games, there are variables that store score and time lapsed.
s
List: List is a tool that can be used to store multiple pieces of information at once. For example,
es
while playing game, we have a list of scores that stores our scores in last few days.
Pr
Event handling: Scratch has Events block that allow the sprite to respond to events triggered either
ity
by the user or other parts of the program. For example, when the green flag is clicked the sprite
rs
can be made to move or draw a shape.
ve
Thread: A thread is just a piece of code within the program that cannot run on its own. When two
ni
threads execute at the same time, it is called parallel execution. For example, if we have two sprites
U
Synchronization: The broadcast block in the Events block can be used to synchronize the
processing especially when we have two or more sprites or two or more threads working
xf
simultaneously.
O
Input: To design interactive programs, the ask and wait block in the Sensing Block prompts users
to type the information.
Boolean logic: It is a form of algebra in which all expressions produce only one of the two values-
True or False. The and, or, not blocks in the Operators block are examples of Boolean logic.
In the script given below, the sprite was dragged to move to the bottom left corner of the stage. By
default, it is present at the center of the stage. The backdrop of the stage was changed by selecting
“blue sky” from the backdrop library. Now, after doing these two things, drag the blocks in the
script area and change some of the values as shown below. (Values are changed by double clicking
an overwriting them).
s
es
Pr
ity
rs
ve
ni
U
d
or
xf
O
Degrees Direction
-360, 0, 360 Up
s
es
Negative values just mean to rotate the sprite anti- clockwise rather than clockwise. So -90 turns
the sprite left and -180 turns it down (just like +180). Table summarizes some common values and
Pr
their directions:
ity
When the direction of the sprite is greater than 360°, it is same as the direction minus 360°. For example,
rs
400° is equivalent to 400-360=40°, and 720° = 360° = 0°.
ve
ni
Before writing the code for the sprite, delete the cat sprite by right clicking on it and selecting
U
Delete. Add a new sprite from the sprite library. Change the backdrop by selecting one from the
library.
d
or
xf
O
To see the blocks of a sprite, select the sprite from the sprite list. If you have multiple sprites in your script,
then select that particular sprite from the list of sprites (next to New Backdrop) to see its blocks.
U
d
or
In computer programming, iteration means repeating certain instructions. Each time the code is executed
O
(repeated), it is called one iteration. At times, programmer may not want to repeat code a fixed (pre-
determined) number of times. Instead the code may be repeated until or while some condition is TRUE.
In such a situation, “Repeat Until” block in the Control block is used.
Similarly, another block REPEAT FOREVER is used to repeat certain instructions infinite number of times
(forever i.e; as long as program runs). However, as a programmer, we must avoid such loops whenever
possible. A Repeat Forever loop if used must have an “if” statement to “break” out of the loop.
Consider the script given below in which the dancer takes 10 jumps.
CHANGING COSTUMES
ni
The switch costume block in the Looks Block is used to change the costume of the sprite. The next
U
While switch to costumes is used to switch to a particular costume (by selecting its name from the
or
menu), next costume changes to its next costume. When the script is run, next costume chooses
xf
Each sprite can have a number of costumes, which are different pictures of it. By using costumes
that look fairly similar, the programmer can create the illusion of animation by switching between
them. For example, the cat sprite comes with two costumes, and by switching between them, it
seems as if the cat is running.
You can see the costumes for your sprite by clicking the Costumes tab at the top of the Scripts
Area.
ADDING BACKDROPS
ni
U
As we have been doing, you can change the backdrop by anyone of the following ways.
d
or
Out of these many users prefer to use the last option. They
take selfies with their webcam and using them as
backdrops.
To add a backdrop using library, click on Choose from Library to view the backdrops library. Now,
select an image and click the OK button.
s
es
Pr
ity
rs
ve
ni
INPUT / OUTPUT
The Ask () and Wait block in the Sensing Block when used prompts the user to enter a value in a
rectangular box displayed at the bottom of the screen. The user’s response is stored in
the Answer block (in the Sensing block). The Answer block automatically updates to most recent
input.
10
1. Chatbots that receive information from the user and communicates with him/her.
s
es
Pr
ity
rs
3. Give instructions to the sprite for example, to move to x steps, where the value of x is entered
ve
by the user.
ni
U
Let’s write a script that prompts the user to enter a password, if the answer matches with the value
of passwd (a variable whose value is pre-defined) then access is granted else denied.
d
or
xf
Note to make a variable select the Data Block and click on Make a variable block. Enter a variable
O
name and click on OK. Now you can set the value of the variable by using the set variable_name
to block.
11
Variables in Scratch consist of a series of symbols to represent a value. A variable can store textual
ni
values, numbers, or Boolean values. For example, a variable may store values like: Hello, 123,
U
As discussed earlier, to create a variable, click on Make a Variable block in the Data Block. Specify
or
a name for the variable. You can make this variable local or global. A local variable is available
xf
only for one particular sprite but a global variable is for all sprites in the script. Click on OK. Now,
O
either prompt user to enter its value or set its value using the set variable to block.
To delete or rename a variable, just right click on the variable and select delete or rename.
Operators are symbols that process values of variables. In Scratch, these operators are available in
the Operators Block. There are different sets of operators, each of which has its own utility.
Comparison operators also known as relational operators are used to compare the values of two
variables or expressions. In Scratch, there are three relational operators- greater than (>), less than
(<) and equal to (=).
12
Logical operators help programmers to combine two or more relational operators to produce a
single true/false result. There are three logic operators: And, Or, Not
The and operator takes two expressions and returns True if both of them evaluate to True and False
otherwise.
The or operator takes two expressions and returns True if either one of them or both of them
evaluate to True and False only if both are False.
The not operator takes only one expression and negates its value. That is, if the expression is True,
s
es
it returns False and vice versa.
Pr
ity
rs
ve
ni
U
d
or
xf
O
Note that in the above script, we have used a combination of blocks in the if statement and set
block. To create these statements first create the inner blocks and then drop them on the outer or
the main block. For example, in the second if statement, first drag the operator and block from the
Operators block. Fill it by dropping the variable sum from the Data block and then drop it on the
if block (from the Control Block).
13
A sound is something that when played in a Scratch program, can be heard by the user. As a
programmer, you can either import a sound file, record a sound or use those available in the
Scratch's built-in sound library, or recording. Sounds are played by using Sound Block which
control a sound's volume, tempo, etc. Note that in Scratch, all sounds are played in mono. Some
blocks that are frequently used to add sound include:
▪ Play Sound () to begin the sound and execute the rest of the sprite’s script, without waiting for
the sound to finish playing.
s
▪ Play Sound () Until Done to play a sound, wait for it to finish playing, and continue with the
es
sprite’s script.
Pr
▪ Stop All Sounds to cancel all sounds currently playing.
▪ Set Volume to ()% to set a sprite's volume.
▪
ity
Change Volume by () to change a sprite's volume by a given amount.
rs
ve
ni
U
d
or
xf
O
SPRITE COMMUNICATION
14
Therefore, in our Scratch scripts, we can make two or more sprites communicate with each other.
For this, there are three blocks in the Event Block.
s
es
broadcast block lets scripts send broadcasts without any waits in its script. You can create a new
message with every communication.
Pr
ity
Broadcast () and Wait block in the Events block sends a
rs
broadcast throughout the whole Scratch project. Any sprite that has been scripted to receive that
ve
message using the When I Receive () block will get activated. This broadcast block lets scripts
send broadcasts and wait until all scripts activated by the broadcast end.
ni
U
However, remember that, a sprite can send as well as receive the same broadcast message. The
xf
broadcast message can be received by all sprites. These messages connect different events and run
O
two scripts simultaneously (creating threads of execution). This makes working with multiple
scripts convenient.
15
CREATING STORIES
You can design projects in Scratch that tells a story. These stories can be interactive stories, where
users make their own story or be talk shows where animated characters talk to each other. Most of
16
In interactive stories, a user is given a variety of options. When he selects a particular story, it will
progress. This gives user the ability to 'make their own story.' These stories take a little more time
than other stories as a dialogue tree has to be constructed and every possible scenario needs to be
programmed. When an interactive story prompts users to give suggestions for what will happen
next, it is called collaborative story.
Talk show stories use the broadcast block to make two or more sprites talk to each other using text
or programmed speech bubbles.
s
Scratch also allow users to create books or short stories in which the user types a story for other
es
user's to read. The text can be augmented with the use of music or sound effects. Generally, such
Pr
a story is typed up in backdrop(s) or sprite(s) and a script is used to change the pages with the
ity
arrow keys. We have already created a story using the broadcast block in the previous section.
rs
GLOSSARY
ve
Programming: The art of writing instructions (or implementing algorithms) that tells the
ni
Sprite: An object or character that will perform certain actions based on the instructions given by
d
the programmer.
or
Stage: The white area in the Scratch window where the sprite moves.
xf
O
Script area: Area in which the blocks are arranged to form a script
List: List is a tool that can be used to store multiple pieces of information at once.
17
Scratch is a simple visual programming language that provides an ideal learning environment for
writing programs.
A program can have more than one sprite. The default sprite is a cat, which the user can change
by either creating a new one or by selecting one from the sprites library.
The Block palette has ten different types of blocks that are color-coded. When you select a block
category, its corresponding blocks are then displayed under the blocks palette.
Comparison operators are also known as relational operators are used to compare the values of
two variables or expressions.
s
es
Pr
REVIEW QUESTIONS
1. Define programming.
7. Give a brief note on programming concepts that can be easily implemented through Scratch.
xf
10. What are different blocks used to add sound in a scratch program?
2. _______ is an object or character that will perform certain actions based on the instructions
given by the programmer. Ans. sprite
18
6. The _________ block in the Sensing Block when used prompts the user to enter a value.
s
es
single true/false result. Ans. Logical operators
Pr
10. Scratch has _________ blocks to signals an event and when I receive block handle that event.
Ans. broadcast
ity
rs
ve
PROGRAMMING EXERCISE
ni
5. Write a script to prompt user to enter his age and then make the sprite say whether the user is
eligible to vote or not.
9. Write a script that shows sprites playing in a garden. Use next costume.
19
4. Backdrop is the white area in the Scratch window where the sprite moves. Ans. False
6. The blocks in the script are executed sequentially (one after the other). Ans. True
7. Sensing block is used to control the appearance of the sprite or to make the sprite to say. Ans.
False
s
es
8. Motion block allows the sprite to draw as it moves over the stage. Ans. False
Pr
9. Costumes are different dresses of sprite. Ans. False
10. A sprite can send as well as receive the same broadcast message.
ity Ans. True
rs
ve
Ans. d
or
a. 7 b. 9 c. 10 d. 8 Ans. c
20
9. When a story prompts users to give suggestions for what will happen next, it is called a
________ story.
s
es
A. Book b. collaborative c. fiction d. talk show Ans. a
Pr
Explain the significance of the following blocks of code
ity
rs
ve
ni
U
d
or
xf
O
21
s
es
Pr
ity
rs
ve
ni
U
d
or
xf
O
22