Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Need help on sand snakes

This field is covered in firetraps. Thankfully we’ve sent a scout ahead to find a path. He left coins along the path so that if we always stick to the nearest coin, we’ll avoid the traps.

This canyon seems to interfere with your findNearest glasses!

You’ll need to find the nearest coins on your own.

items = hero.findItems()
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)
    coinIndex += 1
    distance = hero.distanceTo(coin)
    # If this coin's distance is less than the nearestDistance
    if "Gold Coin".distance < nearestDistance:
        # Set nearest to coin
        coin = coin
        # Set nearestDistance to distance
        Distance = 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.

hero.moveXY(coin.pos.x, coin.pos.y)

Welcome back @pugster, can you please tell us if an error is appearing and what is it, or if you have a different problem?

You need to define coins and coinIndex.

They are actually outside the formatted text

the problem is probably here because you should change “Gold Coin” to coin.