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

The Two Flowers (python) help needed

I have tried defending the peasant both by letting the soldiers attack their own nearest enemy (which seems to draw enemies away from the peasant) and by giving the defend command. That last one works quite some time, but eventually the peasant will get stuck in a corner and the friendly soldiers will get overwhelmed by the large number of enemies.

What am I doing wrong?

PS. when will the game get some proper level hints and have an option to skip a level? I’m playing this with my kid and it really takes all the fun out when there is no way to progress.

# 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():
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "defend", peasant)
            
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    items = hero.findItems()
    for item in items:
        coin = hero.findNearestItem()
        hero.move(coin.pos)

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

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

Welcome to the forum Goodie! Getting frustrated is a given aspect of programming…we rarely hit the correct solution the first time.

EDIT: I just re-read your post. How did you have your soldiers attack the nearest enemy? (Please show that code.) It is oft said that Hector must be protected. While true, it is not ultimate…if he takes a hit, it’s a minor setback, if he takes many, it means defeat. Setbacks can/will be overcome.

Thanks for responding. The code I used for attacking the nearest enemy is:

# 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():
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        enemies = soldier.findEnemies()
        enemy = soldier.findNearest(enemies)
        hero.command(soldier, "attack", enemy)
            
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    items = hero.findItems()
    for item in items:
        coin = hero.findNearestItem()
        hero.move(coin.pos)

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

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

I ran it with your code and it worked just fine. How are you fitted for boots and which hero are you playing with? I used Anya and Boots of Leaping…I don’t think I had the ring of speed when I first completed this, but that might be a consideration too.

I did notice that you can trim down your code by a few lines, making it more efficient and quicker. In your ‘def commandSoldiers’, make the for loop the first line of the definition…like:

for each soldier in hero find by type, soldier
then, assign the nearest enemy
if there is one, command that soldier to attack

The for loop will iterate through all available soldiers, so if one gets killed, or others are summoned, a fresh set is found every time you call commandSoldiers(). This same method can also be applied to your pickup coins definition.

Thanks for the help! It was the ring of speed that made the difference.

1 Like

Glad to have helped!

wheres my dad???i need help on that level…i cant seem to find my dad