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:

1 Like

Thanks :slight_smile: Glad to be here (:

1 Like

I’m late to this post, but this is mine, 35 codelines.

MOD edit: solution removed.

1 Like

Hi PuChiNg,

Welcome to the forum!

You appear to have posted working code - appreciate that you’re trying to help, but it’s against the forum rules.

It’s much better if people only post code that isn’t working, then we can try to solve the problems and make suggestions for how to improve it - that way they learn how to code rather than practising their copy & paste skills.

Please could you delete your code?

Thanks,

Jenny

1 Like