Hunters and Prey Python Help

Here is my code:

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier():
    for soldier in hero.findFriends():
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandArcher():
    for archer in hero.findFriends():
        enemy = archer.findNearestEnemy()
        if enemy and archer.distanceTo(enemy) < 25:
            hero.command(archer, "attack", enemy)
        
    pass

while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier("soldier")
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher("archer")
            pass

For some reason, the archers attack everyone just like the soldiers do. Can someone please help me figure out what is going wrong?

Code looks good overall…but, what do your archers do if the distance is not < 25?

It doesn’t matter how far anything is away they just try to kill it. Then they get too far away from the reindeer and the reindeer get killed.


Here is my gear if that helps.

How about having them stay in place if the distance is not < 25? In other words, think about what ELSE they should do :wink:

   def commandSoldier(): 
    for soldier in hero.findFriends():
    # is your soldier a "soldier" 

def commandArcher():
    for archer in hero.findFriends():
    # is your archer an "archer"

while True:
# code
            commandSoldier("soldier")
#         you defined above function as commandSoldier() - no argument
            commandArcher("archer")
#         you defined above function as commandArcher() - no argument

Here is my new definition of commandArcher:

def commandArcher():
    for archer in hero.findFriends():
        enemy = archer.findNearestEnemy()
        if enemy and archer.distanceTo(enemy) < 25:
            hero.command(archer, "attack", enemy)
        else:
            hero.moveXY(23, 46)
        
    pass

But they don’t move back to the position I want them to.

Maybe it’ better to divide enemy existance and distance to it?

Oh no. I feel so dumb I said hero.move instead of hero.command

But, your still need each archer stay in his own place, if there is nothing going on.

Wait what the heck it only worked when I made my hero not collect coins. I don’t think that’s how it is supposed to work, but ok.