Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Maxmaxer help pls

My person just gets swarmed by the decoys and he can’t move. Here is my code.

# Attack the enemy that's farthest away first.

while True:
    farthest = None
    maxDistance = 0
    enemyIndex = 0
    enemies = hero.findEnemies()

    # Look at all the enemies to figure out which one is farthest away.
    while enemyIndex < len(enemies):
        target = enemies[enemyIndex]
        enemyIndex += 1

        # Is this enemy farther than the farthest we've seen so far?
        distance = hero.distanceTo(target)
        if distance > maxDistance:
            maxDistance = distance
            farthest = target

    if farthest:
        target = hero.findNearestEnemy()
        targetHealth = hero.findNearestEnemy
        hero.moveXY(target.pos.x, target.pos.y)
        # Take out the farthest enemy!
        # Keep attacking the enemy while its health is greater than 0.
        while targetHealth > 0:
            hero.attack(target)
        pass

You are defining farthest twice…and you don’t really need the moveXY statement; the hero will automatically move toward the target, if it is out of weapon range.

In your if farthest: code block, try commenting out the target, targetHealth and moveXY lines and see what happens.

It worked, thanks for helping me

1 Like

You are very welcome! If you would, please mark this as solved, so others will know and won’t spend time trying to resolve it.