You define enemy
outside the loop. That means, to your character, there is only one enemy. Move it inside the loop. That should work.
I did that, but now I wont go to the ambush point.
The while-loop quits as soon as the condition is false. Therefore, you donât need that if-statement. Delete it and move your moveXY
command to the 0th level of indentation, that is, no indentation.
Also, I wouldnât cleave, as that will kill off a bunch of enemies and still count as one hit.
hits = 0
while hits < 10:
enemy = self.findNearestEnemy()
self.attack(enemy)
hits + 1
if self.isReady("throw") and self.distanceTo(enemy) < self.throwRange:
self.throw(enemy)
hits + 1
self.moveXY(79,33)
With this code it is attacking forever
Edit : I removed the loop
I could be wrong but I keep seeing a âloop:â with no way to leave it once you go inâŚ
you do see a âloopâ in my code.
That loop does not need to be there. The while-loop will repeat as long as the condition is true. Remove the loop:
and your program should work.
I removed it and its still not working. I also edited my code. now its
hits = 0
while hits < 10:
enemy = self.findNearestEnemy()
self.attack(enemy)
hits + 1
if self.isReady("throw") and self.distanceTo(enemy) < self.throwRange:
self.throw(enemy)
hits + 1
self.moveXY(79,33)
What happens when you run your code?
Omarn just throws and attacks until he dies.
This is not valid it will not increase hits and so the while
loop never stops):
hits + 1
What you mean is:
hits += 1 # or the long version: hits = hits + 1
You could also improve a bit the structure of your code. This is what it currently does:
- attack
- increase hits
- if throw is ready: throw and increase hits
You could do something like:
- if throw is ready: throw
- otherwise (else): attack
- increase hits
Thanks! Now i beat this level.
i need help, my hero just goes to attack one munchkin and goes to the point right after.
what is wrong
Use hits == 10
instead of hits = 10
âthe ==
is the equality comparison and the =
is the assignment operator. (Programming is great, right?)
yes it is great and awesome
i didnât need to use that nick, i just needed to put a greater than sign before the equal sign on line 9
Actually you donât âneedâ the entire âif - breakâ as it is totally redundant . . . that is the job of âwhile < 10â
(yes, it is pseudo/short-hand code)
loop:
blah
blah
if >= 10:
break
is the same as
while < 10:
blah
blah