enemy = hero.findNearestEnemy()
def shouldRun():
if hero.health < hero.maxHealth / 2:
return True
else:
return False
while True:
# Move to the X only if shouldRun() returns True
if shouldRun() == True:
hero.moveXY(75, 37)
# Else, attack!
else:
hero.attack(enemy)
im using this code, if shouldrun gives back True then move to the coordinates, if not attack. When i press play the character doesn’t even attack. He’s just standing there doing nothing.
I don’t know why but writing this post the spaces are included in the code ( with the if command) but not when i click on post thread. just an FYI
Your code works for me however there’s a couple of issues. First, avoid using global variables whenever you can. The enemy definition belongs in the while True loop. Second, you should always check for the existence of the enemy before doing anything like attacking. In this case you can do this by changing else to elif enemy.
In more detail, (I already had most of this typed up before @MunkeyShynes reply).
It acts like an if statement, but it allows you to check secondary/multiple true/false statements in a specific order. This way you can prioritize what your hero does based on the way you have it lined up. Once an if or elif statement is true it will execute that one and I believe skip the rest.
if - if true it will execute this one and skip the rest, if false goes on to the next one
elif - when the first if statement is false, it will check this one and execute if true or continue
elif - when the first two are false, it will check this one
elif - you get the idea - you can have as many as you want
else: if there is an end condition after all the checks you want finish it here/ not always needed