Python: Hunters and Prey[SOLVED]

No, you need to check if there is a enemy.

2 Likes

oh wait… i did if enemy.pos, not if enemy. :expressionless:

2 Likes

still not working...

# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    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(archer):
    enemy = archer.findNearestEnemy()
    if enemy:
        
        if enemy.pos.x < 25:
            hero.command(archer, "attack", enemy)
hero.setFlowerColor("purple")
hero.toggleFlowers(True)
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
1 Like

i used something like if enemy (find the distance from the archer to enemy):

so i merged it together

2 Likes

@CocoCharlie the issue with your code is this:

You do not actually have a pickUpCoin() or a

summonTroops()

function in your most recent code. If you have it above that, then this is not the issue. Hope this helps!
-Grzmot

1 Like

sry i must not have gotten all of it copied. the issue right now is in commanding my soldiers. It doesn’t like the enemy variable.

# 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.moveXY(item.pos.x, item.pos.y)
    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.
    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(archer):
    enemy = archer.findNearestEnemy()
    if enemy:
        
        if enemy.pos.x < 25:
            hero.command(archer, "attack", enemy)
hero.setFlowerColor("purple")
hero.toggleFlowers(True)
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

No, it doesn’t like soldier in this:

maybe in your colect coin function do move(item.pos)

Colecting coins has nothing to do with the problem, @Eric_Tang. It’s commanding the soldiers.

I know, but moveXY() canceles all code until the move is done.

add ()

in the () replace friend with soldier

Ok, now it’s functioning. now i have to figure out how to keep the archers is place.

It’s this part that’s not right…

For me I did

if archer.pos < 25:
#do the code
else: 
    #command your archers to move to archer.pos

Well, it worked finally. Thanks guys! :sleepy:

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.