Sand snakes/ Python/ Stuck

my mistake add

under coin = coins[coinIndex]

Oh so like this?

    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = hero.distanceTo(coin)

yea.
thats it
20 charactersssss

1 Like

It made it a bit better but now my character is walking collecting 4 coins and dying

Can you post your code?

This is my New code

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.x) 

delete one indent
20 characters

found the mistake get rid of if coin: and move hero.moveXY(nearest.pos.x , nearest.pos.x) outside the while-loop.

where outside the loop?

this loop. It is called a while-loop

like this?

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.
hero.moveXY(nearest.pos.x , nearest.pos.x) 

I tried this and it didnā€™t work its still just collecting a few coins and killing themselves

no, move it outside this loop. Donā€™t move it outside of the while-true-loop

indent this by one
20 chars

It didnā€™t change anything
like this?

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.
    hero.moveXY(nearest.pos.x , nearest.pos.x) 

If there is a nearest, then move to it.

No need. I didnā€™t do it and it worked.

I tried this but all it did was make my character move stop move stop at the same places it was earlier still killing them

What is your current code and what is your equipment?

Programicon 3
boots of leaping
mahogany glasses
quartz sense stone
(misc armors and such)

and my code right now is

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.x)