im still trying to pass the final kithmaze but its saying that I have to pass an array of units to find nearest in line 6. here is my code.
loop:
self.moveRight()
self.moveUp()
self.moveRight()
enemy = self.findNearest(enemy)
self.attack(enemy)
self.attack(enemy)
self.moveDown()
self.moveDown()
self.moveUp()
To use findNearest
, you need to give it some objects to get the nearest from. For example getEnemies
.
Code would look like this:
loop:
#move
enemies = self.findEnemies()
enemy = self.findNearest(enemies)
#attack
#move
To be sure, you actually have to write a bit more code. It could be that there are no enemies around, in which case findNearest
returns None
(or something like that, but you can’t put it in findNearest
).
This actually looks like this:
loop:
#move
enemies = self.findEnemies()
if enemies:
enemy = self.findNearest(enemies)
#We know that an enemy must exist, otherwise enemies would have been None
self.attack(enemy)
#move even more
Actually, this is not quite correct. I’m fairly certain that enemies returns True even if it is an empty list. Rather, you should be checking if enemy:
#move
enemies = self.findEnemies()
enemy = self.findNearest(enemies)
if enemy:
#We need to check if an enemy exists.
self.attack(enemy)
#move even more
Now, maybe I’m wrong and both will work, but this is my method.
dwhittaker, that’s my understanding too. I’m pretty sure if there are no enemies in the array, then a null value is loaded into enemy (which doesn’t break the script).
That looks fine for that level. What glasses is your hero currently wearing? The pair of glasses is going to determine which way you find your nearest enemy. If you are using any pair of the wooden glasses, then that code should work.
If you are wearing the mahogany glasses, then you need to first load the array and then search it for the nearest enemy (as the scripts above are doing).