[SOLVED] Sand Snakes


#1

I have been stuck on this level for a while can you please help me with my code this is it


#2

You didn’t define any distance variable or any of the variables. You have to define all of them in order for them to work


#3

codeproblem


#4

This is my new code I tried.


#5

loop:
coins = self.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 = self.distanceTo(coin)
# If this coin’s distance is less
if self.distanceTo(coin) < 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:
        self.moveXY(nearest.pos.x, nearest.pos.y)

#6

i will summon you

@Chaboi_3000


#7

I can’t really see the code unless you preformat the text using three back ticks [```]


#8

Be sure to copy your code from the game and use the </> button or it won’t format properly. There are many people here willing and able to help. If you use the </> button correctly, your code should look like this:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.say("My code is formatted properly")

If it doesn’t look like the code above, you’re doing it wrong and we can’t see the structure of the code to troubleshoot whether or not that is the issue. Use the </> button and help us help you. This works when sending a private message as well. Thank you and welcome to the board.


#9

</> loop:
coins = self.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 = self.distanceTo(coin)
# If this coin’s distance is less
if self.distanceTo(coin) < 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:
        self.moveXY(nearest.pos.x, nearest.pos.y)

#10
loop:
    coins = self.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 = self.distanceTo(coin)
        # If this coin's distance is less
        if self.distanceTo(coin) < 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:
            self.moveXY(nearest.pos.x, nearest.pos.y)


#11

Is that good? :ok_hand:


#12

Please reply as soon as possible i’m coding right now.


#13

@Chaboi_3000 SUMMONED


#14

I think that’s good to me, though. Does it work?


#15

No, my character just runs into the mine. I think there is a bug because my character stops in place then goes into the mine.


#16


#17

@MunkeyShynes @Chaboi_3000.


#18

I just used the flags and won.


#19

I’m trying to pass the level how it’s used. If you want you can use flags. ;-;


#20

I still beat it without flags.