I have been stuck on this level for a few days and I need some help with it. My char is killing only one of the enemy’s and they just stay there and say “but it’s dead…” and I can’t figure out what is wrong. Here is my code: while True: enemies = hero.findEnemies() # enemyIndex is used to iterate the enemies array. enemyIndex = 0 # While enemyIndex is less than len(enemies) while enemyIndex < len(enemies): # Attack the enemy at enemyIndex enemy = enemies[enemyIndex] hero.attack(enemy) # Increase enemyIndex by one. enemyIndex += 1 # coinIndex is used to iterate the coins array. coinIndex = 0 while coinIndex < len(coins): # Get a coin from the coins array using coinIndex coins = hero.findNearestItem() coin = coins[coinIndex] # Collect that coin. hero.moveXY(coin.pos.x, coin.pos.y) # Increase coinIndex by one. coinIndex += 1
Please Help!
Hi @jacksepticeye_24 and welcome to the forum!
Please use three backticks to format your code so we can help you.
while True:
enemies = hero.findEnemies()
# enemyIndex is used to iterate the enemies array.
enemyIndex = 0
# While enemyIndex is less than len(enemies)
while enemyIndex < len(enemies):
# Attack the enemy at enemyIndex
enemy = enemies[enemyIndex]
hero.attack(enemy)
# Increase enemyIndex by one.
enemyIndex += 1
# coinIndex is used to iterate the coins array.
coinIndex = 0
while coinIndex < len(coins):
# Get a coin from the coins array using coinIndex
coins = hero.findNearestItem()
coin = coins[coinIndex]
# Collect that coin.
hero.moveXY(coin.pos.x, coin.pos.y)
# Increase coinIndex by one.
coinIndex += 1
Here is my code I believe it is properly formatted.
You didn’t define coins before the loop. it should look something like this:
coins = get an array of items #this should return an array if you put in the right line of code
coinIndex = 0
while coinIndex < len(coins):
# Get a coin from the coins array using coinIndex
coin = coins[coinIndex]
# Collect that coin.
hero.moveXY(coin.pos.x, coin.pos.y)
# Increase coinIndex by one.
coinIndex += 1
If that is confusing let me know!
[quote=“Anna, post:4, topic:22876”]
coins = hero.findArrayItems()
[/quote
I put in that code and it is not working, they are still standing there and saying “but its dead…”
while True:
enemies = hero.findEnemies()
# enemyIndex is used to iterate the enemies array.
enemyIndex = 0
# While enemyIndex is less than len(enemies)
while enemyIndex < len(enemies):
# Attack the enemy at enemyIndex
enemy = enemies[enemyIndex]
hero.attack(enemy)
# Increase enemyIndex by one.
enemyIndex += 1
coins = hero.findArrayItems()
coinIndex = 0
while coinIndex < len(coins):
# Get a coin from the coins array using coinIndex
coin = coins[coinIndex]
# Collect that coin.
hero.moveXY(coin.pos.x, coin.pos.y)
# Increase coinIndex by one.
coinIndex += 1
I said that was a placeholder for the actual line of code you should be using. Sorry if that wasn’t clear.
It should not be hero.findNearestItem()
but something else that finds items AND returns them in an array like your enemies are.
your fine my brain just isn’t wanting to work today ill try to figure it out. Thanks for the help
It’s alright! It happens to most of us more often than you think!
This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.