One-sentence summary
In this lesson I learn to use the "if" block from Scratch's Control category together with Sensing blocks, so my sprites can make decisions based on what is happening on the stage.
Why it matters
In the previous lesson we learned to tell a sprite, "when the green flag is clicked, do this." But real games are smarter than that. We want a ball to bounce back when it hits the edge, or a character to stop when it reaches a wall.
For that, the program has to make a decision. A decision is a sentence like "if this is true, then do that." In Scratch we build that sentence with the orange blocks in the Control category.
But how does the program know whether "this is true"? That is where the light-blue blocks in the Sensing category come in. They are like a sprite's eyes and ears: they check whether we are touching something, whether the mouse is down, or whether a key is being pressed.
When you use conditions and sensing together, sprites begin to react to their surroundings. That is exactly the heart of making a game.
What is a condition? The "if ... then" block
A condition is a step that runs only when a certain situation is true. The idea is simple: "if this happens, do that."
In Scratch we build it with the "if ... then" block from the Control category. This block opens up like the letter C, and the blocks you place inside it run only when the condition is true.
Into the hexagon-shaped slot we drop a Sensing block. That hexagon slot always expects a "yes" or "no" (true/false) answer.
Example 1: Change colour when it touches the edge
Let a fish swim across the stage. Every time it touches the edge, it changes colour.
when green flag clicked
forever
move 10 steps
if <touching edge?> then
change color effect by 25
if on edge, bounce
Here touching edge? is a Sensing block and it is hexagon-shaped. change color effect by 25 is a Looks block. As long as the fish does not touch the edge, the steps inside "if" never run.
Example 2: Jump when a key is pressed
Let us make a cat jump with the space key.
when green flag clicked
forever
if <key [space] pressed?> then
change y by 50
wait 0.3 seconds
change y by -50
key [space] pressed? is again a Sensing block. The change y blocks are in the Motion category and move the sprite up and down.
Two paths: the "if ... then ... else" block
Sometimes "if the condition is true, do this" is not enough. We want to say, "if it is true do this, otherwise do something different." For that we use the "if ... then ... else" block. It has two mouths: the top part runs when the condition is true, and the bottom part runs when it is false.
Example 1: Follow the mouse or wait
when green flag clicked
forever
if <mouse down?> then
point towards mouse-pointer
move 5 steps
else
say "Click me!"
If the mouse is down, the sprite follows the pointer; if not, it says "Click me!" on the stage. mouse down? and point towards mouse-pointer sense where the mouse is.
Example 2: Touch the colour to win, otherwise keep going
In a maze, let the finish line be green.
when green flag clicked
forever
if <touching color [green]?> then
say "You win!"
stop [this sprite]
else
move with the arrow keys
The touching color [green]? block checks whether the sprite is touching the colour you chose. You pick the colour by clicking the small square on the block and sampling it from the stage. If it touches, the game ends; if not, the player keeps moving.
Meet the Sensing blocks
The Sensing category (light blue) is the program's "sense organs." The ones we use most:
touching ...?— Is the sprite touching another sprite, the mouse-pointer, or the edge?touching color ...?— Is the sprite touching a particular colour?mouse down?— Is the mouse button being pressed?key ... pressed?— Is a chosen key (space, arrow keys, letters) being pressed?
All of these are hexagon-shaped. Hexagon blocks ask a question, and the answer is either "true" or "false." That is why they fit exactly into the hexagon slot of the "if" block.
Tip: If you click a Sensing block on its own, Scratch shows you in a little bubble whether it is "true" or "false" right now. That is a handy way to test the blocks.
Mini practice
Build a "hot floor" game. The cat moves with the arrow keys. Draw a red zone on the floor. If the cat touches red, it says "Ouch!" and returns to its starting point.
Try these steps:
- Take the
when green flag clickedblock from Events. - Put a
forever(Control) block below it. - Inside, add four
if <key ... pressed?> thenblocks for the arrow keys, and give each one a movement in one direction. - Add one more
if ... then ... else: insideif <touching color [red]?> then, putsay "Ouch!"andgo to x: 0 y: 0. - Click the green flag and test by moving the cat onto the red.
Tip: Start with just one arrow key first. Once you see it work, add the others. Working in small pieces makes it easier to find mistakes.
Common mistakes
Not putting the "if" block inside a forever loop
The if block checks only once. If you want the sprite to keep watching its surroundings, you must put the "if" block inside a forever (Control) loop. Otherwise the program checks the condition once and forgets it.
Leaving the hexagon slot empty
If you do not place a Sensing block into the hexagon slot of if ... then, the condition is left undefined. The block expects a question whose answer is "yes/no."
Choosing the wrong colour
In the touching color ...? block you must sample the colour from the stage with the eyedropper, using the square on the block. If you set a colour by hand that only looks similar, it may not match the real colour on screen, and the block may never become "true."
Forgetting the "else" path
We said, "if it touches the door, stop." But what if it does not touch it? If you only think about the one case where the character should stop and leave the other case empty, the game may not flow the way you expect. Fill in the else path when it matters.
Safety note
Scratch runs entirely on screen, so there is no physical danger. Still, watch two things: when you sit at the screen for a long time, stand up every 20 minutes and rest your eyes. Also, when you share a project on the Scratch website, do not type personal details like your full name, school, or address; anyone can see your shared projects. If you are unsure, ask an adult.
Lesson summary
- A condition is a step that runs only when a certain situation is true; in Scratch we build it with the "if ... then" block in the Control category.
- The "if ... then ... else" block offers two paths: one thing when the condition is true, and something else when it is false.
- The hexagon-shaped blocks in the Sensing category (touching, touching colour, mouse down, key pressed) let the program ask a question and get an answer.
- We make a sprite react to its surroundings by placing Sensing blocks into the hexagon slot of the "if" block.
- For continuous checking, we usually place the "if" block inside a "forever" loop.
Check questions
- In Scratch, which category holds the block that lets a sprite make a decision?
- What kind of block do we place in the hexagon slot of the "if ... then" block?
- What is the correct way to choose the colour in the
touching color ...?block? - What is the difference between "if ... then" and "if ... then ... else"?
- We want a sprite to react every time it touches the edge. Inside which block should we put the "if" block so the check keeps repeating?
Answers
- The Control category (orange blocks). "if ... then" and "if ... then ... else" live here.
- A Sensing block, that is, a hexagon block whose answer is true/false (for example
touching edge?). - Click the small colour square on the block and sample the exact colour from the stage with the eyedropper. Setting the colour by hand may not match the colour on screen.
- "if ... then" runs only when the condition is true; if it is false, it does nothing. "if ... then ... else" also runs a separate step when the condition is false.
- Inside a "forever" (Control) loop. That way the program rechecks the condition on every frame.
Source and verification note
For “Conditions and Sensing”, verification focuses on whether the relationship between What is a condition? The "if ... then" block and Example 2: Jump when a key is pressed remains consistent across examples. Block names are kept consistent with the current core Scratch categories. Project behaviour should be tested separately for start-up, normal play, errors and restarting.
Next lesson
Loops