friends = hero.findFriends()
for friend in friends:
enemy = friend.findNearest(friend.findEnemies())
if not enemy:
hero.command(friend, "move", {'x': friend.pos.x + 24, 'y': friend.pos.y})
else:
hero.command(friend, "attack", enemy)
I know the for loop is going to loop over each friend. I also tried it this way:
friends = hero.findFriends()
for friend in friends:
enemy = friend.findNearest(friend.findEnemies())
if enemy:
hero.command(friend, "attack", enemy)
else:
hero.command(friend, "move", {'x': friend.pos.x + 24, 'y': friend.pos.y})
In each case all of my units move 24 units to the right and then the top units stand there and get attacked. I’m not sure what to do and unfortunately I’m really lost. I can’t figure out exactly what it’s asking me and the hints aren’t helping. Please advise.
That’s what I just did. I saw you adjust your earlier comment. That seems to be working but it’s being really laggy. Well, some troops died, but the while True loop fixed it. Thank you so much. I feel like I’m losing my mind today and this level was just the icing on the cake.
I’m having trouble with this level. My troops move right, but when they see an enemy they don’t attack and actually move left. Here is my code:
while True:
friends = hero.findFriends()
enemy = hero.findNearestEnemy()
# Use for-loop and for each friend:
for friend in friends:
# If they see an enemy then command to attack.
if enemy:
hero.command(friend, "attack", enemy)
else:
hero.command(friend, "move", {'x': friend.pos.x + 0.15, 'y': friend.pos.y})
# Command to move east by small steps.
All your warriors are attacking the enemy that’s closer to the hero - Pinakin. Every warrior must attack the enemy next to him. The first poster - Ian_Lambert-BrownBrown has done it right, ( I have put hero.say(enemy.id) for debug
It says it’s an error because you put the definition of enemy = friend.findNearestEnemy() before you defined friend in the for loop. Put the enemy definition inside the for loop.
# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
while True:
friends = hero.findFriends()
# Use for-loop and for each friend:
for friend in friends:
# If they see an enemy then command to attack.
enemy = friend.findEnemies(friend.findNearest())
if enemy:
hero.command(friend, "attack", enemy)
# Command to move east by small steps.
else:
hero.command(friend, "move", {"x": friend.pos.x + 5, "y": friend.pos.y})