I’ve been having some issues with the self.now function in this level, here’s my code:
loop:
items = self.findItems()
while self.now() < 20:
for item in items:
pos = item.pos
x = pos.x
y = pos.y
self.moveXY(x, y)
while self.now() > 20:
self.moveXY(14, 37)
self.buildXY("fence", 21, 37)
I’ve been trying to simplify it as much as possible / not worrying abot the gold, but whatever I do my char nevers breaks out of the first while loop, even after the now function returns a time greater than 20.
You have a loop in a loop. Your for loop will go to every item in items even if the timer on while has expired. A while loop doesn’t end a process in it if the condition is False; it just doesn’t repeat.
Just axe the for loop and keep the while
(you will have to edit your code to make this work)
The level will also work better if you use self.move(item.pos)
My char just sits there, waiting for the orges to come screaming down on em. What is the array coinIndex doing that allows the code to cycle through the coin poisitions?
Without defining coinIndex the value of coinIndex will be undefined and coins[coinIndex] will be undefined too, as the code will try to look up the element in coins with the index of undefined.
Dont you receive some error message when you use the broken code? At least coin.pos should produce an error.
When you remove the array, where does the variable coin receive his value from? If there is no one assign it a value, then coin will hold the value of undefined, or whatever has been assigned to it before.
Just causes the char to walk in a straight line to the edge of the map. Why? Why isn’t the char moving to the position of the nearest coin, which has been assigned in the find items etc?
I am not sure what the Python to JS transpiller generates here, but my guess is that coin.pos will be interpreted as a vector to {0, 0} when coin is undefined. I am only guessing because I work mainly with JS.
But to your second question, replace coin with item inside of self.move, then it might just walk to the nearest coin. You already search for the nearest item by doing:
If I remove the coin var then my char collects items, but for some reason just stands still once their out of the first while loop and doesn’t move on to the second (to escape and build the fence):
Appreciate you trying to help and sorry if it seems like I’m trying to be difficult, I’m just trying to understand the why of it, instead of just blindly typing it in, if that makes sense.
You leave the first while loop when self.now is >= 20 which may be exactly 20. The programm will then step down and check the condition of the second while loop. Which may be false because the time is exactly 20 and with that its not greater 20. So the programm execution will skip your while loop.
You can completely remove the second while and just call the moveXY and buildXY methods. As the program execution will remain in the first while loop until 20 seconds are reached.
Even if your time is greater than 20 seconds when the programm execution checks the second while loop. The while loop will be an endless loop as the timer will never be below 20 seconds after this point (except for when the value overflows, but thats far after your life time)
If I change the loops to end at < 19 and start at > 20 , without the array I’m still having the same issue.
However if I un-comment the array even when they both end / start at 20 it works fine.
Edit: aware of the <= and >=, but my question is still about the array. Following Marrk’s suggestion works perfectly, but I’m still trying to understand how it is that the array is allowing the original code:
Then my characater will just run straight to the left of the screen and keep going. My question(s) is / are why does the array allow the code to work? What is the array doing?