On the ‘if enemy.type = “sand-yak”’ statement, it highlights “sand-yak” and says that “tmp24 is undefined”.
What is tmp24?!?!
loop:
enemies = self.findEnemies()
enemyIndex = 0
enemy = enemies[enemyIndex]
if enemy.type = "sand-yak":
continue
if enemy.type = "ogre":
while enemy:
self.attack(enemy)
if not enemy:
enemyIndex = enemyIndex + 1
break
- Use a double == in a if test. You are testing an equality == not assigning a value =
if enemy.type == "sand-yak":
continue
-
enemies is a list. You may want to go through every enemy in the list. You can do that by putting your code inside another loop that goes over the each element in the list:
loop:
enemies = self.findEnemies()
for enemyIndex in range(len(enemies)):
enemy=enemies[enemyIndex]
if enemy.type == "sand-yak":
#and the rest of your code follows
-
while enemy
is not good enough. If you want to attack an enemy as long as it is still not dead, check his health. If he still have HP left, then he needs more kicking:
while enemy.health>0 :
self.attack(enemy)
-
Get rid of the if not enemy:
code, it does not do anything useful in that place.
-
Check a python tutorial for more info about lists:
http://effbot.org/zone/python-list.htm
or:
http://www.tutorialspoint.com/python/python_for_loop.htm
Do not use “=”, use "=="
Also about the logic. What if the first enemy is yak? Then you’ll newer change the enemyIndex!
Well, I tried that and it highlighted “enemy.type” in the if sand-yak statement, and said that “tmp85” was undefined.
enemies = self.findEnemies()
enemyIndex = 0
loop:
for enemyIndex in range(len(enemies)):
enemy=enemies[enemyIndex]
if enemy.type == "sand-yak":
continue
if enemy.type == "ogre":
while enemy.health>0:
self.attack(enemy)
if enemy.health<0:
enemyIndex = enemyIndex + 1
break
Your if
statements are not inside the for
loop. You should indent both if
statements (more specifically, everything after the enemy=enemies[enemyIndex]
line) one level further.
Just in case, you should also change enemy.health<0
to enemy.health<=0
so you don’t get an infinite loop in case the enemy hits exactly zero health. Another simpler alternative would be to remove the <= 0
check and simply de-indent the index increment so it runs after the loop has ended, e.g.:
while enemy.health>0:
self.attack(enemy)
enemyIndex = enemyIndex + 1
Another problem: you are never incrementing the index when the enemy type is sand-yak
, this will also get you an infinite loop as your loop will never advance past the given sand-yak.
Another problem: you only get a list of enemies at the very beginning of the simulation (enemies = self.findEnemies()
), it should be inside the loop instead so that you can find enemies that have spawned after the first game frame.
Another (possible) problem: you are only attacking enemies whose type is "ogre"
. I think you want to attack everything that is not a sand-yak, so you can just replace if enemy.type == "sand-yak":
with an else:
statement.