[Solved] Mind the Trap Python Help


#1

This is my code:

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

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


#2

Can you take a screenshot of what is happening @Garv_Jain


#3

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


#4

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


#5

like this:


#6

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


#7

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


#8

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


#9

So then what code do I need to add


#10

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


#11
distance = hero.distanceTo(enemy)

#12

Than say if distance <10


#13

Ohhhh. Thanks. That makes a lot more sense


#14

No problem. Im happy to help any time


#15

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


#16

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!


#17

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


#18

I didn’t. I was just helping him


#19

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


#20

Oh ok. Next time i will do that