Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Hunting Party - stuck! (Python)


#1

Okay so here is my code

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.


#2

From the hints:
HuntParty01

24 is WAY too much. Try putting a decimal point in front of that value. Then keep adjusting the number until it works.

BTW, your second code post should work, IF you put the whole thing (all of it) in a loop.


#3

All that happens when I change it is they move forward less and then stop.


#4

it stops because it’s not looping through the code. There is no loop. Try while True for the whole thing.


#5

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.


#6

now adjust that numeric value until it works and no one dies so you get the bonus.


#7

What do you want? Mk see ya.


#8

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.

#9

I don’t see nothing wrong with your code. Increase the value to 0.3 or 0.45 and submit several times.


#10

I tried changing the number, but the troops still don’t attack.


#11

OMG :slight_smile: Didn’t see that!
You have:

while True:
    friends = hero.findFriends()
    enemy = hero.findNearestEnemy()
   hero.say(enemy.id)
    # and so on... 


Do you see the one on the top right corner :slight_smile:
put:

    for friend in friends:
        # your friend must find the enemy

#12

What? I don’t really understand.


#13

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


#14

What item allows “friend.findNearest(friend.findEnemies())” because I don’t have that, or is it already allowed?


#15

This is same as friend.findNearestEnemy()


#16

It says it’s an error.


#17

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.


#18

Thanks for the advice! It works fine now.