Having Trouble with "Medical Attention" [Python]


#1

Whenever I try to put in “hero.findNearestEnemy” it never works.

Here is my code:

while True:
    
    currentHealth = hero.health
    healingThreshold = hero.maxHealth / 2
    enemy = hero.findNearestEnemy()
    # If your current health is less than the threshold,
    # move to the healing point and say, "heal me".
    # Otherwise, attack. You'll need to fight hard!
    if currentHealth < healingThreshold:
        hero.moveXY(65, 46)
        hero.say("heal me")
    else:
        
        hero.powerUp()
        hero.attack(enemy)
    

Can anyone help me?


#2

You should get in the habit of always checking for an enemy before attacking, otherwise if there is not an enemy when the code iterates through the attack section, it will return a null error. You’ve already defined enemy as hero.findNearestEnemy() so there’s no need to use this method again. Try this:

else:
    if enemy:  # <---  More often than not this is a good idea before attacking
        hero.powerUp()
        hero.attack(enemy)

#3
else:
    if enemy:
        if hero.isReady("powerup"):
            hero.powerUp()
        else:
            hero.attack(enemy)

That works better, since @MunkeyShynes’s code would force the hero to powerUp even if it is not ready. (which means the hero can’t do anything until it powerUps again)


#4

btw try getting the sword of the temple guard if you have enough gems. It will really help with the levels. Just a suggestion.


#5

Oh I only have 412 gems. Should I just save up for better armor then?


#6

save up for better sword first, then better armor. (actually, get the dragon chestplate first, then the sword, then the helm)


#7

That’l take me like 20 levels each, but ok.


#8

@Alphaspectre You could mine for gems by running simulations.