Operation Manual
bottom, and it’s shaped like a diamond—the same shape used for a decision point in a flowchart. That’s no accident: the
majority of the Sensing blocks need to be embedded in a Control block in order to operate.
Switch the Blocks pallete to Control mode, and look for the if block—it’s shaped like a squished and bumpy letter C. Note
that the if block has a diamond-shaped indentation—the same shape as the touching Sprite1? block. Drag the if block
onto the Scripts pane, and then drag the touching Sprite1? block onto its diamond-shaped indentation. The result is a two-
coloured block that now reads if touching Sprite1?.
This represents an if conditional in the program: when this point is reached, any code located within its confines will be executed
if and only if the condition is met. In this case, the condition is that Sprite2 is being touched by Sprite1. With the use of and, or
and not logic blocks from the Operators block palette, some quite complex scenarios can be accommodated.
From the Looks block palette, drag a say Hello! For 2 secs block into the centre of the if touching Sprite1?
conditional. Change the text to read Don’t eat me!, and then add a wait 1 secs Control block, changing the value to 2.
Add a when space key pressed block to the top, changing the value to read when right arrow key pressed. Finally,
drag a hide block from the Looks palette to the bottom of the loop to end up with the block list that’s shown in Figure 10-9.
Figure 10-9: Controlling the Cheesy-Puffs with a Scratch if block
Boolean Logic
Named for George Boole, Boolean logic or Boolean algebra is a key concept to understanding how computers work. In Scratch, Boolean logic is implemented
in three Operators bricks: and, or and not.
The and operator requires that two inputs—in Scratch’s case, Sensing blocks—are both true before its own output will be true. If neither of its inputs is true,
or if both inputs are false, its own output will be false. You can use this operator to check to see if a sprite is touching two other sprites, as an example.
The or operator requires that one or the other of its two inputs are true. If either input is true, the operator’s output will also be true. This is a handy way of
re-using code: if you have multiple sprites that are lethal to the player sprite, a single block of code can be used with the or operator to trigger when any of
the enemy sprites are touched.
Finally, the not operator is known as an inverter: whatever its single input is, it outputs the opposite. If its input is false, then the output is true; if the input
is true, then the output is false.
Double-click the cat sprite on the stage to return to editing its scripts. The script you created for the Cheesy-Puffs sprite will
disappear, but don’t worry—it’s still saved but only appears when you’re editing that particular sprite.