Note: If you don’t already have the zombie world from the previous section open, open it.
Walking to the end of the pier is nice, but we can now do a lot more to control our zombie’s movements based off the characteristics of the pier. Like what?
Well, what if we didn't just want the zombie to walk down a pier -- what if the zombie is supposed to “walk the plank.” (Alice didn't have a plank object -- so, hey, we picked the pier as the closest thing.) That is, we want him to walk a little ways “off” – then fall down into the water. To make it look good, let’s have him move say 0.1 meters past the end of the pier, then move down under the water (for now, let’s just have him move down 10 meters).
[Q7.3.1]: If you have a pier that is 3 meters long, how far should the zombie move forward? What if the pier is 5.7 meters long?
One way we could do this is to have 2 instructions – one to have the zombie move forward the depth of the pier and another one to move him the remaining 0.1 meters. But that will look a little jerky (he’ll move to the end of the pier, stop briefly, then move a bit more off the end. If you don’t believe us, give it a try). Instead, what we’d like to do is to have the computer calculate the full distance – the pier’s depth PLUS 0.1 meters – and have a single move forward command that has as its second parameter the complete “amount” (pier’s distance + 1).
Luckily, computers are BUILT for doing math. Here’s how you create a mathematical expression which the computer will calculate (add up) at the time the program is run (more specifically when that specific move instruction is executed). If you look carefully at the zombie move instruction on the purple tile where you are calling the function "pier’s depth", there’s a small down arrow on the right edge of the purple tile. Click on that, and in the drop down box you will see the word “math”. Hover your mouse over “math” and to the right you will see 4 possible options as shown below – one for each add, subtract, multiply, and divide. In this case we want to add 0.1 meters to the depth of the pier, so select + and then another menu will pop out on the right hand side. None of the numbers there are what you want, so select "other" and type in 0.1 on the calculator pad. When you hit enter, you should see a mathematical expression of (pier's depth + 1) indicated as the amount parameter to the move method.
Let’s see if it works. Press Play.
OK – he’s hanging out in the air. If you want, go ahead and add an instruction to make him fall down into the sea (all the way under).
What else can we do? Obviously we could calculate a number of expressions based on, for example, the depth of the pier. Something that could be useful is having him walk a specific amount down the pier.
Try to have him move 1/2 the way down the pier (hint: pier’s depth / 2).
Try to have him move 1/3 the way down the pier.
How about 2/3 the way down the pier (hint: it’s 1/3 of the way down the pier * 2).
[Q7.3.2]: This is weird, but imagine that before the zombie falls into the water he should spin around (turn). We could have him turn some specific number of times, but maybe he wants to spin around however many times he is “tall” in meters. So if he’s 1.5 meters tall, he spins around 1.5 times. Or if he’s 2 meters tall, he’ll spin around 2 times. What instruction would you create and where would you insert it into the code?