Hunting Party Help please


#1

Hello, I’m stuck on the writing party level! I can’t figure out how to make all of my soldiers and archers move right and attack at the same time?

Here’s my code:

# Command your troops to move east and attack any ogres they see.
# Use for-loops and findFriends.
# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
loop:
    friends = self.findFriends()
    enemies = friend.findEnemies()
    enemy = friend.findNearest(enemies)
    pos = friend.pos
    newX = pos.x + 10
    newY = pos.y
    if enemy:
        self.command(friends, "attack", enemy)
    else:
        self.command(friends, "move", {"x": newX, "y": newY})

But when I try this it says “Hero placeholder needs something to command”.

But when I try this code:

# Command your troops to move east and attack any ogres they see.
# Use for-loops and findFriends.
# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
loop:
    friends = self.findFriends()
    enemies = friend.findEnemies()
    enemy = friend.findNearest(enemies)
    pos = friend.pos
    newX = pos.x + 10
    newY = pos.y
    if enemy:
        self.command(friend, "attack", enemy)
    else:
        self.command(friend, "move", {"x": newX, "y": newY})

Only one soldier/archer moves at a time. The difference is:

if enemy:
    self.command(friends, "attack", enemy)
else:
    self.command(friends, "move", {"x": newX, "y": newY})

and:

if enemy:
    self.command(friend, "attack", enemy)
else:
    self.command(friend, "move", {"x": newX, "y": newY})

Is there a way to make all of them move at once?


#2

You can’t command a list as a group. Do you remember how to use a for-loop? Use that to loop over all your friends.


#3

Oh thank you I figured out how to move them all but they’re not attacking now?

Here’s my code:

loop:
    friends = self.findFriends()
    friend = self.findNearest(friends)
    enemies = friend.findEnemies()
    enemy = friend.findNearest(enemies)
    for friend in friends:
        if enemy:
            self.command(friend, "attack", enemy)
        else:
            self.command(friend, "move", {"x": friend.pos.x + 10, "y": friend.pos.y})

They just keep moving right then once they start getting attacked, some attack but most stay still moving really weirdly? Any ideas why?


#4

Nevermind I figured it out. Thank you


#5

I’m stuck too! How did you figure this out?


#6

@Ian_Lambert-Brown
if you click on the person’s name from whom you are requesting assistance you will find that he/she has not been on this board since January of 2016. That’s almost two years ago. You probably want to start a new thread or request assistance from current participants. We’re all here to help each other. If you post your code using the </> button or a screenshot of it, someone will be happy to help figure out the problem.


#7

Yeah, I noticed. I thought posting on an old thread might get someone’s attention. I just requested help on a new thread. I don’t know if you want to help me but… I’m stuck on this level still. Here’s my code:

friends = hero.findFriends()

for friend in friends:
    enemy = friend.findNearestEnemy()
    if not enemy:
        hero.command(friend, "move", {'x': friend.pos.x + 24, 'y': friend.pos.y})
    else:
        hero.command(friend, "attack", enemy)