Am I on the right track with this code?
I am able to beat the ogres but my hero will not walk to the gems, why?
while True:
enemies = hero.findEnemies()
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy:
hero.attack(enemy)
hero.moveXY(40, 33)
break
enemyIndex += 1
hero.moveXY(64, 58)
hero.moveXY(15, 58)
hero.moveXY(19, 12)
hero.moveXY(65, 13)
1 Like
Anna
July 4, 2020, 4:33pm
2
You didn’t add a part to break the loop once all of the enemies were dead so you could use the moveXY for the items.
I would suggest using a variable to walk to the items once all enemies are dead.
2 Likes
A couple of tips… bash the ogre so he lands behind the mines, then head back to the X.
Move back to the X after collecting each of the first 3 treasures. Your hero will get stuck in the rocks if you don’t.
3 Likes
Thank you both for the help.
I am still a bit stuck. How can I break loop after killing enemies?
I try to add else:
but I can’t after enemyIndex += 1
Here is what I have changed:
while True:
enemies = hero.findEnemies()
enemyIndex = 0
items = hero.findNearestItem()
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy:
hero.attack(enemy)
hero.moveXY(40, 33)
break
enemyIndex += 1
if not enemy:
continue
hero.moveXY(64, 58)
hero.moveXY(40, 33)
hero.moveXY(15, 58)
hero.moveXY(40, 33)
hero.moveXY(19, 12)
hero.moveXY(40, 33)
hero.moveXY(65, 13)
i also tried the following but to no avail:
while True:
enemies = hero.findEnemies()
enemyIndex = 0
items = hero.findNearestItem()
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy:
hero.attack(enemy)
hero.moveXY(40, 33)
break
else:
continue
enemyIndex += 1
hero.moveXY(64, 58)
hero.moveXY(40, 33)
hero.moveXY(15, 58)
hero.moveXY(40, 33)
hero.moveXY(19, 12)
hero.moveXY(40, 33)
hero.moveXY(65, 13)
while True:
enemies = hero.findEnemies()
enemyIndex = 0
items = hero.findNearestItem()
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy:
hero.attack(enemy)
hero.moveXY(40, 33)
break
else:
hero.moveXY(items.pos.x, items.pos.y)
enemyIndex += 1
Try it without using a ‘while True’. Since there are only 4 ogres and 4 treasures, an outer loop is not really needed.
And, I’m going to stress the bash suggestion…you need the ogres to set off the mines for you, so knock them back and when they run forward to attack again, boom!
1 Like
thanks for the suggestion but I tried hero.bash
initially (since thats what the level suggests) and the enemy does not get knocked into the traps as easily as hero.attack
.
removing the outer while True:
did help though. Thank you again for the help.
2 Likes
Ok…if that works, then that’s good! It might be the shield…I had Deflector, so got maximum ‘knock back’.
2 Likes