Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Python: Hunters and Prey[SOLVED]

I can’t get the arrays to work.

# 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
    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.pos.x < 50:
        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()
            pass
1 Like

You’ll be commanding soldiers and archers. Do hero.findByType(“soldier”)

1 Like

try doing thishero.findByType("soldier", hero.findFriends()) this helps you find the specific type of friend this works for all units

1 Like

Where should I write that?

1 Like

I think it says that in the For loop.

1 Like

Hi CocoCharlie,

Your code is nearly right. A couple of things.

Compare the end of these two lines:
enemy = archer.findNearestEnemy

friends = hero.findFriends()
which is right?

Also compare these lines:
commandSoldier(friend)
commandArcher()
which is right?

Jenny

2 Likes

I added friend in commandArcher(), but don’t know what to do next.

1 Like

Arn’t they both right?

1 Like

No. () for the findNearestEnemy.

2 Likes

Can someone please help me?

1 Like

@Babygirl_77 please do not post on mulitipule because it does not get help faster

1 Like

Jeez, sorry
I’m new okay

1 Like

yeah I know first couple of mistakes I made

1 Like

I will pm and help you there

1 Like

Can you post your current code pls @CocoCharlie ?

1 Like
# 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
    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.pos.x < 50:
        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 have no idea why it’s not working. I honestly don’t understand the for loops and command stuff very well yet.

1 Like

add if enemy

its supose to be 25

3 Likes

fixed that, and it does the same thing…

1 Like

Wait… maybe i need a different variable for “enemy”?
cuz i have two.

2 Likes