Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Restless Dead Level

why my hero does not gather coins???

# Найди и убей йети, чтобы добыть его кровь для ритуала.
# Возможно, тебе стоит собрать монеты, которые оставит йети. Они пригодятся, чтобы вызвать подкрепление.
# Встань на ритуальный камень (красная отметка Х) для призвания.
# Теперь всё просто: нужно пережить нашествие орд нежити.
def attackEnemy():
    while True:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
            if hero.canCast("lightning-bolt", enemy):
                hero.cast("lightning-bolt", enemy)
                hero.manaBlast()
            
        

def collectItems():
    coin = hero.findNearest(hero.findItems())
    if coin:
        hero.move(coin.pos)
        
        

hero.moveXY(55, 10)
attackEnemy(hero.findNearestEnemy())
collectItems()

Let me answer your question by asking you this question, how many times will your code run through your functions? What can you add to make it run through more than once?

attackEnemy()
collectItems()

Also, since you don’t have the parameter in your function, you don’t need to add hero.findNearestEnemy() when you call the function.

def attackEnemy(): # no (parameter) defined
attackEnemy(hero.findNearestEnemy()) # remove hero.findNearestEnemy()
1 Like

Still doesn`t make sense why hero does not gather coins

def attackEnemy():
    while True:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
            if hero.canCast("lightning-bolt", enemy):
                hero.cast("lightning-bolt", enemy)
                hero.manaBlast()
            
        

def collectItems():
    coin = hero.findNearest(hero.findItems())
    if coin:
        hero.move(coin.pos)
    
    

hero.moveXY(55, 10)
attackEnemy()
collectItems()
while True:
    attackEnemy()
    collectItems()

Since you have the while True in your first function attackEnemy(), it won’t move to the next one to collect coins. Remove the while True from that function. If you want to make sure you attack the enemy until it is dead before collecting coins, switch the while true to a while loop with a condition like below.

if enemy:
    while enemy.health > 0:

Thanks for the advice, but why it doesn`t move to the next coin since I have a while loop in first function?

Your first function becomes an infinite loop with the while True in that function. Until you add a break, it will keep repeating attackEnemy(). Meaning, once you start that function, it doesn’t stop and you don’t move on to your next function.

But I put there if enemy: So it is gotta be while True if there is any enemy, if not it supposed to move to next function

Now that you have the while True at the bottom, you will circle through the two functions, so you don’t need a while True in the function.

def attackEnemy():
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
        if hero.canCast("lightning-bolt", enemy):
            hero.cast("lightning-bolt", enemy)
            hero.manaBlast() # this may cause problems since you don't check if hero.isReady(mana-blast):
1 Like

You didn’t (I could be wrong) actually specify for the hero to find coins. And, you need to say coin.pos.x, coin.pos.y (I think) Give It a shot…

If you look further up, you will see the whole code that was written. The last few comments I was focusing on the section of code that was causing the problem. Below is the function used to find a coin and move to it. When using the hero.move(), you only need to include the .pos.

def collectItems():
    coin = hero.findNearest(hero.findItems())
    if coin:
        hero.move(coin.pos)