# Soldiers will slowly arrive, but the ogres will overwhelm them.
# A basic attack loop isn't going to be enough to keep you alive.
while True:
flag = self.findFlag()
enemy = self.findNearestEnemy()
if flag:
self.pickUpFlag(flag)
if(self.distanceTo(enemy) <5):
if(self.isReady("cleave")):
self.cleave(enemy)
else:
self.attack(enemy)
self.attack(enemy)
else:
self.moveXY(49, 37)
if(self.distanceTo(enemy) <=5):
if(self.isReady("cleave")):
self.cleave(enemy)
else:
self.attack(enemy)
self.attack(enemy)
else:
self.moveXY(49, 37)
Note what it is saying is ‘null’. You are checking for the distance to an enemy, which may, or may not exist.
Further you say your code is ‘slow,’ but if there is no flag, you are repeatably moving back to a point before each attack. This could slow down your code.
while (enemy): Is your problem. You have a loop in a loop. As long as there is an enemy on the screen your character will wait and check to see if there is one within 5 and ignore all other commands. This will happen 3000000 times in a few seconds. Delete the second loop. You don’t need it.
Also you can simplify your code to make it easier to read with 2 little steps. elif:
is the same as
else:
if:
Using the word “and” lets you check for multiple things in the order listed. The same as:
if:
if:
Simplified it would look like this (which I find much easier for me to read and makes it more likely I won’t make mistakes)
elif enemy and self.distanceTo(enemy) <=5:
if self.isReady("cleave"):
self.cleave(enemy)
else:
self.attack(enemy)