Hey, I’m having trouble understanding how this code works.
I’ve completed the level (and the one before obviously which is similar).
But what is the code actually doing and how does it slow your character’s speed down?
I just don’t get it. Is (n) the character’s speed or time? something else completely?
The part I’m having the most trouble with is the while true loop at the bottom. I just don’t get what it is actually doing and how it affects my character and his speed.
Please simplify as I’m completely new to this.
Thanks in advance.
(Edited as requested for formatting)
# Escape from Death Valley!
# Move by with a zigzag pattern using real modulo functions.
# This function returns a value from 0 to 15:
while n >= 15:
n -= 15
# This function should return a value from 0 to 9:
# While n is greater or equal to 9, subtract 9 from n:
while n >= 9:
n -= 9
# Don't change the following code:
time = hero.time
if time < 30:
y = 10 + 3 * mod15(time)
y = 20 + 3 * mod9(time)
x = 10 + time
Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!
@Psyko2k please format your code correctly using this button:
@Psyko2k thanks for trying to understand as best you can. For this level, I don’t think you’re really supposed to understand it. It just shows you what you can do later with what you’re currently learning. So keep up the good work and you’ll get there!
Good question - it’s great that you’re trying to understand why code works .
With the functions, ‘n’ is whatever you set it to be. In this case we are calling the function with
so yes, here n is time (ie number of seconds).
Have a look at what is happening in the while True loop, and go through each step:
At time = 1, then y = 10 + (3 x 1) = 13 and x = 11. The hero moves to (11, 13).
At time = 2, then y = 10 + (3 x 2) = 16 and x = 12. The hero moves to (12, 16).
At time = 3, then y = 10 + (3 x 3) = 19 and x = 13. The hero moves to (13, 19).
At time = 14, then y = 10 + (3 x 14) = 52 and x = 24. The hero moves to (24, 52).
At time = 15, then y = 10 + (3 x 0) = 10 and x = 25. The modulo function sets the hero’s y position back to the bottom of the screen. The hero moves to (25, 0). This takes a bit of time which is why the next few points are quite spread out as the computer waits for the hero to get there before doing the next loop.
The hero moves slowly because you’re telling them to move a tiny bit at a time - each move is really small, and then there’s a little pause as time increases. (The computer is calculating in smaller units than each second, so we don’t see these pauses).
Feel free to post with more questions if you want more of an explanation. If you haven’t used graphs before, then have a play around with something like Equation Grapher - start by putting in y=2x-2, and then change the equation to see what effect it has.