Guys I don’t get how to use arrays and a while cycle.
My guy that stand one place and doing nothing.
# Use while loops to pick out the ogre
loop:
enemies = self.findEnemies()
enemyIndex = 0
# Wrap this logic in a while loop to attack all enemies.
enemy = enemies[enemyIndex]
# "!=" means "not equal to."
if enemy.type != "sand-yak":
# While the enemy's health is greater than 0, attack it!
pass
# Between waves, move back to the center.
so I did by my own
loop:
enemies = self.findEnemies()
for enemy in enemies:
if enemy.type != 'sand-yak':
self.attack(enemy)
self.powerUp()
self.bash(enemy)
and i kill all stinky ogres in 35.3s (2nd place for today at this level, guys)
I am also having trouble with this level and I can’t seem to figure out which variable to use with the while loop. Can someone help me?
This is my code
# Use while loops to pick out the ogre
loop:
enemies = self.findEnemies()
enemyIndex = 0
# Wrap this logic in a while loop to attack all enemies.
while enemyIndex:
enemy = enemies[enemyIndex]
# "!=" means "not equal to."
if enemy.type != "sand-yak":
# While the enemy's health is greater than 0, attack it!
In this case you would be using the index, since it is the least complicated and the other variable would [essentially] be index based anyway so…
index = somenumber
while index compares to something
do stuff with index
inc/dec-rement index
do other stuff
If you only use “index” near the top of the while loop, I’d suggest inc/dec-rementing it there, so you know it is done, because if you forget —> inf-loop!!
# Use while loops to pick out the ogre
loop:
enemies = self.findEnemies()
enemy = self.findNearest(self.findEnemies())
enemyIndex = 0
# Wrap this logic in a while loop to attack all enemies.
while enemyIndex < 10:
enemy = enemies[enemyIndex]
# "!=" means "not equal to."
if enemy.type != "sand-yak":
while enemy.health > 0:
self.attack(enemy)
pass
# Between waves, move back to the center.
RIght now, the loop: is blocking your code. All that’s happening is the first three commands happening over and over again. Also, in Python, whitespace actually matters, so indentations are important. Fix those, then come back if you still have an error.
# Use while loops to pick out the ogre
enemyIndex = 0
while enemyIndex < 10:
# "!=" means "not equal to."
if enemy:
if enemy.type != "sand-yak":
while enemy.health > 0:
self.attack(enemy)
enemyIndex += 1
else:
self.moveXY(40, 31)
# Use while loops to pick out the ogre
enemyIndex = 0
while enemyIndex < 10:
enemy = self.findNearest(self.findEnemies())
# "!=" means "not equal to."
if enemy:
if enemy.type != "sand-yak":
while enemy.health > 0:
self.attack(enemy)
enemyIndex += 1
else:
self.moveXY(40, 31)
you only increment the index when the enemy is not a sand-yak . . . . so if it is a sand-yak it loops infinitely checking and rechecking and rerechecking the same sand-yak.
Then you want to loop through all the enemies, no matter how many there are (not 10):
# start counting the enemies from zero:
enemyIndex = 0
# while we check all enemies (0, 1, 2, 3...) do this:
while enemyIndex < len(enemies):
# len(enemies) tells how many elements (=enemies) are in the "enemies" list
enemy = enemies[enemyIndex]
# enemies[0] is the first element (=enemy) in the list
# enemies[1] is the second, etc...
Then you check if the enemy is not a sand yak, attack it until it’s dead and increase the index (= the “counter”): this mostly done in your code.
Finally when you went through all the enemies (and either killed them or skipped them), go back to the center. Note: this should be outside your while block!
Oh, and wrap the whole code in a loop, because there are multiple waves of enemies…
The moveXY() should be part of your loop. Currently you’ll never do it, because the loop never stops.
loop:
# blabla
while ...
# blabla
moveXY(...)
You should also restructure the attacking part a bit, because currently you attack the enemy and then cast drain-life if it’s ready. However, if the enemy was killed with your attack, you’ll get an error message when you try to cast… Also, after you hit the enemy, you switch to the next one, and then the next one, and so on, without killing them.
Probably it’s better to keep it simple, as in your original code: