[SOLVED]Help! Distracting Dungeon

def moveBothTo(point):
    while hero.distanceTo(point) > 1:
        hero.move(point)
        hero.command(peasant, "move", point)

peasant = hero.findNearest(hero.findFriends())
while True:
    # Command your friend to build a decoy towards x + 1:
    hero.command(peasant, "buildXY", "decoy", peasant.pos.x + 2, peasant.pos.y)
    nextPoint = {"x": hero.pos.x, "y": hero.pos.y + 28};
    moveBothTo(nextPoint)
    # Create a new x/y dict +28 units away in the x dir:
    nextPoint = {"x": hero.pos.x + 28, "y": hero.pos.y}
    # Find the nearest enemy:
    enemy = hero.findNearestEnemy()
    
    
    if enemy:
        
        # While the enemy's health is > 0:
        while enemy.health > 0:
            # Attack the enemy:
            hero.attack(enemy)
            pass
        
        # Update the variable to the next nearest enemy:
        enemy = hero.findNearestEnemy()
        
        
    moveBothTo(nextPoint)

https://codecombat.com/play/level/distracting-dungeon?
My hero only kills the first ogre, not the second one.
Lydia

Hi Lydia,

I had the same problem.

I just put these lines in twice. Not sure if that’s how your supposed to do it, but it works!

Jenny

1 Like

@jka2706: You can emulate a do while loop
Instead of:

    enemy = hero.findNearestEnemy()    
    if enemy:
        while enemy.health > 0:
            hero.attack(enemy)
        enemy = hero.findNearestEnemy()

write:

    while True:
        enemy = hero.findNearestEnemy()     
        if enemy:
            while enemy.health > 0:
                hero.attack(enemy)
        else:
            break

Lidia is using Naria and often the default code for a warrior will not be the same for a ranger. The above code can be modified to:

    while True:
        enemy = hero.findNearest(hero.findByType("scout"))
        # not to be distarcted by brawlers or yeti        
        if enemy and hero.distanceTo(enemy) < 15:
            while enemy.health > 0:
                hero.attack(enemy)
        else:
            break
2 Likes

Thanks! It worked, but any idea why?
Lydia

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.