Mind the Trap Python


#1

If you try to attack a distant enemy, your hero will charge toward it, ignoring all flags.

You’ll need to make sure you only attack enemies who are close to you!

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)

I’m not sure what I’m doing wrong. Hero won’t attack.


#2

If you’re trying to look for the distance between you and the enemy, you have to put down hero.distanceTo(enemy) in order for the computer to understand the distance between you and the enemy


#3

What is if enemy < 10:? To check a unit’s distance from the hero, you can use a method called hero.distanceTo(unit). In this case, the unit is the enemy.


#4

Lol I literally responded at the same time as you :joy:


#5

Oi! ( 20 characters)


#6
# If you try to attack a distant enemy, your hero will charge toward it, ignoring all flags.
# You'll need to make sure you only attack enemies who are close to you!

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 hero.distanceTo(enemy) < 10:
            hero.attack(enemy)

please help


#7

I have a few questions because the code is running but not completing. Please describe what is happening when you run your code. Also:

  1. What glasses are you using?
  2. What is your total health at the start?
  3. What sword are you using?

#8

don’t say “I should pick up the flag” because that’s going to give your hero a 1 second delay.