In this section we are going to explore how conditional loops (whiles) work. Download the world at the bottom of the page: Zombie-LunchLady World. We are going to do something slightly different from the video in 10.1, we are first going to build a game where the zombie chases the lunchLady and always catches her after his third move:
First we should understand what needs to happen in English:
The lunchLady turns left 1/4 revolution
The lunchLady walks 1 meters
The zombie turns to face the lunchLady
The zombie walks 1 meters towards the lunchLady
This should continue happening while the zombie is at least 1 meter from the lunchLady
When the zombie is no longer at least 1 meter from the lunchLady the lunchLady should say "Ah! A zombie is eating me!" and then she should disappear
It is important to first identify what part of the code needs to repeat until a certain condition is met. For example, the statements in blue should happen in order, over and over while the zombie is at least 1 meter away from the lunchLady**.
The lunchlady turns left 1/4 revolution
The lunchlady walks 1 meter
The zombie turns to face the lunchlady
The zombie walks 1 meter towards the lunchlady
This should continue happening while the zombie is at least 1 meter from the lunchLady
When the zombie is no longer 1 meter from the lunchlady the lunchlady should say "Ah! A zombie is eating me!" and then she should disappear
[Q10.5.1]: Will the zombie ever catch the lunchLady? Why or why not? In what case would he catch her?
We can say this differently:
While the zombie is at least 1 meter from the lunchLady:
The lunchLady turns left 1/4 revolution
The lunchLady walks 1 meter
The zombie turns to face the lunchLady
The zombie walks 1 meter towards the lunchLady
When the zombie is no longer at least 1 meter from the lunchLady
The lunchLady should say "Ah! A zombie is eating me!"
The lunchLady should disappear
Now let's build our Zombie Chasing Game. First bring a while into the method, you will have to choose a default value, just like if/else tiles:
We should first add the condition that, while it is true, the statements inside the while will continuously execute. This condition is: zombie is at least 1 meter from lunchLady. To add this condition, click on world in the object pane, then click on functions and find the > function and drag it into the conditional part of the while:
Pick some default values, then change it so that the loop executes while the zombie is at least 1 meter from the lunchLady.
Now we need to add the statements that should continuously execute while our condition (that the zombie is at least 1 meter from the lunchLady) holds.
Let's revisit our English description of what should happen:
While the zombie is at least 1 meter from the lunchLady:
The lunchLady turns left 1/4 revolution
The lunchLady walks 1 meter
The zombie turns to face the lunchLady
The zombie walks 1 meters towards the lunchLady
When the zombie is no longer 1 meter from the lunchLady
The lunchLady should say "Ah! A zombie is eating me!"
The lunchLady should disappear
We still have to do what is left in blue.
[Q10.5.2]: Where should we put the last two statements? While's don't have an else, so where would the two statements go? Why don't while tiles have an else?
We need to add the last two statements after the while. Like this:
When the first of the two statements we just added gets executed (when the lunchLady says "Ah! A zombie is eating me!") that means that the zombie is within 1 meter of the lunchLady, and so she is being eaten.
Click play. Did the code do what you expected it to do?
[Q10.5.3]: How come we don't have to tell the zombie and the lunchLady to stop moving when the zombie is no longer 1 meter from the lunchLady?
[Q10.5.4]: What is the difference between the code we have written here and replacing the while loop with a counted loop that loops 3 times? If there isn't a difference, why are we using a while loop?
[Q10.5.5]: How can we make this game more interesting by allowing the zombie to catch the lunchLady randomly instead of on the third move?
**You could also say that the statements in blue should happen in order, over and over until the zombie is within 1 meter of the lunchLady. This is correct, but when building while tiles we need to identify the condition that must be true every time you want the blue statements to execute, not true when you want the blue statements to stop executing. The condition in the while should become false when you want the blue statements to stop executing.