Stuck In Shrapnel Level


#1

Here are mu codes. It throw but it wont shoot arrows once enemy get close

loop:
    enemy = self.findNearestEnemy()
    if enemy:
        if self.isReady("throw"):
            distance = self.distanceTo(enemy)
            # Only throw if the ogres are more than 15m away.
            # Use "if" to compare distance to 15.
        if distance > 15:
            self.throw(enemy)
            # Use "else" to attack if you're not throwing.
            
        else:
            self.attack(enemy)

#2

Hard to be sure what the issue might be - is it possible to get all the code into the code format?
Its possible everything is inside the isReady if block?


#3

The codes are properly indented. It just wont show up here. Now everything works fine using the same code, The arrows are firing when the enemies get close. However, by the time the third group of ogres arrived, I keep dying because it only fire once and just stood there doing nothing waiting for the ogre club me to death. Why is it not going through the loop progression and fire again?


#4

I have put triple-backticks around your code so that it will have the code format.

It looks like you need to indent your distance > 15 check so that it’s in the same if-statement that you created the distance variable in.

You also need two else statements, to attack either if the distance is too close, or if the throw isn’t ready.


#5

This is what I did and it seems to work for a while but (spolier alert :stuck_out_tongue:) my character dies at the end still…just can’t think what I’d need to do to fix it. Any help appreciated pls. (i have tried to emulate the expected Indentation.

while True:
enemy = hero.findNearestEnemy()

    if enemy:

            if hero.isReady("Throw"):
    distance = hero.distanceTo(enemy)
                           if distance > 15:
                                    hero.throw(enemy)

                    else:
                          if distance < 15:
                                    hero.attack(enemy)

#6

I would do code another way
maybe if hero.isReady(“throw”) and hero.distanceTo(enemy)>15 ----> throw, else - attack ?

but if your enemy is too close then you will newer throw.

maybe better think of different strategy?


#7

No the code was absolutely right it was just my gear that needed changing. :slight_smile: