Fourth Condition: Event & Control
Quick References for all Events blocks
when [green flag] clicked:
This block starts the script when the green flag (which is the universal start button in Scratch) is clicked. It's commonly used to start games, animations, or reset the project to its initial state.when [key name] key pressed:
This block triggers the script when the specified key on the keyboard is pressed. You can choose different keys for different actions.when this sprite clicked:
This block starts the script when the sprite it is attached to is clicked with the mouse. This can be used for interactions directly with the sprite, like making it talk, move, or change appearance when clicked.when backdrop switches to [backdrop name]:
This block triggers the script when the stage's backdrop changes to the specified backdrop. It is often used to change the behavior of sprites when the scene changes.when [loudness/timer] > (number):
This is an event block that begins when the loudness detected by the computer's microphone or timer exceeds the specified value. This can be used for interactive projects that respond to sound, such as voice commands or clapping.when I receive [message]:
This event block triggers when it receives the specified broadcast message. This is used for communication between different scripts or sprites within a project.broadcast [message]:
This block sends out a "message" broadcast throughout the project. Any scripts with a "when I receive [message]" block will start running when this broadcast is sent.broadcast [message] and wait:
This block not only sends out the "message" broadcast but also waits until all the scripts triggered by this message have completed their execution before continuing with the next block. This is useful for coordinating sequences of events that must occur in a specific order.
Broadcast messages are essential for creating interactions between sprites in Scratch projects. They allow for decoupled communication, where one part of a project can send out a message without needing to know exactly which other parts will respond to it.
Quick References for all Control blocks
wait (number) seconds:
This block pauses the script for a specified number of seconds. The script waits for 1 second before moving on to the next block.repeat (number):
This is a loop block that repeats the nested blocks a specified number of times. Here, the blocks inside this loop would repeat 10 times.forever:
This block repeats the blocks inside it infinitely until the program is stopped or the sprite is deleted.if <condition> then:
This block is a conditional statement that runs the blocks inside if the specified condition is true.if <condition> then {blocks} else {blocks}:
This is an extended conditional statement that runs the first set of blocks if the condition is true and the second set of blocks (after "else") if the condition is false.wait until <condition> :
This block pauses the script until the specified condition becomes true. The script will resume immediately once the condition is met.repeat until <condition> :
This block is a loop that continues to run the nested blocks until the specified condition becomes true, then stops.stop [all/this script/other scripts in sprite]:
This block is a control block that can end the game, stop animations, or reset the project.
all: Stops all scripts in the project.
this script: Stops only the script that contains this block.
other scripts in sprite: Stops all other scripts running in the same sprite.
when I start as a clone:
This is an event block that triggers when a clone of the sprite is created. It runs the blocks that are attached to it, allowing you to define specific behavior for clones, separate from the original sprite.create clone of (sprite name):
This block creates a clone (a copy) of the sprite. The dropdown can be changed to clone other sprites if needed. Clones can be used for effects like particles, characters in a game, or other repetitive elements.delete this clone:
This block deletes the clone that runs this block. It's typically used to manage the number of clones present in the project at any time, like removing a bullet once it hits a target or clearing particles once they've faded away.
When [condition] Blocks
These blocks execute the following codes, when [something happens]. The [Condition] can be detected from different input sources such as mouse, keyboard, microphone, camera, other sprites/backdrop, or other extension devices.
The orange blocks on the left are ready-to-use blocks for all Scratch projects. Most of them are in Events blocks, the last one is the only one in Control blocks.
The Green blocks on the right are some sample event blocks from Scratch Extensions. You can add them to your project by clicking the purple extension button. All of them need to be connected to another hardware device to work except the Video sensing block. If you have a camera on your laptop/computer/tablet that you use to code Scratch, you can use video sensing by simply allowing camera access.
Input from Mouse
The green flag is usually used as a start button for the game. You can attach any initial setting of the game after it to make sure the game reset.
Input from Keyboard
Input from other sprites
The backdrop switch block will only be triggered by switch backdrop blocks. Similarly, receive block links to boardcast, start as clone block links to create a clone.
Input from Microphone
This block will allow Scratch to access your microphone and detect the loudness of sound.
wait (number) seconds
This block waits for a certain time, then executes the next block.
Scratch refreshes your screen 30 times per second and constantly checks the conditions. If you would like to control the detection to make only 1 change in a certain time, you can use the wait block to pause.
Example:
wait (0.3) seconds
Click the green flag to start
Click the cat to create a clone(copy) of it. This clone will move automatically.
repeat (number)
This block repeats running all blocks inside for a certain times. This is called a loop in programming.
It acts the same as copying and pasting a code segment many times. After the last one runs, it will jump back to the first block immediately.
Example:
repeat(10):
switch costume to (costume2)
wait (0.5) seconds
switch costume to (costume1)
wait (0.5) seconds
Equivalent Code (x10)
Click the green flag to start
Press the Space key to see the animation of the cat walking
forever
This block repeats running all blocks inside infinitely until you press STOP manually. This is called an infinite loop in programming.
Be careful with the forever block, it might conflict with other blocks and stick your program in the infinite loop.
Example:
forever:
play sound (Garden) until done
Click the green flag to start: it will set the cat to the lower middle of the screen, change the background to flower, clear all graphic effects if any, and constantly play the background music Garden.
if <condition> then {blocks}
if <condition> then {blocks} else {blocks}:
If-then block checks a condition (see details in sensing). If that condition is true, it will execute the blocks inside. If it is not true (false), it will not do anything but go to the next block.
The first half of the if-then-else block is the same as the if-then block. But if the condition is not true, it will execute the code inside the else.
Example:
if <touching edge>? then:
broadcast Butterfly
delete this clone
else:
glide (1) secs to random position
Butterfly Code
Say something to your microphone and keep your mouse on the cat (don't need to press)
Click the cat to create a clone. If the clone touches the edge, it will broadcast (Butterfly) to show the Butterfly and delete itself. If it is not touching the edge, it will glide 1 second to a random position. This is in a forever loop, it will keep running until the clone touches the edge and deletes itself.
wait until<condition>
repeat until<condition>
These two blocks act the same as repeat and wait. But instead of using a number to control the time, it will use a condition. If this condition is true, it will end the waiting time or loop time to run the next block.
Example:
wait until <mouse down>?
repeat until <touching color (red)>:
glide (0.5) secs to mouse-pointer
Butterfly Code
Wait for one clone that touches the edge, the butterfly will broadcast color after talking. It will wait until all codes under [when I receive Color] finish running.
Click the mouse anywhere, it will switch to Tree background. Here the cat will move with your mouse pointer to find the flower. After getting the flower(touching color red), it will stop all scripts and end this game.
stop [all/this script/other scripts in sprite]
This block can end the game, stop animations, or reset the project.
stop [all]: Stop all scripts in the project.
stop [this script]: Stop only the script that contains this block.
stop [other scripts in sprite]: Stop all other scripts running in the same sprite.
Example:
stop [all]
In the Tree background, the cat will move with your mouse pointer to find the flower. After getting the flower(touching color red), it will stop all scripts and end this game.
create clone of (sprite name)
delete clone
These blocks allow you to make a copy of your sprite and delete it easily without copying all the codes again. The clone will act the same as the original one, and can also act differently by adding [when I start as clone] block. Clone is commonly used in an element that repeats a lot.
Example:
create a clone of myself
delete this clone
In the Tree background, the cat will move with your mouse pointer to find the flower. After getting the flower(touching color red), it will stop all scripts and end this game.