Code Help for level: Seige of Stonehold

Here is my code, I do’nt know what I should do now, it is not working.

loop:
    enemy = self.findNearestEnemy()
    flag = self.findFlag("green")
    target = enemy

    if flag:
        flagpos = flag.pos
        fx = flag.pos.x
        fy = flag.pos.y
        self.pickUpFlag(flag)

    elif enemy:
        self.distanceTo(target)
        self.findNearestEnemy()
        self.attack(enemy) 

    elif enemy:
        enemy = self.findNearestEnemy()
        distance = self.distanceTo(enemy.pos)
         if distance <4:
            self.isReady("cleave")
            self.cleave(enemy)

In my opinion " Seige of Stonehold " lvl is one of the lvls ppl should not ask for help to pass it , if you could not pass then go back to the previous lvls and look at the codes and how you passed them then come back to " Seige " and try again untill you pass it , that would make you understand any code later and will give you a good Ideas later , sorry if i was rude ro something but i only want to help you

and finally , self.move(fx.fy)

Just try your best and check all those different levels. This level took me about 4 hours or so.

A couple thoughts about troubleshooting code.

  • You seem to have a lot of code that either does nothing or is a duplicate of something you already handled.
    – Are the fx and fy variables actually used anywhere?
    – You define “enemy” twice.
    – Sometimes you use “enemy” and “target”. Are they different?
    – You have 2 “elif enemy:” statements. Is there a reason for that?

  • There are no comments.
    – When seeking help with your code, comments help explain what you wanted to happen.

  • You didn’t explain what’s happening.
    – We’d love to help but “it is not working” isn’t enough information to go on.
    – “The character is just standing there” is not the same as “it is not working”.

The main problem is that you have this:

        distance = self.distanceTo(enemy.pos)
         if distance <4:
            self.isReady("cleave")

The distance = ... line and the if distance < 4: line don’t line up, so you get an indentation error.

You also want to check if cleave is ready, which you can use an and operator to do:

        distance = self.distanceTo(enemy.pos)
        if distance < 4 and self.isReady("cleave"):
            self.cleave(enemy)