Conditional Statements

Conditional statements ask questions about the program state to choose from a set of different sequences of commands. In Scratch for example, you can determine whether you are at the edge of the stage with the if touching edge control block. The following example (implemented within a forever loop on the shark in the conditional project) causes the sprite to think each time it reaches the edge of the stage before bouncing off again:

Conditional statements have slots that are shaped with points on either side which evaluate to a true or a false value and execute if the statement is true. They are found in the controls programming blocks and are used for program flow with if, repeat, forever, and wait blocks. The conditional part of the statement is found amongst the numbers and the sensing areas.

A predator-prey relationship common in many games can be built using the touch sensors and conditional statements so that they prey is killed when touched by the predator. There seems to be no command to remove a sprite from an application whilst it is running so the following sequence is used to remove a sprite once it has been touched by the predator sprite. The sequence stamps the sprite image onto the stage, shrinks and hides the sprite and moves it into a corner remembering to stop the forever loop (there may be a better way to perform this operation).

The example conditional project implements this on the fish sprite with a corresponding change of costume on the shark so it looks like the shark eats the fish when they touch:

Pressing the green flag will need to restore the fish sprite back to life by showing the sprite, setting the initial position and size and starting the forever loop:

19/03/2024 06:38:01