My character keeps moving to the first coin then walking into the mines and im not sure whats wrong
# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
while True:
coins = hero.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999
# Loop through all the coins to find the nearest one.
if coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = hero.distanceTo(coin)
# If this coin's distance is less than the nearestDistance
if distance < nearestDistance:
# Set nearest to coin
nearest = coin
# Set nearestDistance to distance
nearestDistance = distance
# If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if coin:
hero.moveXY(nearest.pos.x , nearest.pos.y)
also i am aware that all i need is a flag and i can win but i would prefer to learn the full lesson
Okay, so I could not figure out the code when I was completing the level myself, so I just used flags. You should not do that. That’s just abusing the level. Check if all of your code is indented properly
while True:
coins = hero.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999
# Loop through all the coins to find the nearest one.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = hero.distanceTo(coin)
# If this coin's distance is less than the nearestDistance
if distance < nearestDistance:
# Set nearest to coin
nearest = coin
# Set nearestDistance to distance
nearestDistance = distance
# If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if coin:
hero.moveXY(nearest.pos.x , nearest.pos.y)
So, I would advise checking over your code to see if everything is indented. I have not figured to answer out yet, because I used flags earlier. @Chaboi_3000 just saying, you may want to restrict flags in this level
I changed it back to while but it still isn’t working did I do it right
while True:
coins = hero.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999
# Loop through all the coins to find the nearest one.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = hero.distanceTo(coin)
# If this coin's distance is less than the nearestDistance
if distance < nearestDistance:
# Set nearest to coin
nearest = coin
# Set nearestDistance to distance
nearestDistance = distance
# If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if coin:
hero.moveXY(nearest.pos.x , nearest.pos.y)
Like this? Because now my character is just moving a bit then stopping infinity not collecting a single coin
while True:
coins = hero.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999
# Loop through all the coins to find the nearest one.
while coinIndex < len(coins):
coin = coins[coinIndex]
distance = hero.distanceTo(coin)
# If this coin's distance is less than the nearestDistance
if distance < nearestDistance:
# Set nearest to coin
nearest = coin
# Set nearestDistance to distance
nearestDistance = distance
# If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if coin:
hero.moveXY(nearest.pos.x , nearest.pos.x)
coinIndex += 1