Can't figure out what's wrong

Well, this lvl looked very simple to me, so I was doing a mockup that would pickup coins at least there was an enemy near me, in which case I would to to the teleporter, how ever, the hero seems to ignore the enemy instruction and just go and pickup coins, what’s wrong? :frowning:

loop:
index = 0
enemies = self.findEnemies()
enemy = enemies [index]
items = self.findItems()
coin = items[index]

if enemy and self.distanceTo(enemy) < 5:
    self.moveXY(7, 20)

elif coin:
    self.moveXY(coin.pos.x, coin.pos.y)

Your code will only have your hero move to (7, 20) if:

  • The enemy indexed as [0] (not necessarily the closest) is within 5

  • You aren’t moving towards a coin.

Is there a reason you aren’t using self.findNearest() or self.move() ?

No particular reason for no using nearest, just wanted to get better at the while function. Is there any reason to use self.move instead of self.move(x,y)?

Right there would be two ways to check to see any enemy is within 5:

  1. Go through the enemies one a time to see if any are within 5. This would require a loop that goes through each enemy to see if it is in 5. You would want to use a for loop for this (although you don’t have to)

  2. Only check to see if the nearest enemy is within 5 (because if the closest isn’t in 5, no enemy is within 5)

The second one is easier to code.

The difference between move() and moveXY() is that move only moves 1 step at a time and allows for other things to happen. moveXY() will move you to the point before other code will happen (like checking to see if enemies are close)