It’s an Infinite loop
# Gather coins to summon soldiers and have them attack the enemy.
while True:
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
item = hero.findNearestItem()
hero.move(item.pos)
hero.say("I need coins!")
# If you have funds for a soldier, summon one.
if hero.gold > hero.costOf("soldier"):
hero.say("I should summon something here!")
hero.summon("soldier")
enemy = hero.findNearest(hero.findEnemies())
if enemy:
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
# Loop over all your soldiers and order them to attack.
while soldierIndex < len(soldiers):
# Use the 'attack' command to make your soldiers attack.
#hero.command(soldier, "attack", enemy)
hero.command(soldier, "attack", enemy)
It’s a infinite loop because the soldierIndex is always 0.
2 Likes
Also, while not required, I’d recommend doing without the ‘say’ statements. While they would add flavor to the game play, in this lesson they just take up space and resources. Yes, there are some lessons where you must have the hero/pet speak, but not this one.
1 Like
Another problem is that soldier = soldiers[soldierIndex]
is not in the while loop. You are just commanding soldiers[0]
right now
1 Like
This is my new code. I died
while True:
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
item = hero.findNearestItem()
hero.move(item.pos)
# If you have funds for a soldier, summon one.
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
enemy = hero.findNearest(hero.findEnemies())
if enemy:
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
# Loop over all your soldiers and order them to attack.
while soldierIndex < len(soldiers):
# Use the 'attack' command to make your soldiers attack.
#hero.command(soldier, "attack", enemy)
hero.command(soldier, "attack", enemy)
soldierIndex += 1
Ok, you are now properly incrementing soldierIndex. However, take a closer look at your ‘while soldierIndex’ loop…which soldier is being commanded to attack?
I changed my code again but forgot to say so.
No worries…glad to see you got through the level
It’s been 2 years @itik, please don’t necropost
3 Likes
Is that a solution or a snippet?
Posting solutions is against the rules, so please remove it.
2 Likes