Try this, instead of just using the
if enemy: conditional, try putting
if enemy and hero.distanceTo(enemy) < 12:. That way, when the yaks get closer than the given distance, it triggers the while True loop to iterate through the if conditional at a specific event. Also, the code isn’t using the reassigned values of yPos in the conditional statements. However, if you change
yPos - 3 and
yPos + 3 to
hero.moveXY(xPos, yPos - 3) and
hero.moveXY(xPos, yPos + 3) respectively, the hero then moves properly.
You may have to adjust the numbers a little bit (I did) but with these changes you should see the correct movement of the hero.