Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Zig zag and zoom level the function of (n) (Lua)


the game did not translate this level or the one before it into Lua

I was able to pass the last level by guessing how to translate java into lua ( something I am getting good at now lol) however I did not understand how it worked or what I really did and now I am lost with out a clue.

so here I am.

Can any one explain what (n) dose IE:

local mod30 = function(n) 
    if (n >= 30) then
        return n - 30
        return n

what dose any of that mean? and how did this

while (true) do
    local time = hero:now()
    local x = mod30(time) + 25
    local y = mod40(time) + 10
    hero.moveXY(x, y)

using that make my hero move slower as he walked and seemingly randomly generate a move pattren
(the (time) + 25 added on is what made my char move slower right?)


@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.