To make projects that react, we use conditionals together with sensing. A conditional lets a sprite decide what to do, and sensing lets it notice things around it.
The if then Block
The if then block (a Control block) runs the blocks inside it only if a condition is true. For example: if touching edge then turn 180 degrees. If the sprite is not touching the edge, the blocks inside are skipped.
The if then else Block
The if then else block gives two paths. If the condition is true, it runs the first group of blocks; if it is false, it runs the else group instead. This is like saying: if it is raining, take an umbrella, else wear a hat.
Sensing Blocks (light blue)
Sensing blocks check the world. They are often shaped like pointy hexagons, called boolean blocks, which report true or false. Common ones include:
- touching mouse-pointer? — is the sprite touching the pointer?
- touching edge? — has the sprite reached the edge?
- touching color? — is the sprite touching a chosen colour?
- key space pressed? — is a key being held down?
- mouse down? — is the mouse button pressed?
- ask and wait — asks the player a question and stores the reply in answer.
Putting Them Together
Sensing blocks fit into the hexagon slot of an if then block. A common pattern is a forever loop with an if then inside, so the sprite keeps checking a condition all the time.
Remember
- if then runs blocks only when a condition is true.
- if then else chooses between two paths.
- Sensing boolean blocks report true or false and fit inside the hexagon slot.