There is no error for this one. My character just stands still, like nothing is happening. Confusion??
# Use while loops to pick out the ogre
while True:
enemies = hero.findEnemies()
enemyIndex = 0
for enemy in enemies:
hero.attack(enemy)
# Wrap this logic in a while loop to attack all enemies.
# Find the array's length with: len(enemies)
enemy = enemies[enemyIndex]
# "!=" means "not equal to."
if enemy.type != "sand-yak":
# While the enemy's health is greater than 0, attack it!
hero.attack(enemy)
pass
# Between waves, move back to the center.
hero.moveXY(42, 32)
Your indentation is wrong. All the code you want to run more than once should be inside the while True loop. That’s all of it. Also, you can either use a for loop, or a while loop (which will need the enemyIndex. You can’t switch between them. You’re also attacking twice. It looks like two different strategies. Just chose one, loop through the enemies, attack the non-yaks and move back to the centre after the for loop has finished running.
Danny
# Use while loops to pick out the ogre
while True:
enemies = hero.findEnemies()
enemyIndex = 0
for enemy in enemies:
hero.attack(enemy)
# Wrap this logic in a while loop to attack all enemies.
# Find the array's length with: len(enemies)
enemy = enemies[enemyIndex]
# "!=" means "not equal to."
if enemy and enemy.type != "sand-yak":
# While the enemy's health is greater than 0, attack it!
while enemy.health > 0:
hero.attack(enemy)
# Between waves, move back to the center.
hero.moveXY(42, 32)
Your hero is attacking the sand-yaks because your line 7 tells the hero to attack each enemy. Delete this line, and just use the attack command in line 14.
I think you can choose to use a while loop or a for loop (when I tried it it worked with both). However you need to stick to one or the other, as you seem to have bits of code that are trying to do both.
The other thing you need to do is sort out your indentation. This would be easier to see if you go back to the while loop, as the comments are all written in the right place; but you could work it out for the for loop if you wanted. Which bits of code are running inside other conditions?
1, thanks for the first thing,
2, i havent gotten for loops yet
3, i did indent it correctly
4, all of it? haha
5, now the problem is that he gets stuck in his walking animation… but doesnt do the walking in my “move back to the center code”…
6 If i delete my “move back to the center code” he does literally nothing- NOTHING
# Use while loops to pick out the ogre
while True:
enemies = hero.findEnemies()
enemyIndex = 0
# Find the array's length with: len(enemies)
enemy = enemies[enemyIndex]
# "!=" means "not equal to."
if enemy and enemy.type != "sand-yak":
# While the enemy's health is greater than 0, attack it!
while enemy.health > 0:
hero.attack(enemy)
# Between waves, move back to the center.