Forest storm help (please help i'm desperate)

I am for some reason not collecting coins

code:
def heroAttack():
enemy = hero.findNearestEnemy()
if enemy:
if hero.distanceTo(enemy) < 15:
if enemy.type == “munchkin”:
hero.attack(enemy)
return “enemy”

def heroCollect():
item = hero.findNearestItem()
if item:
if hero.distanceTo(item) < 15:
if item == “coin”:
hero.moveXY(item.pos)
return “item”

while True:
enemy = hero.findNearestEnemy()
if enemy:
heroAttack()
item = hero.findNearestItem()
if item:
heroCollect()

Hello, @meee

Please format your code, by pressing the small menu on top, as it makes it easier to read your code, and understanding the indentation of your code.
image

And welcome to the discourse! We hope you’ll enjoy the discourse here, just don’t forget to read the FAQ’s and Rules.
:confetti_ball: :confetti_ball: :confetti_ball: :confetti_ball: :confetti_ball: :confetti_ball:

Return is not needed in this code.

Try this:

def heroAttack():
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.distanceTo(enemy) < 15:
            if enemy.type == "munchkin":
                hero.attack(enemy)

def heroCollect():
    item = hero.findNearestItem()
    if item:
        if hero.distanceTo(item) < 15:
            if item.type == "coin":
                hero.moveXY(item.pos.x, item.pos.y)

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        heroAttack()
    item = hero.findNearestItem()
    if item:
        heroCollect()

And welcome to the discourse!

1 Like

Thank you so much. I owe you my life.

1 Like