@LordMaddog So it took me some time to really think about this one. I will be honest, I just passed it and didn’t think further about it.
Question 1: Do you understand the concept of modulo ? (or mod )
If we have the number 7 and divide it by 3 we will get 2 with a remainder of 1 because 3 + 3 + 1 = 7 right?
All modulo does is to ask what is the remainder after I divide through by a number.
Question 2: Were you asking about your special case of “if
” or the codes actual case of using the while
loop?
Explanation of the code:
In the code the modulo is used to define an interval to diagonally walk up and to the right. Once you hit a maximum then it will reset and go back to 0. Thus the hero walks down.
So in the beginning from time 0 to 30 seconds, the hero has an interval of 15 seconds. Or mod15()
, once the time is over fifteen seconds, 15 is then subtracted from its total and the Y value (up and down) goes back to 0. Or in this case 10 as ten was being added to the Y value to give an offset above the rocks below.
At time 0, Y is 10 right? At time 15, Y is also 10 right? Again if we take time and divide by 15 and ask what is the remainder, there is no remainder at a time of 0 and a time of 15. When the time is at 1 second or 16 seconds we have a remainder of 1 right?
Thus the Y value can increment upwards.
In this clever way the distance of the vertical walking lines were reset every so often so that the hero didn’t bump into the rocks above or below. That is why the code uses mod9 after 30 seconds. The rocks above and below are closer, thus making for a need to reset the Y coordinate sooner.
They could have illustrated this further by making an even more narrow gap and using a smaller modulo like mod5()
The mod15()
function just subtracts 15 from the number as many times as possible, leaving you with a remainder.
-HW