[Desert] Sarven Shepherd - Hard execution limit of 3000000 exceeded

Hi o/

I’m blocked in this level :confused:
My character just stand, do nothing else.

# 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 < len(enemies):
        enemy = enemies[enemyIndex]
        # "!=" means "not equal to."
        if enemy.type != "sand-yak":
            # While the enemy's health is greater than 0, attack it!
            while enemy.health > 0:
                self.attack(enemy)
                enemyIndex += 1
        break
    # Between waves, move back to the center.
    self.moveXY(39, 32)

Are you sure about the position of the line enemyIndex += 1?
With this code, you are increasing enemyIndex after each attack to enemy if it is not a sand-yak. Just after enemy is dead, break makes you exit the while loop.
If you find a sand-yak, again, you break the first while loop and in any case you do not check all the enemies.

Can you correct my code so i can see ?

For the enemyIndex += 1, i don’t really see how putting him. I just assume i put on for every ennemies. I mean, it’s what i though.

So, i have for the yak, i have to put something like:

if enemy:
if enemy.type != “sand-yak”:

Could this work ? Because i check if there enemy, and if it’s not a yak.

You don’t need to use a double if, you can use the and operator to connect

if enemy and enemy.type != "sand-yak":

In this way if there is an enemy, you check if the type is correct.

Then, you have just to remove the break and move enemyIndex += 1:

            if enemy.type != "sand-yak":
                # While the enemy's health is greater than 0, attack it!
                while enemy.health > 0:
                    self.attack(enemy)
            enemyIndex += 1

In this way, if you find a sand-yak nothing happens, but if you find an ogre you attack until it’s died and then you go the following element of enemies.

1 Like

Thanks for your help.

In the game, we don’t really learn these thing, we just have a level we they are showed, and after, we have to use it directly.
I hope some others chapter won’t be learned that way, because it’s pretty hard.

This forum is part of the learning process :wink:

Ok :smiley:

Next school years, i’ll be in a formation about web programming.
So i’ll ear a lot of HTML/CSS, a little part of Java/Javascript, and some server based langage.

(I know Python have nothing to do with, but i planned to learned some others like Python (and i like Python because it’s not really hard)

loop:
enemies = self.findEnemies()
enemyIndex = 0

# Wrap this logic in a while loop to attack all enemies.
while enemyIndex < len(enemies):
    enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak":
        # While the enemy's health is greater than 0, attack it!
        while enemy.health > 0:
            self.attack(enemy)
    enemyIndex += 1
   
# Between waves, move back to the center.
self.moveXY(39, 32)

The code above has been corrected, but I would suggest you think about a couple of things as it is not a great answer unless you are over geared for the level.

  1. Is there a better order in which to attack the mobs?
    Currently, you could end up zigzagging all over the field. Do you have access to findNearest(), the health property and/or distanceTo() for example?
  2. When your targets are outside of your current range, in which direction should you move or use moveXY?
    If you do not have access to .move, think about what this does:

Fx = (self.pos.x * 9 + enemy.pos.x)/10
Fy = (self.pos.y*9 + enemy.pos.y)/10
self.moveXY(Fx, Fy)

You might also look into Vector math in terms of deciding where to go next. I will admit to that maybe being overkill for this level, but it will come in handy later.

Apologies on formatting above, on )$&&@: tablet device. Sigh … also just realized how old this thread was. Apology for the necromancy.