# I am trying to create a program to maintain a distance of between 4 and 6 meters, so that I can attack with the 7 meter sword without retaliation

#1

Here is what I have:

while True:
enemy = hero.findNearestEnemy()
posx = enemy.pos.x
posy = enemy.pos.y
mey = hero.pos.y
mex = hero.pos.x
if posx < mex-5 and posx > mex-7:
hero.moveXY(mex + 2, hero.pos.y)
elif posx > mex+5 and posx < mex+7:
hero.moveXY(mex-2, mey)
elif posy > mey+5 and posy < mex+7:
hero.moveXY(mex, mey-2)
elif posy < mey-5 and posy > mex-7:
hero.moveXY(mex, mey+2)
else:
hero.attack(enemy)

Is this the best way to do this? Currently it doesn’t even work, just attack them always.

#2

this won’t even work, an easier way is to check the distance to enemy using hero.distanceTo(enemy) and then see if it is less than 7 or not

#3

But how do I move away?

#4

when do you want exactly to move, do you want to move when an enemy is far away, or do you want to run away when an enemy is so near

#5

I want to retreat if they get within 4 meters

#6

so use

``````var enemy = hero.findNearestEnemy();
if(hero.distanceto(enemy)<=4){
hero.moveXY(0,0);//replace this with your code to retreat
}
``````

#7

He needs python version, which would be

``````enemy = hero.findNearestEnemy();
if hero.distanceTo(enemy) <= 4:
hero.moveXY(x, y) #replace this with your code to retreat
``````

#8

sorry i didn’t notice that he uses python, thanks for the correction

#9

Yes I know how to check distance but how do I retreat?

#10

just move somewhere else, for example 5 meters away from the ogre so that you can attack him