Mind The Trap Python Help

I’m using this code so far.

while True:
flag = hero.findFlag()
enemy = hero.findNearestEnemy()
distance = hero.distanceTo(enemy)
ready = hero.cleave(enemy)

if flag:
    # Pick up the flag.
    hero.moveXY(flag.pos.x, flag.pos.y)
elif enemy and distance < 10:
    # Only attack if the enemy distance is < 10 meters
    hero.moveXY(flag.pos.x, flag.pos.y)

i’m rather new to this whole code stuff but i like it a lot. As far as i can see it gives me the position of the flags im placing, it moves there and picks it up, and also attacks enemy (that i know the distance of) that are within a 10 block radius. But still when i press play and place a flag the guy just runs towards the bombs.

What am i doing wrong?

Remove this one line and you are good. This variable you created calls the cleave command and since your didn’t put the distance limit on this, it finds the nearest enemy and moves to the cleave range. A flag won’t interrupt his attack. Keep this in mind when you start multiplayer, when an attack command is given for a warrior and they are out of attack range, they will move to the enemy until they reach attack range. They won’t listen to any other command until they finish the attack.

ready = hero.cleave(enemy)

On another note, are you getting an error with the else:? Since you may not have placed a flag and this is the last option, it will give you an error.

    hero.moveXY(flag.pos.x, flag.pos.y) # if no flag, there is no position

as far as i can see it wont respond to a flag that i place, so i’m gonna try your deleted line and see if i get an error with the else line. If i do i’ll delete that as well

Thanks for the help!

I’m looking at the code again and i see i have made a variable out of ready = cleave. But in the code i never use the ready variable. So it shouldn’t use it anyway right?

Correct. The “ready = cleave” variable is not needed at all and can be removed. That is what is making your hero rush the enemy. I don’t usually create a variable with a command method. While I suppose it can be done, you have to consider that the command is being called when you create the variable and might create an issue like it did here.

Another extra note, you didn’t assign a variable to hero.distanceTo(flag) and I don’t see a need for that line either. It didn’t create any issues, but it isn’t being used either.

yea, well. I want to learn this stuff and need to start somewhere. Thx for the tips though! I’m waiting on the email to get premium so I can do the premium content and go back to the first island to complete it 100% first.

You are Welcome! And enjoy the premium. I bought the subscription package and it definitely helps getting the repetition. There is so much to learn and circling back to the previous levels helps you quite a bit. When you get a better overall understanding of the concepts and advanced commands/methods it gets quite challenging and fun. I’m still struggling through the Glacier island. Good luck!