0% found this document useful (0 votes)
344 views22 pages

Oxford University Press: Visual Programming Through Scratch

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)
344 views22 pages

Oxford University Press: Visual Programming Through Scratch

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/ 22

VISUAL PROGRAMMING THROUGH SCRATCH

Writing programs is key to development of solutions using computers. Therefore, learning


programming skills is necessary for anyone who wants to enter the field of computing. In this
chapter, we will learn Scratch programming to understand the concept of programming that are
common to every computer programming language.

INTRODUCTION TO PROGRAMMING CONCEPTS WITH 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 Programming Environment


xf
O

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.

© Oxford University Press. All rights reserved.


s
es
Pr
ity
rs
Sprite is an object or character that will perform certain actions based on the instructions given by
ve

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

© Oxford University Press. All rights reserved.


example, as we see in the figure, Motion in Block Palette and its corresponding blocks are blue in
color. These blocks are arranged in the script area to form a script.

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

pen up, down, change color and size of pen.


O

Data Contain blocks to hold numerical (in digits) and string (textual or alphabet)
values.
More Has options to create your own blocks.

IMPLEMENTING PROGRAMMING CONCEPTS

Before implementing the programming concepts, let us first have a look at different key concepts
that we will be frequently using in our programs.

© Oxford University Press. All rights reserved.


Sequences: In a sequential code, one instruction (or block) is executed after the other.

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

talking to each other then we can program it as two parallel threads.


d
or

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.

Sprite Looks and Motion

© Oxford University Press. All rights reserved.


We have seen that Motion blocks have blocks to control the movement of sprites. This is done by
setting the x and y position and direction of the sprite. The Looks blocks control the appearance
of sprites and the stage.

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

© Oxford University Press. All rights reserved.


Angles and Directions
-270, 90 Right
The direction of a sprite controls how much it is rotated. It is measured
in degrees (°) and varies from -90° (pointing left) to 180° (pointing
-180, 180 Down
down). The default value for the direction is 90° (pointing right).
Scratch also allow programmers to use angle measurement. The angle
-90, 270 Left values can even be negative. For example, an angle value "0" means
"up" and then every degree added corresponds to turning the sprite
one degree clockwise. Therefore, a direction of 90 means a sprite turns 90 degrees (a quarter turn)
after pointing straight up and will thus point right.

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

© Oxford University Press. All rights reserved.


s
es
Pr
ity
rs
ve
ni

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

Iteration and Variation


xf

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.

© Oxford University Press. All rights reserved.


s
es
Pr
ity
rs
ve

CHANGING COSTUMES
ni

The switch costume block in the Looks Block is used to change the costume of the sprite. The next
U

costume block switches to the next costume in the list.


d

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

the next, then first again, so on and so forth.


O

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.

© Oxford University Press. All rights reserved.


s
es
Pr
ity
rs
ve

ADDING BACKDROPS
ni
U

As we have been doing, you can change the backdrop by anyone of the following ways.
d
or

• Choose backdrop from library



xf

Paint new backdrop



O

Upload backdrop from file


• New backdrop from camera

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.

© Oxford University Press. All rights reserved.


Here you can add or delete backdrops and edit existing ones. We're going to delete the unwanted
one and then add two new ones based on the one we've just added. Once added, a backdrop can
be deleted and copied. Just select the backdrop in the backdrops pane, and right-click on it. In the
shortcut menu, click duplicate or delete.

s
es
Pr
ity
rs
ve
ni

Backdrops can even be edited by:


U
d

• drawing on them, using the brush tool, lines or shapes


or

• adding text to them, with a variety of fonts available


xf

• erasing parts of them


O

• selecting and duplicating specific areas

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

© Oxford University Press. All rights reserved.


Therefore, the ask and wait block and the answer block are used to create interactive programs in
Scratch. Some common uses for the Ask () and Wait block:

1. Chatbots that receive information from the user and communicates with him/her.

2. Setting preferences like changing the sprite’s coordinates, color, etc.

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

© Oxford University Press. All rights reserved.


s
es
Pr
ity
rs
VARIABLES AND OPERATORS
ve

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

3.14, -321, True, False, etc.


d

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

© Oxford University Press. All rights reserved.


These operators compare the relationship between two values or variables and then result in either
True or False. If the result is True, the next line of code will be run else not.

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

© Oxford University Press. All rights reserved.


Working with Sounds

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

© Oxford University Press. All rights reserved.


In any programming language that supports multi-threading, communication within threads is
ensured by signaling events and handling them. Correspondingly, in Scratch, an event is like a
message that is sent from one thread to another. To deal with communication, Scratch has
broadcast blocks to signals an event and when I receive block handle that event.

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.

Broadcast Message block in the Events Block is used to send a broadcast


message throughout the whole Scratch program. Any sprite that has been
scripted to receive that message using the When I Receive () block will get activated. This

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

When I Receive () block in the Events block is used to activate a script


d

when it receives the specified broadcast message.


or

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

© Oxford University Press. All rights reserved.


s
es
Pr
ity
rs
ve
ni
U
d
or
xf
O

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

© Oxford University Press. All rights reserved.


the stories especially those which are fixed (and not interactive) are fan fiction as they use
characters and settings from a work of fiction.

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

computer what to do.


U

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

Backdrop: The background of the stage known as Backdrop.

Script: A program written in Scratch.

Script area: Area in which the blocks are arranged to form a script

Variable: A placeholder that stores specific information.

List: List is a tool that can be used to store multiple pieces of information at once.

Costumes: Different pictures of sprite.

17

© Oxford University Press. All rights reserved.


SUMMARY

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.

2. Why is Scratch an ideal programming for beginners?


ity
rs
3. List at least five advantages of using Scratch.s
ve

4. Differentiate between stage and backdrop.


ni

5. Explain the significance of Block Palette.


U
d

6. Write a short note on different blocks and their usage.


or

7. Give a brief note on programming concepts that can be easily implemented through Scratch.
xf

8. List different ways in which backdrops can be edited.


O

9. Explain the significance of Answer and wait block.

10. What are different blocks used to add sound in a scratch program?

FILL IN THE BLANKS

1. Scratch has _________ blocks of code. Ans. color coded

2. _______ is an object or character that will perform certain actions based on the instructions
given by the programmer. Ans. sprite

18

© Oxford University Press. All rights reserved.


3. The default sprite is a _________. Ans. cat

4. ________ means repeating certain instructions. Ans. Iteration

5. Negative values rotate the sprite _________. Ans. anti-clockwise

6. The _________ block in the Sensing Block when used prompts the user to enter a value.

Ans. Ask () and Wait

7. The user’s response is stored in the _______ block. Ans. Answer

8. A variable in Scratch is created using the ________ block. Ans. Data

9. ____________ help programmers to combine two or more relational operators to produce a

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

1. Write a script to depict the conversation between two friends.


U

2. Write a script in which a sprite grows in size while moving.


d

3. Write a script to draw a pentagon.


or

4. Write a script to add two random numbers.


xf
O

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.

6. Write a script to show bouncing of a ball.

7. Write a script to show fishes in a pond. Use the sound block.

8. Write a script that depicts a birthday party.

9. Write a script that shows sprites playing in a garden. Use next costume.

10. Write a script to show a dancer performing on a stage.

STATE TRUE OR FALSE

19

© Oxford University Press. All rights reserved.


1. Scratch is vulnerable to typing errors. Ans. False

2. Scratch is a proprietary software. Ans. False

3. A program can have at the most two sprites. Ans. False

4. Backdrop is the white area in the Scratch window where the sprite moves. Ans. False

5. A program can have multiple backdrops. Ans. True

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

MULTIPLE CHOICE QUESTIONS


ni

1. It is not possible to create ________ through Scratch.


U

a. Animations b. stories c. programs d. none of these


d

Ans. d
or

2. The sprite is initially located in the _______ of the stage.


xf

a. bottom-right corner b. bottom-left c. center d. top left Ans. c


O

3. The Block Palette has ____ number of blocks.

a. 7 b. 9 c. 10 d. 8 Ans. c

4. A program written in Scratch is called ________.

a. Sprite b. script c. stage d. backdrop Ans. b

5. The resize toolbar cannot be used to __________ the sprite.


a. duplicate b. grow c. delete d. none of these Ans. d
6. Which block has blocks to repeat certain actions or to wait for certain time?

20

© Oxford University Press. All rights reserved.


a. Motion b. Event c. Control d. Looks Ans. c
7. ________ is a tool that can be used to store multiple pieces of information at once.
a. Variable b. list c. block d. thread Ans. b
8. Which operator is also known as relational operator?
a. Comparison b. logical c. arithmetic d. binary Ans. a

9. When a story prompts users to give suggestions for what will happen next, it is called a
________ story.

a. Interactive b. collaborative c. fiction d. talk show Ans. b

10. __________ story is typed up in backdrop(s) or sprite(s).

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

© Oxford University Press. All rights reserved.


.

s
es
Pr
ity
rs
ve
ni
U
d
or
xf
O

22

© Oxford University Press. All rights reserved.

You might also like