[SOLVED]Help in Hunter's prey

Lydia I still don’t get what your saying

Check if the distance to the enemy is less than 25 before you command your troops to attack

Delete this
Lydia

but then the code you told me to delete didn’t that check the distance?

But you checked it in the wrong place
Lydia

1 Like

so here is my entire 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 hero.distanceTo(enemy) <= 25:

        if friend.type == "archer":
            hero.command(friend, "attack", enemy)
            
        else:
            if hero.distanceTo(enemy) >= 25:
                hero.command(friend, "move", friend.pos)
    
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(friend)
elif friend.type == “archer”:
# Be sure to command your archers.
commandArcher(friend)
pass

can you please tell me what should I fix and what not to fix since what you just said I didn’t get

# 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 hero.distanceTo(enemy) <= 25:
                
            if friend.type == "archer":
                hero.command(friend, "attack", enemy)
                
            else:
                if hero.distanceTo(enemy) >= 25:
                    hero.command(friend, "move", friend.pos)
        
    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(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher(friend)
            pass

Format it correctly please.
Lydia

        ^

Delete this |

Change this into

hero.command(archer, "move", {'x':24, 'y':47})

And hit sumbit

Lydia

1 Like

This is right the archers are supposed to stay in there spots if the enemy is out of range

this is what it says when I changed the things you told me to change:

        if hero.distanceTo(enemy) <= 25:

Post your full code
Lydia

you have to define enemy put this in function

enemy = hero.findNearestEnemy()
# 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 hero.distanceTo(enemy) <= 25:
                
            if friend.type == "archer":
                hero.command(friend, "attack", enemy)
                
            else:
                hero.command(archer, "move", {'x':24, 'y':47})
        
    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(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher(friend)
            pass

Your code look identical to mine, hit sumbit
Lydia

Submit it then it should work

nope it got null (20 chars)

umm so if his code is correct should he delete it?

Yes
Lydia
20 characters

@Lydia_Song can I see your code since you beat it right? And it looks identical so that wouldn’t be showing me the answers.

Submit a few times then it should work