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

The two flowers HELP

This is my code…

# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findByType("soldier")
    for friend in friends:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x, item.pos.y)
    peasant = hero.findByType("peasant")[0]

while True:
    summonSoldiers()
    # commandSoldiers()
    # pickUpNearestCoin()
    commandSoldiers()
    pickUpNearestCoin()

The code is fine is just that Hector dies :confused:

you have to make sure your soldiers stay near hector. If hector runs off make sure your soldiers will follow him.

P.S. a little hint: the code needs to be much longer

Comparing your code to mine, the problem appears to be in pickUpNearestCoin.

def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x, item.pos.y)
    peasant = hero.findByType("peasant")[0]

Try making an array if items and then finding the nearest of those…only one additional line needed, also with an edit to one existing line.

Ok now my soldiers protect hector but he still dies…

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findFriends()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "defend", peasant.pos)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move({"x":item.pos.x,"y":item.pos.y})
peasant = hero.findByType("peasant")[0]


while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

@Ry4n_17, without re-reading the entire thread again, I believe someone said you need to keep your soldiers close to Hector. I found this not necessary. In my ‘def commandSoldiers()’, I simply have them attack if there is an enemy.

In your ‘def pickUpNearestCoin()’, I would simplify the move command with simply ‘hero.move(item.pos)’. I used a different method to find the nearest, but I think yours should work fine. If not, I’ll try to explain mine.

So now i have this, i make my soldiers atack and i changed my move, stil dosent work :confused:

# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findFriends()
    for friend in friends:
        enemy = friend.findNearestEnemy()
        soldiers = hero.findByType("soldier")
        for soldier in soldiers:
            if friend.type == "soldier":
                hero.command(soldier, "attack", enemy)

# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)

peasant = hero.findByType("peasant")[0]

while True:
    summonSoldiers()
    # commandSoldiers()
    commandSoldiers()
    # pickUpNearestCoin()
    pickUpNearestCoin()



Ok…try setting a variable = all ‘items’. Then, find the nearest of those ‘items’ and use that ‘item’ to collect the coins.

I dont understand, can you explain a little more

I have an additional line that finds all items: items = hero.findItems()
My ‘item’ line is: item = hero.findNearestItem(items)

If you’d like to see the actual code, I’m PM it to you.

Ive been looking at other codes and all the pickupnearestcoin() def are the same, they all have the

if item:
    hero.move(item.pos)