I’m really newbie in coding, and I’m trying to do my best, learning by myself !
I’m stuck at the level Sarven Treasure !
Here is my code !
return #Commented out to stop infinite loop.
# Recueillir 150 pièces d'or tout en évitant les ogres avec les téléporteurs
# Si vous gagnez , il devient plus difficile ( et plus gratifiant )
# Si vous perdez , vous devez attendre un jour avant de pouvoir soumettre à nouveau
# Rappelez-vous, chaque soumission obtient une nouvelle amorce aléatoire
while True:
coins = hero.findNearestItem()
coinIndex = 0
enemy = hero.findNearestEnemy()
while 1000 > hero.health > 500 and coinIndex < len(coins):
coin = coin[coinIndex]
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
goldCoin += 1
elif enemy and hero.distanceTo(enemy) < 8 and hero.isReady("hide"):
hero.hide()
if enemy and hero.distanceTo(enemy) < 8:
hero.attack(enemy)
while hero.health < 500 and coinIndex < len(coins):
coin = coin[coinIndex]
if enemy:
if hero.isReady("hide"):
hero.hide()
else:
hero.attack(enemy)
elif coin:
hero.moveXY(coin.pos.x, coin.pos.y)
goldCoin += 1
I don’t really understand everything I do, to be honnest, but with what i’m learned on the past level, this code should work …
I’m not absolutely sure of this, maybe it works, but the better way of writing it is two separate conditions. So while hero.health < 1000 and hero.health > 50 and here the rest of it.
Ok now I’m trying to make it even more simplier, so here is my new code:
while True:
enemy = hero.findNearestEnemy()
coin = hero.findNearestItem()
if coin and enemy and hero.distanceTo(enemy) > 8:
hero.moveXY(coin.pos.x, coin.pos.y)
if enemy and hero.distanceTo(enemy) < 8:
hero.attack(enemy)
Now the hero starts by going to pick up all the coins until the distance to enemy is less than 8, then he kills the enemies, but after that he stops moving.
How do I do to make to make the while loop continue ?
It’s okay. That’s how everyone starts, but later on, when you get better at coding, you will be able to make much more complex code. Also, brawls are designed to become harder by time, so you can improve your code bit by bit as you learn new things. Don’t feel disappointed, we are all learning
P.S press the checkmark on the post that was the most helpfull/ the post that helped you solve the level so this topic is close and could you add a solved at the title?