[SOLVED] Two flowers (Python)

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():
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "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()

My hero will summon soldiers, but the soldiers just protect me and Hector dies.
Can’t figure out what is wrong

I am JavaScript but try telling your soldiers to find the enemy, not your hero.

enemy = friend.findNearestEnemy()
or
enemy = friend.findNearest(friend.findEnemies())

Also maybe add an “if enemy” in there

Also quick question, what is the purpose of this line:

peasant = hero.findByType(“peasant”)[0]

?Are you identifying Hector for some reason?

Sorry last question, I noticed you have a line separating your definition of “summonSoldiers()” but no line separating “commandSoldiers()” and “pickUpNearestCoin”. I am not familiar with Python yet but could that be a contributing factor?

Also for future reference it is good practice to post your code with the </> button

ie)

# 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():
enemy = hero.findNearestEnemy()
soldiers = hero.findByType(“soldier”)
for soldier in soldiers:
hero.command(soldier, “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()

What kind of strange double quotes are you using? Your browser is maybe not set to English as default language. If properly indented your code must work if your double quotes are right and you

hero.moveXY(item.pos.x, item.pos.y) # comment this line and replace it with
hero.move(item.pos)
#/ use the **</>** symbol above to format properly /

You must also replace boots if they don’t support move - moveXY has a hidden loop inside it and can add troubles when you command troops. In your case I think that they follow you hero as pets until you all die.

#cmmnd can be also defend  ( attack needs enemy)
#and pos can be peasant.pos or hero.pos or soldier.pos
#Try all possibilities and see what works best
hero.command(soldier, cmmnd, pos)
1 Like

You have to see if the enemy exists. If you want it to protect Hector, then change the code to

this = hero.findByType(peasant)[o]
if this:
    hero.command(friend, "defend", this)
1 Like

Thanks to you all, but I still have a problem. This is my code again

# 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():
    friend = hero.findFriends()
    enemy = friend.findNearest(friend.findEnemies())
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    this = hero.findByType("peasant")[0]
    if this:
        hero.command(friend, "defend", this)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

Now it says that friend has no method findEnemies

So then use friend.findNearestEnemy().

Changed some stuff. Here.

# 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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    if hero.findByType("peasant"):
        hero.command(hero.findFriends, "defend", hero.findByType("peasant"))
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

Now I get an error message: Command's argument minion should have type unit, but got function. Hero Placeholder needs something to command.

peasants = hero.findByType("peasant")
for thing in peasants:
    if this:
        hero.command(this, "defend", thing)

or

peasant = hero.findByType("peasant")
if peasant and this:
    hero.command(this, "defend", peasant)
    

where do I insert this?

In your commandSoldiers()

I did what you said, but I still get an error.

# 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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
    peasant = hero.findByType("peasant")
    if peasant and soldier:
        hero.command(soldier, "defend", peasant)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

It says defend takes no arguments. As in the part in commandSoldiers.

Now I have this:

# 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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    peasant = hero.findByType("peasant")
    x = peasant.pos.x
    y = peasant.pos.y
    for soldier in soldiers:
        hero.command(soldier, "defend", {x, y})
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

My hero just stands there. How do I fix that?

Thanks to you all!
I got it!
:smile:

1 Like

Please don’t post solutions on the board, thanks 123hi123!

It took me a little bit because I don’t know python but I got it as well lol (not in time to help you though!) I didn’t try defend because I am not familiar with the language but I am happy you got it!

If you want to post spoilers people usually put them between a

[spoiler] text here [/spoiler]

Like this! <- To get this =) (click to reveal)

Also can you add [SOLVED] to your topic header?

1 Like

how can I solve it?
P.S: I nearly spoils

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
def commandSoldiers():
    friend = hero.findFriends()
    soldiers = hero.findByType("soldier")
    peasant = hero.findByType("peasant")
    for soldier in soldiers:
        hero.command(soldier, "defend", {'x':peasant.pos.x, 'y': peasant.pos.y})
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

zython

Here you should select only an element of the array that findByType gives you, not the whole array (maybe something like hero.findByType(“peasant”)[0]?)

Here you should command your soldiers to attack their nearest enemy instead of defending the peasant’s position.

Andrei

it works at the start, but, all the soldiers dies at the end and Heator died too.:frowning:

1 Like

Can you send me your new code?

Andrei

pass!

[en_US.composer.my_button_text]

1 Like

Congratulations for completing the level then! :partying_face:

Andrei