[SOLVED] Sand Snakes


#21

@Ironhead All Gamestack is saying is that he wants to learn the point of the lesson. There are many levels that you can just beat with flags, however the point of the lesson is missed.

@Gamestack - the code I can see looks fine, although you define distance in line 10 but don’t use the variable in line 12. I can’t see if it’s there, but you need an “if nearest” conditional inside your loop to tell your guy to move to the nearest coin. The comments on line 18 are the instructions for the remaining code.


#22
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
        while coinIndex < len(coins):
            # 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)


#23

That’s my new code. ;-;


#24


#25

He walks in place without going to the coins.


#26

@Chaboi_3000 summoned


#27

@MunkeyShynes :smiley:


#28

Help please. :smile:


#29

You have not defined nearest.


#30

You said that nearest equals none, yet you call on it when you want to move to the nearest coin, when nearest does not have a value, as u defined it.


#31
loop:
    coins = self.findItems()
    coinIndex = 0
    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
        while coinIndex < len(coins):
            # 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)


#32

My character isn’t moving. :disappointed_relieved:


#33

Help :smiley: please


#34

The two indicated lines are identical. The second one is wrong and doesn’t belong there.

In the first one, you are iterating through the array of coins and incrementing by one. You then define distance but never use this variable.

In the second line, you should have a conditional statement comparing each coin’s distance to nearestDistance to see if it’s less than nearestDistance. You then properly redefine nearestDistance as distance.

Other than the line where you see the second red arrow, your code is fine.


#35

There’s also a mistake in your logic. :slight_smile: You need two separate steps:

  1. Loop through all the coins to find the nearest
  2. THEN move to the nearest

#36

38%20AM


#37

:smiley::smiley::smiley::smiley:


#39

Please post in the correct topic please @Naimaa1


#40

Sorry, I had two tabs open on two different discussions and accidentally posted on the wrong one!:weary:


#41

I made a post here