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

Lvl: magic-exam How else can the code be optimized?

How else can the code be optimized?
Thank you.

hero.moveXY(18,40)
targetPos = hero.pos.x, hero.pos.y
while True:
    friendAction()
    enemyAction()
    takePotion()
    x = hero.pos.x
    y = hero.pos.y
    hero.moveXY(x, y - 16)
    friendAction()
    enemyAction()
    takePotion()
    x = hero.pos.x
    y = hero.pos.y
    hero.moveXY(x + 16, y + 16)
    friendAction()
    enemyAction()
    takePotion()


def enemyAction():
    enemy = hero.findNearestEnemy()
    if enemy:
        if enemy.type == "ogre":
            hero.cast("force-bolt", enemy)
        if enemy.type == "brawler":
            hero.cast("shrink", enemy)
        if enemy.type == "scout":
            hero.cast("poison-cloud", enemy)

def friendAction():
    friend  = hero.findNearestFriend()
    if friend:
        if friend.type == "soldier":
            hero.cast("heal", friend)
        if friend.type == "goliath":
            hero.cast("grow", friend)
        if friend.type == "paladin":
            hero.cast("regen", friend)

def takePotion():
    item = hero.findNearestItem()
    if item:
        if item.type == "poison":
            hero.cast("grow", hero)
            hero.moveXY(item.pos.x, item.pos.y)
            x = hero.pos.x
            y = hero.pos.y
            if y >= 54:
                hero.moveXY(x, y - 14)
            else:
                hero.moveXY(x, y + 15)
        if item.type == "potion":
            hero.moveXY(item.pos.x, item.pos.y)
            x = hero.pos.x
            y = hero.pos.y
            if y >= 54:
                hero.moveXY(x, y - 14)
            else:
                hero.moveXY(x, y + 15)

Hi @PotapovIP and welcome to the forum! :smiley:

Thanks :slight_smile: Glad to be here (: