Hello, I would need help with my code in level Diamond dozen:
In the beginning I couldn’t even start the code, the instructions were that unclear. After viewing others’ code, I came to this conclusion.
My problem is: I can’t get to the coins in time, I bought the “best” shoes, so I can jump to the coin, too. The problem is, that my hero won’t jump, only walk. The other thing is, do I really need over 1000 Health? Sometimes I die, sometimes I don’t, but I only survive with like, 5 Health?
I am not even sure if my main code is good, sometimes my hero only goes to the bronze coins…
# Claim the coins while defeating the marauding ogres. def findMostHealth(enemies): target = None targetHealth = 0 enemyIndex = 0 while enemyIndex < len(enemies): enemy = enemies[enemyIndex] if enemy.health > targetHealth: target = enemy targetHealth = enemy.health enemyIndex += 1 return target def valueOverDistance(item): return item.value / hero.distanceTo(item) # Return the item with the highest valueOverDistance(item) def findBestItem(items): bestItem = None
bestValue = 0 itemsIndex = 0 # Loop over the items array. while itemsIndex<len(items): item=items[itemsIndex] if item.value>bestValue:
bestValue=valueOverDistance(item) bestItem=item # Find the item with the highest valueOverDistance() itemsIndex+=1 return bestItem while True: enemies = hero.findEnemies() enemy = findMostHealth(enemies) if enemy and enemy.health > 15: while enemy.health > 0: hero.attack(enemy) if coin: if hero.isReady('jump'): hero.jumpTo(coin) else: hero.moveXY(coin.pos.x, coin.pos.y)