Programming With Scratch
Programming With Scratch
Table of Contents
Lessons Pg.No
1. Introduction to Scratch …………………… 1
Introduction
A computer is a electronic machine. It is a
wonderful machine. It does many types of work.
But a computer cannot do anything on its own.
We have to instruct a computer to do
something by giving commands.
What is a Program?
A set of instructions or commands that makes the computer to do the work in
step-by-step manner to produce the results we want is called program.
Commands given to the computer must be in proper order and with correct
spellings.
Why Programming?
Programming is the technique of writing instructions to the computer to achieve a
result.
By learning programming:
1
We can learn the logical and analytical skill.
It greatly expands our creativity.
We can learn important problem-solving and design strategies.
With Scratch, we can create interactive stories, games, graphic art work,
experiments, effective education tools such as math quiz, physics simulation,
educational videos and other multimedia projects and animation programs.
2
Exercise 1
1. What is a program?
2. Define Programming language.
3. What is Scratch?
4. When and by whom was the Scratch programming language developed?
5. What can we do with Scratch?
6. What are the benefits of learning Scratch?
7. What is Scratch’s slogan?
A B
1. FORTRAN a) Assembly language
2. Scrach b) High level language
3. 0 and 1 c) Visual interface
4. Mnemonics d) machine language
3
Lesson 2: First look at Scratch
Scratch is a visual programming language with graphical interface.
Getting Started
Scratch is started by
4
Scratch anatomy
Scratch window is called as Integrated Development Environment (IDE). It
allow us to design, program, and run our projects.
Title bar :
At the top of the IDE is the Title bar. Iy shows the name of the application to the
left and the Control buttons (Minimize, Maximize and Close) to the right.
Menu Bar :
Below the Title Bar is the Menu Bar. It contains four menus – File, Edit, Share
and Help. Also it contain buttons to set the language, save the project and share
the project on web,
To the right of the Menu Bar is the Sprite Tool Bar. It allows deleting, duplicating,
growing or shrinking a sprite.
To the right of the Sprite Tool Bar is the Stage Tool Bar. It allows to change stage
size or open presentation mode.
Each type of block is color-coded so that we can easily identify them in our
scripts.
6
Current Sprite Info
Current Sprite Info shows a sprite's name, its position on stage, direction, etc.
At the top of the Script area are three tabs which shows the properties or
components of a sprite or a stage.
Script Area
7
The blocks and their order are very important, as they determine how sprites
interact with each other.
Stage
The Stage is where you see the actions of stories, games, and animations.
8
The Stage is 480 units wide and 360 units
tall.
It is divided into an x-y grid.
Like sprites, the stage can have its own scripts and sound effects.
To run a script or to stop a script execution the green and red button
at the top of the stage are used.
9
Scratch runs the projects on the stage by default.
It can be opened by clicking the presentation mode button, located above of the
stage, in the tool bar.
The default sprite is a cat named as Sprite1. There can be one or more sprites
in a project.
Sprites can be moved around and made to interact with one another.
Each sprite shows different looks. Look of the sprite changes with its action.
10
Sprite List
To see and edit a sprite's scripts, costumes, and sounds, click on the sprite's
thumbnail in the Sprite List or double-click on the sprite itself on the Stage.
The selected sprite is highlighted and outlined in blue in the Sprite List.
.
Just like sprite, the Stage can have different appearance
New sprites can be added to the program with the New Sprite buttons located
below the stage.
11
c) The Get Surprise Sprite button randomly retrieves one of Scratch’s ready-
made sprites and adds it to your application project.
Exercise 2
Lab work 1
2. Create a Scratch project that makes the kitten meow and say ‘‘Hello
World!’’ when clicked.
1. Start Scratch
2. Change the name of the sprite from ‘Sprite1’ to
‘Cat’ in the Current Sprite Info window and
press Enter key.
3. Click on the Looks button located at the top of the code
block area.
4. Click on the code block labeled ‘say Hello! for 2 secs’
and drag and drop it onto the script area
5. Replace the text ‘‘Hello!’’ with ‘‘Hello World!’’ and press Enter key.
6. To test this, click on the script. You can see the cat displaying the text
“Hello World!’’ in a bubble caption on the stage.
7. To run the program click on the Green Flag Control button
located at the top of the code block area.
13
Lesson 3: Blocks Palette
Every Scratch project contains sprites, costumes, blocks, scripts, and a stage.
To bring the backgrounds and sprites to life, we create scripts.
Scripts are created by dragging and dropping code blocks from the blocks palette
to the scripts area. A stack of connected blocks makes a script which makes the
sprite to perform an action.
Blocks are commands in puzzle-piece shapes that are used to create scripts.
The blocks can be connected to each other as they have a notch at the top and a
projection at the bottom. Each block represents an instruction that tells the sprite
what to do or how to interact with other sprites.
The blocks are classified into six categories based on their shape. They are,
Stack blocks
Hat blocks
Reporter blocks
C blocks
Cap blocks
Boolean blocks
Stack Blocks :
Stack blocks perform the main commands. There are 75 Stack blocks.
14
Hat Blocks :
Hat blocks are the blocks that start every script. They are
shaped with a rounded top and a bump at the bottom.
This is so you can place hat blocks only at the top of the script.
There are 4 Hat blocks, and they are found in the Control category
Reporter Blocks :
Reporter blocks are used to enter equations, report values, to enter input values
etc.
C Blocks :
Cap Blocks :
Cap blocks are the blocks that stop scripts. They are shaped
with a notch at the top and a flat bottom. Cap blocks cannot
be placed at the top of other blocks. They are placed only at
the end of te script.
There are two Cap blocks, and they are found in the Control category.
Boolean Blocks :
15
Types of Blocks based on function
Blocks can also be grouped on their function. There are eight groups in the
blocks palette. Each group is named based on the function and are displayed in
different colour.
These 8 groups are : Motion block, Looks block, Sound block, Pen block, Control
block, Sensing block, Operator block, and Variable block
Motion blocks : The motion blocks allow you change the direction of a sprite
and to move it. Motion blocks are colored blue. There are currently 16 motion
blocks.
Looks blocks : The Looks blocks are used to modify the appearance of sprite
or stage. Looks blocks are colored purple. There are 20 Looks blocks.
Sound blocks : The Sound blocks are used to control sound and MIDI
functions. Sound blocks are colored pink. There are 13 sound blocks.
Pen blocks : The Pen blocks are used to control pen and drawing. Pen blocks
are colored mint green. There are 11 pen blocks.
Control blocks : The control blocks are used to control scripts. Control blocks
are colored gold. There are 16 Control blocks.
Sensing blocks : The sensing blocks are used to detect things. Sensing
blocks are colored sky blue. There are 17 Sensing blocks.
Variables blocks : The Variable blocks are used to hold values and strings.
Variables blocks are colored orange. There are 5 Variables blocks.
Exercise 3
1. Define a block.
2. Define script.
3. What are Stack blocks?
4. What are Hat blocks?
5. Explain Reporter blocks.
6. What is the use of reporter block?
7. Can we use a reporter block directly in a script? Give reason.
8. How many block groups are there based on function? Name them.
9. Why are looks blocks used?
10. What is the use of variable blocks?
11. How are control blocks useful?
12. Which code blocks are used to stop scripts? Where are they found?
A B
1. Motion blocks a) sky blue colour
2. Looks blocks b) green colour
3. Sound blocks c) blue colour
4. Pen blocks d) orange colour
5. Control blocks e) pink colour
6. Sensing blocks f) purple colour
7. Number blocks g) gold colour
8. Variable blocks h) mint green colour
Lab work 2
1. In the blocks palette, click on the Control button. Drag When flag clicked
code block to script area.
2. Click on Motion block
3. Click and drag Change x by 10 block and place it below When flag clicked.
17
4. Click on the value 10 and change it to 2.
5. Click on the Looks button.
6. Drag the switch to costume block onto the scripts area.
7. Right click on the drop arrow next costume# and click on costume2
8. Click on Control block and drag wait 1 sec next to switch to costume block.
9. Change the value 1 to 0.5
10. Drag glide 1 secs to x 0 y 0 and change the value of glide to 0.2, x to 50 and
y to –50
11. Click on the sprite or the script to test your work
.
18
Lesson 4: Motion Code Blocks
Motion blocks are used to control movement of sprites.
The motion code blocks, are organized into six sub groups,
each of which is separated by a blank space in the blocks
palette.
They are,
There are two motion blocks that are used to point a sprite in a specified
direction. They are Point in direction() and Point towards() blocks.
19
0 = turns the sprite in the upward direction
90 = turns the sprite in the right direction
–90 = turns the sprite in the left direction
180 = turns the sprite in the downward direction
These values are listed in the block’s drop-down list
or
you can type in an integer value in the range of 0 to 360 in the block.
Repositioning a Sprite
There are 3 motion blocks that move a sprite to a specific location on the stage.
Glide () secs to x : () y : () block glides the sprite to the location, for the said amount
of time.
There are 4 motion blocks that changes the location of a sprite on the stage
20
They are,
There are 3 motion (reporter) blocks that can be used to display information about X-
position, d Y-position and the direction of the sprite.
Exercise 4
21
II. Answer the following :
1. If you tell your sprite to change x by -30 and change y by 10, which of the
following statements best describes the motion of the sprite?
a) 30 pixels right, 10 pixels up b) 30 pixels left, 10 pixels down
c) 10 pixels left, 30 pixels up d) 30 pixels left, 10 pixels up
2. If you specify a value of 90 in the point in direction block, what position
does that represent on the Scratch stage?
a) Up b) Down c) Left d) Right
3. The go to block can do all of the following except:
a) Go to the location of another sprite b) Go to location of the mouse
cursor
c) Go to the location of the current sprite
Lab work 3
22
3. Click on the Image icon under the stage to go to the Backdrops Library and
select an underwater background.
Select ‘Animals’ folder. From that, select ‘fish2’ sprite by double clicking on
it.
In the ‘ Current sprite info’ area, change the name of the sprite to ‘purple’ and
press enter key.
5. Repeat step 4 for two more times, add the following list of sprites to the
application project
Sprite Filename Sprite Application Name
fish3 Yellow
fish4 Spotted
6. Arrange the sprite on your stage by dragging them where you want them to
go.
23
8. Next, change the direction of each
fish and add the set rotation style to
left and right.
10. Go back to each fish and add the script in the same way. Set different values for
each fish.
11. Finally, add sound to your project. Click on the stage. You will add sound
to the stage script. Click on the Sound tab above the Script Blocks to enter
the sound library. Select "effects" bubbles.
12. Save your project and run it by clicking on the green flag.
24
Lesson 5: Looks Code Blocks
Looks blocks are used to control the appearance of sprite and of stage backgrounds.
A sprite can have more than one costume. All costumes are listed in the costume tab..
The code blocks that are used to change the costume of sprite are, Switch to
costume(), next costume, costume#
25
Costume # block display the sprite's current costume number on stage.
The blocks that are used to change the background of stage are, Switch to
background(), next background, background#
The background # block display its stage’s current background number on stage
The say () for () secs, say (), think () for () secs, think () blocks are used to display text
in speech and thought bubbles, making a sprite look like talking or thinking.
26
‘say () for () secs’ block displays text in speech bubbles for specific time.
‘think () for () secs’ block display text in thought bubbles for specific time.
27
Changing a Sprite’s Size
Change size by(), Set size to ()% and size() code blocks, are applied only to sprites.
They allow you to change a sprite’s size.
Set size to ()% code block lets you set a sprite’s size to a specific percentage of its
current size (larger or smaller).
size() block holds the sprite's size and the size value can be displayed as a
Stage monitor.
Exercise 5
28
II. Answer the following:
1. Why do we use Looks code blocks?
2. What is a costume?
3. How do you change the costume of a sprite?
4. State the three blocks used to change the background of a stage.
5. Which blocks are used to make a sprite talk or think?
6. Name the special effects that can be given to a sprite.
7. How do you change the size of a sprite?
8. Does the switch to costume block and the next costume block always achieve
the same result? Justify your answer.
Lab work 4
29
2. Delete the cat sprite.
3. Select the spotlight-stage from the Indoor
folder.
4. To add the first dancer sprite, click on the
Choose Sprite from File button located in the
middle of the new sprite button controls.
5. Double-click on the People folder, scroll down
and select the cassy-dancing-1 sprite, and
then click on the OK button.
Place this sprite on left side – first spot light – of the stage.
6. Next, click on the Import button in the sprites area and add a costume to
the first dancer by clicking on Import People cassy-jumping-3
7. In the same way, add a third costume cassy-jumping-4. Now we have
three costumes for first dancer.
8. Similarly add second dancer dan1, place it in the middle of the stage –
middle spot light – and two more costumes dan2
and dan3.
9. Similarly add third dancer anjuli-1, place it in the
right of the stage – right spot light – and two more
costumes anjuli-2 and anjuli-3.
10. Write the given script to first dancer.
11. Repeat the above script to other two sprites also.
12. Save your project and run it by clicking on the green
flag.
30
Lesson 6: Paint Editor
Paint Editor is a simple image editing environment. It is used to create new sprites or
edit costumes or backgrounds and to apply text, color, and shape to our sprites and
backgrounds.
To start Paint Editor, click on Print new sprite button in the New sprite list,
31
Drawing and Coloring Tools
Paint Editor contains the following tools for drawing and coloring:
These options are found under Paintbrush, Eraser tool, Line Tool, Fill Tool,
Rectangle Tool and Ellipse Tool..
Both Paintbrush and Line Tool use the same brush size —
selecting the brush size in one of the two tools will cause a change
in brush size of the other tool.
32
The brush size for Paintbrush and Line Tool also affects the brush size of Ellipse
and Rectangle Tool.
The Fill Tool shows the fill modes you can make
to an image. It is solid color Vertical gradient,
Horizontal gradient or Circle gradient.
The Scratch Paint Editor contains the following tools for manipulating part or the
whole image in the Paint Editor:
To edit an image.
Or
Now you can make changes to the image. Click on OK to get these images on the
stage. However, these new costumes or backgrounds will not be saved in the Scratch
image folders.
Exercise 6
1. If you want to create a text sprite with the Paint Editor, what tool do you
use?
a) Fill tool b) Text tool c) Gradient tool
2. The gradient tool produces what kind of graphical effect?
a) A block of color fades from light to dark shades
34
b) A block of color alternates from light to dark shades like zebra stripes
d) It draws a solid block of color with a dark center
Lab work 5
19.Now do the same for costume 3 using right arrow to provide animation.
20.Save and test your project
36
Lesson 7: Pen Code Blocks
The Pen code block allow to draw custom lines, shapes,
and other graphics. They can also be used to set the
color, width, and shade used in drawing operations.
This pen works very much like a real pen. When placed in a
down position, it can be used to draw on the stage. When
placed in an up position, drawing ceases.
There are three pen code blocks – Set pen color to (), Change pen color by() and Set
pen color to() are used to control the pen color.
Set pen color to () lets you set the color to be used while
drawing.
37
When the swatch is clicked, it displays a color
palette.
The following list show the numbers that you can use to specify a range of commonly
used colors.
The code blocks, Change pen shade by() and Set pen
shade to() are used to control the level of shading
applied while drawing.
The code blocks, Change pen size by() and Set pen
size to() let you change the size of the pen.
38
Stamping (copying) an Instance of a Costume on the Stage
The last block, Stamp allows you to make copies of the selected
costume of a sprite on the stage.
Exercise 7
1. A shade value of 0 results in a ……… color and a shade value of 100 results in
…………. color.
2. The range of values supported by the pen shade is …………….
3. The default shading value for any color is ………..when drawing colors
Lab work 6
39
3. Click on “Paint New Sprite” button from New Sprites options to open the
Paint Editor.
4. Select the line tool and draw a pencil.
5. Fill pencil with a color of your choice. Click on OK to get pencil sprite on the
stage.
6. Select the Pencil sprite by clicking on it.
7. In the Scripts area, drag the following code blocks and make changes to
values as shown.
40
Lesson 8: Control Code Blocks
Control blocks are the building blocks
of programming.
start events
check conditions
repeating or looping
pause or halt script execution
send and receive messages
Sequence
In this method only stack blocks are used to create script. This is a simple
method. With this construct the script becomes very lengthy script and is difficult
to create powerful events.
41
All stack blocks are simple commands. They instruct the sprite to do one simple
action only one time.
For example,
Stack blocks alone are not useful when we want to execute a set of commands
selectively or when we want to repeat some commands several times.
By using condition checking and looping methods we can create short and
powerful scripts.
Boolean Expression
For example,
Conditional statements ask questions about the program state to choose from a
set of different sequences of commands.
Conditional statements have slots for Boolean blocks which evaluate to a true or
a false value and execute if the statement is true.
In Scratch, any block whose label says "if," "while," or "until" is example of
conditional logic
42
Conditional blocks
There are five conditional blocks used to apply conditional
logic in the scripts.
They are, if(), if()… else, wait until(), forever if() and
repeat until() blocks.
If() block
if ()… else block is also used to check a true or false condition of an expression.
If the condition is true, the blocks embedded within if() block are
executed otherwise the blocks embedded within else block are
executed.
43
foreverif () block executes blocks inside it continuously, as long
as the condition is true.
Loops
Repeat () block runs the blocks inside for said number of times.
So, the fewer code blocks you use when writing scripts, the more is the
program efficiency.
Nested Blocks
When one or more conditional blocks or looping blocks are inserted with in
another conditional block or looping block, it is called Nested blocks.
Nested blocks are very useful to test multiple conditions or to execute multi tier loops.
44
Endless Loops
Loops are extremely powerful tools, providing the capability to perform repetitive
tasks with ease. However, if loops are not set properly, that is if there are
overlapping or the sequence is not correct, it results
in an endless loop.
Event Handling
When an event is generated another object may responds to that event. For
example, when ‘Save’ button is clicked it opens a dialog box.
Event handling is controlled by hat blocks which are used to start when an event
occurs.
They are When the flag is clicked, When () Key Pressed, When I receive () and When ()
Clicked.
When the flag is clicked, block starts the script. It is the most
common means of starting an application’s execution.
45
When I receive () block starts script execution when a
message is sent by another script.
There are two code blocks that are used to send and receive broadcast messages as a
means of communication.
Using the hat block When I receive (), you can set up a script to execute whenever
a specified message is received.
The stop script and stop all blocks halt script execution.
Using stop script block, you can halt the execution of the
scripts in which the code block is placed.
Using stop all block, you can halt the execution of the scripts completely.
46
Exercise 8
Lab work 7
1. Drawing circle
1. Summary of the Project :
In this exercise you will create a pencil sprite in the Pain Editor. When flag
is clicked the pencil start drawing a circle in the green color..
Method :
1. Start a new Scratch application.
2. Delete the cat sprite.
3. In the Paint editor draw a pencil sprite.
4. In the script area stack the following code blocks.
when flag clicked
clear
set pen color to (select color for your pen by clicking on the color
box and
pen down with the eyedropper)
repeat 360
move 1 steps
wait 0.05 secs
turn 1 degrees
5. Test your work and save.
2. Painting application
1. Summary of the Project : In this exercise you will create a drawing
application where the user can select a color by clicking on the
respective color box. Then the user can draw any drawing using the
mouse.
Method :
1. Start a new Scratch application.
2. Delete the cat sprite.
3. In the Paint editor draw a small square sprite in blue color. Rename it to
colour1.
4. Create a costume of blue sprite and rename it to red.
5. .In the script area stack the following code blocks.
49
Lesson 9: Variables Code Blocks
Variables play an important role in computer
programming.
Data is any type of information stored in the variable that is used in a program for some
purpose.
Scratch lets you work with a number of different types of data. They are,
A string is a piece of text data that you hard code within Scratch
applications.
Boolean data is data that represents a value of either True or False.
An integer is a numeric value that does not include a decimal point.
A real number is a number that includes a decimal number.
Variables may belong to the sprites or to the stage. Therefore, it is important that
when adding new variables to your application, you select the thumbnail for the
sprite (or stage) where the
variable belongs
Creating Variables
There are 5 Variables blocks. There are four Stack blocks and
one Reporter block in this category.
50
To create a variable, you must first define and add them to your application projects.
You can choose whether the variable is for all sprites (global) or just for one sprite
(local).
Variables are of two types – local or global. A local variable can be used by just one
sprite; and a global variable can be used by all of your sprites in a program.
A variable’s scope identifies the location within an application where the variable’s value
can be modified. Scratch supports two levels of variable scope :
1. Local. Variables can be modified only in the scripts belonging to the sprite in
which the variable is defined.
2. Global. Variables can be modified by any script in an
application.
After a variable is created, the variable block show this set of blocks
set the variable’s initial value or to increment or decrement the
variable value.
51
To delete a variable, click on delete variable button in blocks palette. From the
variable list, click on the variable name to delete it.
Letters
Numbers
Special characters
Blank spaces
Example
For the first time you run the previous script, it counts from 1 to 10. However, if you run
it again, it will count from 11 to 20. You can change this behavior by explicitly assigning
an initial value to the Counter variable.
Creating Lists
A list can store more than one item in it in continuous spaces. A list is identified
by a name like variables. Each item in the list is accessed by a location number.
Lists can store numbers as well as strings of letters and other characters.
Allows you to create and name a new list. When you create a
list, the blocks for that list will appear. You can choose whether the list is for all
sprites (global) or just for one sprite (local).
When you create a list, a list monitor will appear on the stage. A list monitor
shows all the items in a given list. You can type items directly into a list monitor.
52
At first the list will be empty, with length 0. To add to the list, click the + button on
the bottom left of the list monitor. The length will increase by 1. Alternatively, you
can add to the list using the list blocks (e.g., ).
You can resize the list monitor from the bottom right-hand corner.
Adds the specified item to the end of the list. The item can be
a number or a string of letters and other characters.
Deletes one or all items from a list. You can choose from the
pull-down menu, or use a number to specify which item to delete. Choosing
?last? deletes the last item in the list. Choosing ?all? deletes everything from the
list. Deleting decreases the length of the list.
Strings are made of letters, words, or other characters (e.g., apple; October
2009; You win!).
53
Strings can be stored in variables or lists(such as or
).
You can join together strings using . You can compare strings using the
following blocks: , , or .
Strings are evaluated as 0 in mathematical operation blocks (such as: )
).
Keyboard input
Exercise 9
1. If you want to create a variable for a sprite’s exclusive use, which of the
following options do you select when creating the variable?
a) For all sprites b) Not for this sprite
c) For all sprites but this one d) For this sprite only
54
2. We use a counter variable to:
a) Track how many times an event occurs
b) Identify how many sprites we have in the project
c) Select a random item from a list
d) Add a new item to a specific position in the list
3. A variable name can have
a) letters b) numbers c) blank spaces d) all of these
1. Define a variable.
2. Define data.
3. What does the data type indicates?
4. Which data types are supported by Scratch?
5. How can we create a variable?
6. How many types of variables are there? Differentiate them.
7. How do you delete a variable?
A B
1. String a) 100
2. Boolean b) 0.5
3. Integer c) you win
4. Real Number d) true or false
Lab work 8
This project demonstrate how one can find the prime factors of any
number, systematically. Any number can be expressed in a product of
prime factors, or in a factor tree.
55
Method :
Method :
57
Lesson 10: Operator Code Blocks
Operator code blocks are green in color. Operator code blocks are used to perform
arithmetic operations and logical comparisons. In addition, operator code block also has
many built-in mathematical functions that can be used in calculations.
Arithmetic operators
Order of Precedence
Example : The following script evaluates a numeric expression and assigns the result to
a variable named Total.
In the equation ((4 * 8) + (4 – 2)) / 2, multiplication (4 *
5) is performed first and subtraction (4 – 2) next.
Result of these two arithmetic operations are added
next (32) + (2). Resultant value 34 is divided by 2 to
give the total 17.
58
Generating a Random Number
A random number is any number produced or generated from within the given
range of numbers or a random number is one that is drawn from a set of possible
values.
Logical Operators
round () block rounds off the given decimal number to specified digits.
59
Built-in Mathematical Functions
Some the functions that are performed by this code block are
;
Exercise 10
1. When using the pick a random number block, how can we control what
random number gets selected?
a) Change the beginning number range b) Change the end
range
c) We can’t control which random number gets selected d) All of the
above
60
2. Which of the following mathematical functions does Scratch make
available for use in your scripts:
a) Square root b) Exponents c) Absolute value d) All of the above
3. The mod block:
a) Modifies a number in the list
b) Creates a variable that tracks an interval
c) Transforms the sprite into pixelate form
d) Divides two numbers and returns the remainder
Lab work 9
1. Drawing a polygon
Method :
1. The goal this project is to connect nine dots using only 4 lines. If the user does he
wins. Otherwise the game will over. The game can be started by clicking on reset
button. You can greet the winner with suitable remarks and screen. When the user
fails display that game is over. You can also display the user how many dots are
connected and how many lines are used to connect them.
61
Method :
5. Create a black dot (little bigger than pen). Rename it to Dot1. Make a copy to
create another costume of this dot. Change the color of this costume to red.
6. Add the following code to Dot1.
7. Create other 8 copies of the Dot1 sprite by right clicking on it and selecting
duplicate. Rename all of them as Dot2, Dot3 and so on. Place the dots as
shown in the screen.
8. Create a Reset button sprite at the bottom of the screen.
9. Add this code to Reset button sprite
when reset clicked when flag clicked
broadcast reset broadcast reset
62
10. Create a gameover sprite and add these codes ot its script area.
when i receive reset when i receive fail
hide show
14. Create a win sprite and add these script to it.
when i receive reset when i receive win
hide show
stop script
15. Click on stage thumbnail to select it. In its script area, add the following code
when i receive reset when stage clicked
set dots connected to 0 set x to mouse x
set lines used to 0 set y to mouse y
set first click to 0 broadcast draw
clear
63
Lesson 11: Sensing Code Blocks
Sensing code blocks are used to detect the location or distance between the
sprites, collision of sprites with one another, which key is pressed by the user,
mouse pointer location etc.
There are 17 Sensing blocks. Of that 2 are Stack blocks, 7
are
Boolean blocks and 8 are Reporter blocks.
The color() is touching () block is used to check if a color on the sprite is touching
another specific color.
Keyboard input
When ask() and wait block is executed, the program waits until you type the
response and press the Enter key or the click check mark.
The mouse (Y) block returns the location of the mouse pointer
as it moves along the Y-axis. The range of ‘Y – axis’ value is
from –180 to 180.
Determining Distance
Timer blocks
The timer blocks are used to check the time elapsed since the start of application.
The reset timer block resets the timer back to its default value of
zero.
Using timer blocks, you can control the pace of animation and the operation of your
applications.
The x position of sprite code block is used to get information about the sprite or
the stage.
X position
Y position
65
Direction
Costume number
Size
Volume
The loudness() and loud? blocks allow you to sense sound input from the
computer’s microphone and to use that input within your
applications.
The loud? block returns a true / false value, depending on whether a sound value
of 30 or greater is detected through the computer’s microphone.
Exercise 11
Lab work 10
1. The user controls the main character, the red car, through the
maze using the arrow keys. The car is not able to move through
the maze walls. The car needs to pick up the Wizard Hat and
the Key before it is allowed to move through the door at the
upper right of the maze.
Method :
1. Use the paint tool to create a maze as a background as shown. Make sure all
of the paths are large enough for your main character to move through. Just
use a single color.
2. Import a car sprite. Resize it.
Set the rotation property of the Sprite to None. Write code so that the car can
move up, down, left, and right. Write the initialization code to specify where the
car starts in the maze.
67
move 5 steps move 5 steps
when right arrow key pressed when left arrow key pressed
point in direction 90 point in direction –90
if not color pink is touching purple if not color blue is touching
purple
move 5 steps move 5 steps
3. Create objects to be picked up – A Wizard Hat and a Key Place these objects
in different locations in the maze. Write initialization code so the objects
appear in good places with a good size.
68
6. To get to the next level, the car has to go through the door. 7. Make a door
Sprite and place it in a good location in the maze. We want the door to detect
when the car touches it.
when flag clicked if ans =1 and have hat = 1 and have key = 1
go to x1 210 y1 102 broadcast you win
show else
wait until touching car? broadcast you lost
set ans to 1
when I receive you win
hide
when I receive you lost
hide
8. When the car touches the door, the backgrounds should change with
appropriate message..
9. Create two more backgrounds – one to show win message and the other to
show lost message. Rename them as ‘win’ and ‘ lost’. Add the following code
to the stage
69
Lesson 12: Sound Code Blocks
Applications like computer games, uses sound as a means of conveying meaning and
excitement.
Playing Sounds
These code blocks allow to play or stop the playback of
MP3 and wave files in your applications.
Play sound and Play sound() until done code blocks let
you play any MP3 or wave file in a Scratch project.
Once the file is imported, you can play the audio file using a script belonging to the
stage or sprite.
Play a Drum
Using the two code blocks shown, you can add the playing of a drum to your Scratch
application and, when necessary, pause drum play for a specifi ed number of beats.
The Rest for () Beats block is a music block and a stack block. The block pauses
its script for the specified amount of beats.
In addition to playing audio files and different types of drum beats, Scratch lets you play
musical notes with various instruments using the sound code blocks.
71
You can specify a note either by typing it into the code block’s first input box or by
clicking on the drop-down list located inside the code block’s input field, which displays
a graphic representation of a piano keyboard.
The Set Instrument to () block changes the instrument that the Play Note () for ()
Beatsblock will play.
It supports a total of 128 different instruments, numbered 1 to 128. You can select an
instrument by keying its number into the block’s input field or by selecting an instrument
from the block’s drop-down list.
Set volume to() code block lets you assign a specific value to a sprite in the range of 0
to 100.
Using the third code block, you can retrieve a sprite’s volume and optionally display this
value in a monitor on the stage.
The last three looks blocks are used to set, change, and
report on the tempo at which drum beats and musical notes
are played.
Change tempo by () code block changes the tempo used to play a drum or note.
Tempo is a measurement of the speed, in beats per minute, at which a drum or note is
played. The larger the tempo value, the faster the drum or note is played.
72
Volume is the loudness of the sound and tempo is the pace of the sound.
Set temp to () code block lets you set the tempo used to play a drum or note to a
specific number of beats per second.
Using tempo () code block, you can display a sprite’s currently assigned tempo
and optionally display this value in a monitor on the stage.
Exercise 12
Lab work 11
Method :
74
when flag clicked when flag clicked
forever forever if touching tracer?
if touching tracer? play drum 36 for 0.2 secs
Switch to costume drum3 wait until not touching tracer?
else
Switch to costume drum2
11. Right click on drum2 sprite in sprite list area and click duplicate to make 2
more copies. Place these drums in the positions shown in figure.
12. Similarly import ‘lamp’ sprite. Make a copy of it. Edit it in Paint editor by
adding yellow shade to lamp head to give a lighting effect. Rename it to
lamp2.
13. Add this script to all lamp sprite.
12. Similarly import ‘cheasy puffs’ sprite. Make a copy of it. Edit it in Paint editor to
make it distorted. Rename it to cheasy puffs2.
14. Make 2 copies of cheasy puffs sprite by right clicking on the sprite and
position them as shown.
15. Click on stage sprite. Click on import button Select pop sound. Click on OK to
add it to stage sprite.
16. Save, test and enjoy your program !
75