Also when my archers kill the witch they just stand still how can i fix that?
Tell them to move if witch is dead
i need help with my code:
def move():
for enemy in hero.findEnemies():
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult:
hero.attack(catapult)
else:
hero.move({'x': hero.pos.x + 2, 'y': hero.pos.y})
def commandfriend(friend):
for paladin in paladins:
hero.command(paladin, "move",{'x': 31, 'y': 58})
hero.command(paladin, "move",{'x': 50, 'y': 58})
hero.command(paladin, "move",{'x': 50, 'y': 38})
hero.command(paladin, "move",{'x': 78, 'y': 40})
while True:
paladins = hero.findByType("paladin")
for paladin in paladins:
commandfriend(paladin)
move()
i donāt know what to do
and i am confused completely
my main target is to get one of the paladins out of there, but then they start walking into the wall
The reason this isnāt working is because the move command for friends is like hero.move() not hero.moveXY(), it has to be repeated to work. Each move command lasts a very small amount of time. You have to use something like:
if hero.distanceTo({"x": 10, "y": 10}) > 2:
hero.command....
elif hero.dist...
Do that for each move command and your paladin will move, but youāll still need to to some more work before everything works.
sorry, i donāt understand
Okay. Lets just say that putting move commands one after the other like:
does not work. It makes the paladin move to the last coordinate in the list (78, 40).
What you have to do, is make sure the paladin is at the right place, before she moves on to the next location.
So you want the paladin to move to {'x': 31, 'y': 58}
first, right?
To make sure the paladin actually moves there, instead of going to 78 40 first, you have to use an if statement to check that the paladin has arrived at (31, 58), before you move to the next place.
That if statement would be:
if paladin.distanceTo({"x": 31, "y": 58}) > 2: # if the paladin is further than 2 meters from her destination.
hero.command(paladin, "move", {"x": 31, "y": 58})
elif paladin.distanceTo({"x": 50, "y": 58}) > 2:
hero.command(paladin, "move", {"x": 50, "y": 58})
The first if statement makes sure the paladin has reached (31, 58), before it goes onto the elif. Remember: elif means else if, so the condition of the if statement above the elif must be false for it to run. This means that the paladin will only move to (50, 58) when it has already reached (31, 58).
Now repeat the pattern of elifs with the other two destinations.
Danny
sorry, i donāt understand fully, but here is my new code:
def move():
hero.move({'x': hero.pos.x + 1.5, 'y': hero.pos.y})
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult and hero.distanceTo(catapult) < 3:
hero.attack(catapult)
def commandfriend():
friend = hero.findNearest(hero.findByType("soldier"))
if friend:
if friend.distanceTo({'x': 17, 'y': 58}) > 0.5:
hero.command(friend,"move",{'x': 17, 'y': 58})
elif friend.distanceTo({'x': 50, 'y': 58}) < 0.5:
hero.command(friend, "move",{'x': 50, 'y': 58})
elif friend.distanceTo({'x': 50, 'y': 38}) < 0.5:
hero.command(friend, "move",{'x': 50, 'y': 38})
elif friend.distanceTo({'x': 78, 'y': 40}) < 0.5:
hero.command(friend, "move",{'x': 78, 'y': 40})
hero.move({'x': hero.pos.x, 'y': hero.pos.y + 2})
while True:
commandfriend()
move()
i make two soldiers go to (17, 58)
my little tweaks to the code made the witch go straight to the bear trap for some reason
def move():
hero.move({'x': hero.pos.x + 1.5, 'y': hero.pos.y})
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult and hero.distanceTo(catapult) < 3:
hero.attack(catapult)
def commandfriend():
friends = hero.findFriends()
for friend in friends:
if friend.distanceTo({'x': 17, 'y': 58}) > 0.5:
hero.command(friend,"move",{'x': 17, 'y': 58})
elif friend.distanceTo({'x': 50, 'y': 58}) < 0.5:
hero.command(friend, "move",{'x': 50, 'y': 58})
elif friend.distanceTo({'x': 50, 'y': 38}) < 0.5:
hero.command(friend, "move",{'x': 50, 'y': 38})
elif friend.distanceTo({'x': 78, 'y': 40}) < 0.5:
hero.command(friend, "move",{'x': 78, 'y': 40})
hero.move({'x': hero.pos.x, 'y': hero.pos.y + 2})
while True:
commandfriend()
move()
now my paladin almost escapes but dies close to the exit
def move():
hero.move({'x': hero.pos.x + 1.5, 'y': hero.pos.y})
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult and hero.distanceTo(catapult) < 3:
hero.attack(catapult)
def commandfriend():
friends = hero.findFriends()
for friend in friends:
witches = hero.findByType("witch")
for witch in witches:
hero.command(friend, "move", witch.pos)
hero.move({'x': hero.pos.x, 'y': hero.pos.y + 2})
while True:
commandfriend()
move()
now i am getting errors:
def move():
hero.move({'x': hero.pos.x + 1.5, 'y': hero.pos.y})
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult and hero.distanceTo(catapult) < 3:
hero.attack(catapult)
def commandfriend():
friends = hero.findByType("archer")
sold = hero.findByType("soldier")
pals = hero.findByType("paladin")
for friend in friends:
witches = hero.findByType("witch")
for witch in witches:
hero.command(friend, "attack", witch)
for so in sold:
enemy = so.findNearestEnemy
if enemy:
hero.command(so, "attack", enemy)
for pal in pals:
if pal.canCast("heal", pal):
hero.command(pal, "cast", "heal", pal)
hero.move({'x': hero.pos.x, 'y': hero.pos.y + 2})
while True:
commandfriend()
move()
What errors are you getting?
sorry, i am now just fixing some parts of my code, iāll let you know if there is problems
i am just trying to get my friends to the cross, but they go to the second position instead
def move():
hero.moveXY(hero.pos.x + 40, hero.pos.y)
hero.moveXY(hero.pos.x - 40, hero.pos.y)
safe = commandfriend()
if safe:
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult:
hero.attack(catapult)
def commandfriend():
friends = hero.findByType("archer")
for friend in friends:
target = {'x': 16, 'y': 58}
hero.command(friend, "move", {'x': 16, 'y': 58})
hero.command(friend, "move", {'x': 50, 'y': 58})
return target
hero.move({'x': hero.pos.x, 'y': hero.pos.y + 2})
while True:
commandfriend()
move()
def move():
hero.moveXY(hero.pos.x + 40, hero.pos.y)
hero.moveXY(hero.pos.x - 40, hero.pos.y)
safe = commandfriend()
if safe:
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult:
hero.attack(catapult)
def commandfriend():
friends = hero.findByType("archer")
for friend in friends:
target = {'x': 16, 'y': 58}
if friend.distanceTo({'x': 16, 'y': 58}) > 0.001:
hero.command(friend, "move", {'x': 16, 'y': 58})
elif friend.distanceTo({'x': 50, 'y': 58}) > 0.001:
hero.command(friend, "move", {'x': 50, 'y': 58})
elif friend.distanceTo({'x': 50, 'y': 40}) > 0.001:
hero.command(friend, "move", {'x': 50, 'y': 40})
elif friend.distanceTo({'x': 78, 'y': 40}) > 0.001:
hero.command(friend, "move", {'x': 78, 'y': 40})
return target
hero.move({'x': hero.pos.x, 'y': hero.pos.y + 2})
while True:
commandfriend()
move()
now my archers keep going to the same spot
or should i use while instead of if @Rachel699 ?
now it says infinite loop:
def move():
hero.moveXY(hero.pos.x + 40, hero.pos.y)
hero.moveXY(hero.pos.x - 40, hero.pos.y)
safe = commandfriend()
if safe:
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult:
hero.attack(catapult)
def commandfriend():
friends = hero.findByType("archer")
for friend in friends:
target = {'x': 16, 'y': 58}
while friend.distanceTo({'x': 16, 'y': 58}) > 0.001:
hero.command(friend, "move", {'x': 16, 'y': 58})
while friend.distanceTo({'x': 50, 'y': 58}) > 0.001:
hero.command(friend, "move", {'x': 50, 'y': 58})
while friend.distanceTo({'x': 50, 'y': 40}) > 0.001:
hero.command(friend, "move", {'x': 50, 'y': 40})
while friend.distanceTo({'x': 78, 'y': 40}) > 0.001:
hero.command(friend, "move", {'x': 78, 'y': 40})
return target
hero.move({'x': hero.pos.x, 'y': hero.pos.y + 2})
while True:
commandfriend()
move()
i now have the paladin going straight to the last target despite using the advice or maybe there is a problem:
def move():
hero.moveXY(hero.pos.x + 40, hero.pos.y)
hero.moveXY(hero.pos.x - 40, hero.pos.y)
safe = commandfriend()
if safe:
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult:
hero.attack(catapult)
def commandfriend():
friends = hero.findByType("paladin")
for friend in friends:
target = {'x': 78, 'y': 40}
if friend.distanceTo({'x': 16, 'y': 58}) > 2:
hero.command(friend, "move", {'x': 16, 'y': 58})
if friend.distanceTo({'x': 50, 'y': 58}) > 2:
hero.command(friend, "move", {'x': 50, 'y': 58})
if friend.distanceTo({'x': 50, 'y': 40}) > 2:
hero.command(friend, "move", {'x': 50, 'y': 40})
if friend.distanceTo({'x': 78, 'y': 40}) > 2:
hero.command(friend, "move", {'x': 78, 'y': 40})
return target
hero.move({'x': hero.pos.x, 'y': hero.pos.y + 2})
while True:
commandfriend()
move()
i realised the mistakes so i changed them but the paladin still dies:
def move():
hero.moveXY(hero.pos.x + 40, hero.pos.y)
hero.moveXY(hero.pos.x - 40, hero.pos.y)
safe = commandfriend()
if safe:
catapult = hero.findNearest(hero.findByType("catapult"))
if catapult:
hero.attack(catapult)
def commandfriend():
friends = hero.findByType("paladin")
for friend in friends:
target = {'x': 78, 'y': 40}
if friend.distanceTo({'x': 16, 'y': 58}) > 0.001:
hero.command(friend, "move", {'x': 16, 'y': 58})
elif friend.distanceTo({'x': 50, 'y': 58}) > 0.001:
hero.command(friend, "move", {'x': 50, 'y': 58})
elif friend.distanceTo({'x': 50, 'y': 40}) > 0.001:
hero.command(friend, "move", {'x': 50, 'y': 40})
elif friend.distanceTo({'x': 78, 'y': 40}) > 0.001:
hero.command(friend, "move", {'x': 78, 'y': 40})
return target
hero.move({'x': hero.pos.x, 'y': hero.pos.y + 2})
while True:
commandfriend()
move()
I think the problem might be here. If you use too small a distance the code thinks that the paladin has never reached the correct location. Could you try > 2?
ok, iāll try that now.
my paladin still dies out
My paladin first goes to the first destination, but then continues walking into the wall for a while, then goes to the second destination, but then dies
First quick look, no testing - I think it wonāt work.
One possible way to do this: [Solved] Chained statements like .moveXY() and behavior override each other? - #32 by xython
@Flamekey_42 better read the whole linked post and then see the short animated gif posted here
it almost worked, it is just that the paladin went back to the first destination after arriving at the second