Hey! So my code is this, my hero doesn’t even MOVE.
# findEnemies returns a list of all your enemies.
# Only attack shamans. Don't attack yaks!
enemies = hero.findEnemies()
enemyIndex = 0
# Wrap this section in a while loop to iterate all enemies.
# While the enemyIndex is less than the length of enemies
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type == "shaman":
while enemy.health > 0:
hero.attack(enemy)
elif enemy and enemy.type == "yak":
hero.say("Hi.")
# Remember to increment enemyIndex
enemyIndex += 1
If you are still just standing there, I’m guessing you didn’t properly follow @Code_Master tip. Does your run bar always show “Running” and do you get an error for an infinite loop?
When I reset my level, I noticed the indentation was all shifted at the beginning which can be confusing. Looking at your original code, to include it in the while loop you need to have that line of code indented one level, otherwise you will have an infinite loop since you are always comparing 0 to the length of the enemy array.
Here is my code again, it reset because a notification popped up, claiming my code had an infinite loop. I clicked the wrong button and I was back at the start. I took the advice you gave me and here’s what I have, however, THE HERO STILL DOESNT MOVE.
# findEnemies returns a list of all your enemies.
# Only attack shamans. Don't attack yaks!
enemies = hero.findEnemies()
enemyIndex = 0
# Wrap this section in a while loop to iterate all enemies.
# While the enemyIndex is less than the length of enemies
enemy = hero.findNearestEnemy()
if enemy.type == 'shaman':
while enemy.health > 0:
hero.moveXY(enemy.pos.x, enemy.pos.y)
hero.attack(enemy)
enemyIndex += 1