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

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.

1 Like

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

But how do I move away?

1 Like

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

I want to retreat if they get within 4 meters

1 Like

so use

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

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
1 Like

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

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

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