6: Exploration: Conditional Looping: Introducing Random

In this section we are going to explore how we can make the Zombie Chasing Game more interesting by allowing the zombie to catch the lunchLady at different points in the game each time you run it. If you don't remember the video of the zombie chasing the lunchLady from module 10.1, you might want to go back and watch that again.

To do this, we need to allow the lunchLady to:

    • Turn left a random amount instead of always turning left 1/4 revolution
    • Walk a random amount in that direction instead of always walking 1 meter

And we need the zombie to:

    • Turn towards the lunchLady, just like before
    • Walk a random amount towards her instead of always walking 1 meter

This way, the distance between the zombie and the lunchLady is always changing, and they aren't just going in a square.

Let's revisit what we did in 10.5:

While the zombie is at least 1 meter from the lunchLady:

    • 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

And our code looks like this:

Now we have to change our direction and distances to be random. Let's start with the direction. We don't want her to turn left 1/4 revolution every time, we want her to turn left some random amount between 0 and 1 revolution.

Click on World, click on Functions and drag Random Number onto the 1/4 revolutions in the lunchLady's turn method call:

Click on the arrow next to Random Number and choose a minimum of 0 and a maximum of 1:

Click play. Did this change how the game worked? Play the game a few times and see if the zombie ever catches the lunchLady after a different number of moves.

But this still isn't random enough, we still have the zombie catching the lunchLady pretty quickly. We can also have the lunchlady and the zombie move random amounts each time.

Replace the two 1 meter moves with random number meter moves where the lunchLady and zombie will move a random amount between 1 and 3 meters:

Now click play and see if this made the game more interesting. Is it finally doing what we wanted it to? Does the zombie catch the lunchLady more randomly now?

Some extra refining:

[Q10.6.1]: How can you make the game more interesting given you can control the randomness of the zombie and the lunchLady?

[Q10.6.2]: When we play the chasing game, the zombie only catches the lunchLady if he is within 1 meter of her after she runs AND he runs. Can you change the code to allow him to catch her if he is within 1 meter of her after she runs but before he runs?

Make the world as described in Q10.6.2 and save it as:

Ch10-pg6-Zombie-LunchLady-YourName.a2w

<< Back Next >>