Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

[Solved] Mind the Trap Python Help


This is my code:

while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    if flag:
        # Pick up the flag.
        hero.say("I should pick up the flag.")
    elif enemy:
        # Only attack if the enemy distance is < 10 meters
        if enemy < 10:

But for some reason, my hero won’t attack. Please help!


Can you take a screenshot of what is happening @Garv_Jain


I can’t but what happens is that when the ogres attack my hero, he won’t attack back


Lets see :thinking:. Are you placing flags so that your hero can go attack them?


like this:


Yes, and my hero will move to the flag, just won’t attack


Hmm… That is weird i am using the same code as you


AHA! you didn’t find the distance to your enemy


So then what code do I need to add


You have to find the distance to your enemy then say if distance <10 not if enemy<10 because you have to find the distance

distance = hero.distanceTo(enemy)


Than say if distance <10


Ohhhh. Thanks. That makes a lot more sense


No problem. Im happy to help any time


And after you have solved the problem could you please put Solved on the title because it is a pain for me to always put solved for every one @Garv_Jain


For your next one you should put solved but i will do this one. Just this one. See you around @Garv_Jain :smile:. Good Job!


don’t post your solution, Enderlord832. Just don’t.


I didn’t. I was just helping him


You posted your solution by using the screenshot. next time crop the image.


Oh ok. Next time i will do that