Hello! Love CC so far, had a few stuff I could get by just using Google for explanation, but here’s something I don’t understand:
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 nearest:
hero.moveXY(nearest.pos.x, nearest.pos.y)
Just for info, I passed the level after fixing the last line’s indentation (the code above is prior to fixing). I don’t understand why is it that I must put that last if statement indented to the while loop, and not indented within the previous if statement? (My understanding for putting this if statement in the previous if statement is that the code can only check if there’s a nearest coin only if the previous if statement’s criteria is met)
I apologize if this question is too vague/intuitive, but any help or explanation would be really appreciated!