Mind the Trap Python

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.

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

1 Like

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.

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

Oi! ( 20 characters)

1 Like
# 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

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?

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

while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    Distance = hero.distanceTo(enemy)
    if flag:
        # Pick up the flag.
        hero.pickUpFlag(flag)
    elif enemyDistance < 10:
        # Only attack if the enemy distance is < 10 meters
        hero.attack(enemy)
        hero.attack(enemy)

Whats wrong with my code?

Different names for variables. Change the enemyDistance to Distance

У меня он убил одну сторону врагов а других не видит и они не подходят ,вследовательно герой стоит на месте и ничего ни делает…

while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    if flag:
        # Возьми флаг.
        hero.pickUpFlag(flag)
    elif enemy:
        # Атакуй врага, если он находится на расстоянии < 10 метров
        distance = hero.distanceTo(enemy)
    if distance < 10:
        hero.attack(enemy)

Чего то тут не хватает ,подскажите чего?