Scratch Programming F
Scratch Programming F
Module-V
SCRATCH PROGRAMMING
Scratch Programming
Introduction to Scratch
Scratch is a block-based visual programming language created by the MIT Media Lab. It is
widely used as an educational tool to introduce children and beginners to computer
programming. Unlike traditional programming languages (such as C, Python, or Java), Scratch
does not require typing code. Instead, it uses a drag-and-drop interface with colorful blocks
that snap together like puzzle pieces.
The goal of Scratch is to make learning programming concepts fun, interactive, and
intuitive. With Scratch, learners can create stories, animations, games, and music while
simultaneously developing problem-solving skills and logical thinking.
Scratch introduces almost all the fundamental programming concepts found in traditional
languages, but in a visual, simplified form:
o Example: Making a sprite move forward 10 steps, then say “Hello” for 2 seconds.
o Example: Making a ball bounce continuously by repeating the "move and turn"
blocks forever.
o Example: If the sprite touches the edge of the stage, it turns around.
Dr.T.Premkumar
o Example: Starting the program when the green flag is clicked or when the space
key is pressed.
The Scratch environment is graphical and interactive, with several key components.
3.1 Stage
• The Stage is the main output screen where all animations, interactions, and games take
place.
Example: In a racing game, the stage could be a road image, while cars (sprites) move on top of
it.
3.2 Sprites
Example: A cat sprite can have costumes to show walking or running movements.
This is the toolbox of Scratch. It contains all the programming blocks, categorized by function.
Dr.T.Premkumar
ASP/ME
o Example: move 10 steps, turn 15 degrees.
• The workspace where blocks are dragged and snapped together to form scripts.
Example:
To make a sprite move when the green flag is clicked:
move 10 steps
Script:
move 10 steps
The Paint Editor in Scratch is a tool used to create, modify, and customize costumes and
backdrops for sprites and stages. Costumes define how a sprite looks, and multiple costumes
can be used to create animations.
o Vector: Shapes and images that can be scaled without losing quality.
• Drawing Tools
• Color Palette
Example
2. Scratch Blocks
Scratch programs are built using blocks. These blocks are color-coded and shaped to fit
together like puzzle pieces. Each category of blocks represents a type of action or control.
Looks Purple say "Hello!", change color effect Control appearance and
speech.
Sound Pink play sound "meow", stop all Play sounds or music.
sounds
Events Yellow when green flag clicked, when Start actions on triggers.
key pressed
Variables Dark set score to 0, change lives by -1 Store and change data.
Orange
Dr.T.Premkumar
ASP/ME
My Red define jump Custom reusable blocks.
Blocks
Arithmetic operators are found in the Operators Block category (Green). They are used to
perform mathematical calculations and logical operations.
Example Program
Addition (a + b) a+b
Subtraction (a - b) a-b
Dr.T.Premkumar
ASP/ME
Multiplication (a * b) a*b
Division (a / b) a/b
Functions in Scratch are represented using “My Blocks”. These are custom-defined blocks that
allow users to create reusable sets of instructions.
define jump
change y by 50
change y by -50
• Any sprite that calls jump will move up and then come down.
repeat (n)
move 10 steps
end
Dr.T.Premkumar
ASP/ME
• Code reusability (no repeating scripts).
Motion commands are part of the Motion Block category (blue blocks) in Scratch. They
control how a sprite moves and rotates on the stage. Motion commands work with x and y
coordinates of the stage.
o Center: (0,0)
move (10) steps Moves the sprite forward by given Cat moves 10 steps
steps in current direction. forward.
turn clockwise (15) Rotates sprite clockwise. Cat rotates right 15°.
degrees
glide (1) secs to x: (50) y: Smoothly moves sprite to position in Cat glides to (50,50) in
(50) given seconds. 1 second.
point in direction (90) Makes sprite face specific direction. Cat faces right.
if on edge, bounce Makes sprite bounce off stage edges. Used in ball games.
end
➡ The sprite draws a square path by moving forward and turning right 90° four times.
Pen commands are available through the Pen Extension. They allow a sprite to draw on the
stage as it moves, similar to “turtle graphics” in Logo programming.
pen down Lowers the pen, sprite draws as it Sprite leaves a trail when
moves. moving.
pen up Lifts the pen, sprite moves without Sprite moves invisibly.
drawing.
set pen color to [ ] Sets pen color. Change pen to red or blue.
set pen size to (5) Sets line thickness. Draw thick or thin lines.
clear
pen down
repeat 3
Dr.T.Premkumar
ASP/ME
move 100 steps
end
pen up
By combining Motion Commands and Pen Commands, Scratch can be used to draw
geometric shapes, patterns, and art designs.
clear
pen down
repeat 360
move 2 steps
end
pen up
clear
pen down
Dr.T.Premkumar
ASP/ME
set pen size to 2
repeat 50
end
clear
pen down
end
end
pen up
Shape Steps
Dr.T.Premkumar
ASP/ME
Triangle repeat 3 {move N steps, turn 120°}
The Looks Palette in Scratch contains purple blocks that control a sprite’s appearance,
speech, thought bubbles, and costume changes. These blocks are essential for creating
animations, dialogues, and visual effects.
Speech/Thought say "Hello!" for 2 seconds, Make sprites talk or think on stage.
Bubbles think "Hmm.."
Graphic Effects set color effect to 25, change Apply visual effects like color,
size by 10 brightness, ghost.
Size/Visibility set size to 150%, hide, show Resize, hide, or display sprites.
repeat 10
next costume
move 10 steps
Dr.T.Premkumar
ASP/ME
end
➡ The sprite switches costumes and moves, creating the effect of walking.
The Sound Palette (pink blocks) allows sprites to play sounds, music, and voice recordings.
Scratch includes a sound library, but users can also record or upload custom sounds.
play sound [meow] Plays a sound completely before Cat meows when clicked.
until done moving to next block
start sound [drum] Plays a sound but continues running Background music starts while
next blocks sprite moves.
stop all sounds Stops all currently playing sounds Useful for ending a game with
silence.
set volume to 100% Sets volume to a specific level Starts music at full volume.
clear sound effects Resets sound modifications Removes pitch or echo effects.
repeat 10
next costume
move 10 steps
Dr.T.Premkumar
ASP/ME
end
3. Power of Repeat
The Repeat command is part of the Control Blocks (orange blocks). It allows Scratch
programs to loop instructions, reducing repetition and making programs efficient.
Repeat Until repeat until <touching Loops until a condition becomes true.
Condition edge?>
Advantages of Repeat
Dr.T.Premkumar
ASP/ME
when green flag clicked
pen down
repeat 4
end
pen up
forever
move 10 steps
if on edge, bounce
end
move 10 steps
end
Dr.T.Premkumar
ASP/ME
Draw Square move-turn, move-turn, move-turn, repeat 4 {move-turn} (5 blocks)
move-turn (8 blocks)
Animate Walking move 10 steps, next costume repeat 10 {move 10 steps, next
10 steps repeated 10 times (20 blocks) costume} (3 blocks)
Unlike text-based languages (like Python, C, or Java), Scratch does not have strict data types.
Instead, values are dynamically handled depending on their use. Every variable or block input
can hold numbers, strings (text), or Boolean values.
Number Whole numbers or decimals, used for score = 100, speed = 3.5
calculations.
Examples
Number Example:
1. set score to 50
2. change score by 10
String Example:
Boolean Example:
Dr.T.Premkumar
ASP/ME
5. say "I hit the edge!"
6. end
2. Variables in Scratch
A variable is like a storage box that holds values such as numbers or text. Variables make
programs dynamic, allowing data to change during execution.
In Scratch, variables are created using the Variables Palette (dark orange blocks).
Creating a Variable
1. Go to Variables Palette.
4. Choose For all sprites (global) or For this sprite only (local).
Variable Blocks
show variable [ ] Displays variable value on stage. Shows “score: 10” on screen.
set score to 0
forever
Dr.T.Premkumar
ASP/ME
change score by 1
end
end
Example 2: Timer
set time to 60
forever
wait 1 second
change time by -1
stop all
end
end
Scratch allows programs to interact with users through the Sensing Palette (light blue
blocks). The most common way is by asking questions and storing responses.
Input Blocks
ask [What's your name?] Displays a question and waits for Program asks for player’s
and wait user input. name.
answer Stores the user’s last input. Used after the ask block.
key [space] pressed? Returns true if a key is pressed. Controls sprite movement.
Dr.T.Premkumar
ASP/ME
mouse down? / mouse x, Detects mouse actions and Used in drawing or games.
mouse y position.
set score to 0
change score by 1
say "Correct!"
else
say "Wrong!"
end
forever
change x by 10
end
Dr.T.Premkumar
ASP/ME
if <key [left arrow] pressed?> then
change x by -10
end
end
In Scratch, programs often need to make decisions just like humans do.
For example:
These decisions are made using control blocks such as if and if-else.
Decision Example
end
2. Comparison Operators
Scratch uses comparison operators (found in the Operators Palette, green blocks) to
compare numbers, variables, or text.
They always return a Boolean (true/false).
say "Correct!"
else
say "Wrong!"
end
3. Decision Structures
3. end
2. say "Winner!"
3. else
5. end
Nested Decisions
3. else
6. else
Dr.T.Premkumar
ASP/ME
7. say "Bronze Medal"
8. end
9. end
4. Logical Operators
Logical operators combine or modify conditions. These are also found in the Operators Palette.
and Both conditions must be true. <score > 10> and <time > 0>
not Reverses the condition (true → not <touching edge?> → true if sprite is not
false). touching
end
end
5. Repetition (Loops)
Repetition allows actions to be done multiple times without writing the same code repeatedly.
Dr.T.Premkumar
ASP/ME
Types of Loops
repeat (n) Repeats a set of actions a fixed number Repeat a dance move 10
of times. times.
repeat until Repeats until a condition becomes true. Loop until time = 0.
<condition>
forever
move 10 steps
end
end
repeat 4
turn 90 degrees
end
change score by 1
end
end
Loops are essential for repeating actions. Scratch offers several loop control blocks in the
Control Palette.
repeat until Repeats until a condition is true. Play until time runs
<condition> out.
forever
move 10 steps
end
end
2. Stop Commands
Scratch provides commands to end a script, all scripts, or a specific sprite’s scripts.
Dr.T.Premkumar
ASP/ME
Stop Block Action
stop [other scripts in sprite] Stops other running scripts of the same sprite.
stop [all v]
end
3. Counters in Scratch
A counter is usually implemented with a variable that keeps track of how many times
something happens.
set [counter v] to 0
repeat 10
change [counter v] by 1
move 10 steps
end
Dr.T.Premkumar
ASP/ME
Time Count seconds elapsed
4. Nested Loops
Nested loops mean a loop inside another loop. They are useful for complex patterns.
repeat 5
repeat 5
move 20 steps
stamp
end
change y by -20
end
5. Recursion in Scratch
Scratch does not have traditional recursion, but custom blocks (My Blocks) can be designed to
call themselves.
turn 15 degrees
drawSpiral (size - 2)
end
Dr.T.Premkumar
ASP/ME
Strings are sequences of characters (letters, digits, symbols). Scratch has operators for string
handling.
letter (n) of [string] Extracts nth letter of a string. letter 3 of "Scratch" → “r”
else
end
a. Concatenation
say (greeting)
b. Extracting Letters
say (firstLetter)
c. Counting Characters
Dr.T.Premkumar
ASP/ME
set [count v] to (length of (answer))
set [tries v] to 0
change [tries v] by 1
end
stop [all v]
A list in Scratch is like a container that can hold multiple items (numbers, words, or both). It is
similar to an array in other programming languages.
• Displays as a block with operations like add, delete, insert, replace, item of, length of,
contains.
2. Operations on Lists
Dr.T.Premkumar
ASP/ME
replace item (index) of [list] with Updates value at Replace Banana with Guava.
[thing] position.
3. Dynamic Lists
repeat 3
end
4. Numerical Lists
Lists can also store numbers, useful in games, calculations, and data analysis.
set [Scores v] to []
add 85 to [Scores v]
add 92 to [Scores v]
add 76 to [Scores v]
Dr.T.Premkumar
ASP/ME
Index Scores
1 85
2 92
3 76
set [sum v] to 0
end
5. Searching in Lists
Scratch does not have built-in search, but we can create it using loops and conditions.
a. Linear Search
set [found v] to 0
set [found v] to 1
end
end
Dr.T.Premkumar
ASP/ME
say "Name Found"
else
end
6. Sorting Lists
set [swapped v] to 1
set [swapped v] to 0
if <(item (repeat counter) of [Scores v]) > (item ((repeat counter) + 1) of [Scores v])>
then
set [swapped v] to 1
end
end
end
repeat 5
end
end
end
Dr.T.Premkumar
ASP/ME