Hunters and prey help plz

I can’t beat the level Hunters and Prey because my archers run off and don’t come back in time.

    # Collect coins.
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.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(soldier):
    # Soldiers should attack enemies.
    friend = hero.findFriends()
    if friend and friend.type == "soldier":
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
    pass

here is my code

I added comanding the archers

Please post all of your code…this looks to be only part of the middle.

As dedreous says, it would be useful if you could post all the code, especially if you need help with your archers.

But from the commandSoldier function that you’ve put above, you need to put the argument (in this case the word soldier (note not “soldier”)) somewhere in the function code. If you then want to run the function for thisThingHere you type:

hero.commandSoldier(thisThingHere);

and then thisThingHere will replace the word soldier wherever it appears in the function code.

I here it is

# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.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(soldier):
    # Soldiers should attack enemies.
    friend = hero.findFriends()
    if friend and friend.type == "soldier":
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "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():
    friend = hero.findFriends()
    if friend and friend.type == "archer":
        enemy = friend.findNearestEnemy()
        archer.pos = (23, archer.pos.y)
        while True:
            hero.command(friend, "move", archer.pos)
        if enemy and friend.distanceTo(enemy) <= 25:
            hero.command(friend, "move", archer.pos)
            hero.command(friend, "attack", enemy) and hero.command(friend, "move", archer.pos)
            hero.command(friend, "move", archer.pos)
        else:
            hero.command(friend, "move", archer.pos)

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(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher()
            pass

Thanks tv337.

OK, so the functions commandSoldier and commandArcher are like machines - they have an input, and then the machine does something with that input.

Starting at the bottom of your code:

 if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher()

then the commandSoldier(friend) has an input; you need to make the commandArcher() have an input as well.

So then functions with an input. Say you have a piece of code:

def makeAttack(gloop):
    if(gloop):
        hero.attack(gloop)

it’s like a machine that takes whatever word is put into the gloop position and crunches the rest of the code assuming that gloop has been replaced with the new word. So you can use this code to attack lots of different things - makeAttack(enemy) and if there’s an enemy the hero will attack it; makeAttack(ogre) and the hero will attack the ogre; makeAttack(friend) and the hero will attack the friend…).

So looking at your code:

def commandSoldier(soldier):
    # Soldiers should attack enemies.
    friend = hero.findFriends()
    if friend and friend.type == "soldier":
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
    pass

somewhere in this you need to have the input word (soldier), so that when we run the function as commandSoldier(friend), then the word friend will replace all the soldier words.

Try that for starters, post again if you’re still stuck.

Wait which one of you is stuck

I solved it and passed thanks to all who helped me!! :sunglasses: :lion:

2 Likes

yay great you solved it