Fourth Condition: Event & Control

Quick References for all Events blocks

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

 

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.

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.

Enjoy Flowers Here!