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

Combat Code evo


while True:
    green = hero.findFlag("green")
    black = hero.findFlag("black")
    violet = hero.findFlag("violet")
    nearest = hero.findNearestEnemy()
    
    if green:
        hero.pickUpFlag(green)
    elif black and hero.isReady("cleave"):
        hero.pickUpFlag(black)
        enemy = hero.findNearestEnemy()
        hero.cleave(enemy)
    elif violet and hero.canCast("chain-lightning", enemy): 
        hero.pickUpFlag(violet)
        enemy = hero.findNearestEnemy()
        hero.cast("chain-lightning", enemy)
    else:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)
        pass

anyone have any ideas to make better?

What level is this? That will help understand the complexity and approach that best suits the level.
Since you aren’t using the one variable at the beginning, you can remove it completely.

nearest = hero.findNearestEnemy() # not used so not needed

Always check for enemy before trying to attack them. If you don’t, it can lock up your code pretty quick.

enemy = hero.findNearestEnemy()
if enemy: # always want to add an enemy check before any of the attacks below
    hero.cleave(enemy)
    hero.attack(enemy)

Also, you want to make sure you are checking if you canCast a spell on the same enemy you cast the spell on. Since you check for a new enemy after the canCast check, it may switch enemies on you.

    elif violet: 
        hero.pickUpFlag(violet)
        enemy = hero.findNearestEnemy()
        if enemy and hero.canCast("chain-lightning", enemy): # make sure you are looking at the same enemy when you check and cast the spell
            hero.cast("chain-lightning", enemy)