Backwoods Forest Code Python

I’m having problems with my code… I’ve tried to fix it but to no a vale. This is it:

loop:
self.moveXY(43,35)
self.moveXY(58,52)
enemy = self.findNearestEnemy()
if enemy:
self.attack(enemy)
else:
self.moveXY(43,35)
self.moveXY(58,16)
self.findNearestEnemy()
if enemy:
self.attack(enemy)
else:
self.moveXY(40,31)

He doesn’t attack the second munchkin in the bottom self.findNearestEnemy()
if enemy:
self.attack(enemy)
I can’t figure it out but I’m a beginner.

1 Like

Its hard to read your code. Can you please format it correctly by adding 3 backtick before and after the code or press the button that looks like </> at the top of the text editor.

The reason its not attacking is because all you called was self.findNearestEnemy() You need to make it look like the line on the top where it says enemy = self.findNearestEnemy()

1 Like

Ah. I’m sorry its my first time. Thanks I’ll try it and see if it works.

1 Like
loop:
    self.moveXY(43,35)
    self.moveXY(58,52)
    enemy = self.findNearestEnemy()
    if enemy:
        self.attack(enemy)
    else:
        self.moveXY(43,35)
        self.moveXY(58,16)
        enemy = self.findNearestEnemy()
        self.findNearestEnemy()
        if enemy:
            self.attack(enemy)
        else:
            self.moveXY(40,31)

Problem is that he goes to the top X and kills the munchkin and then he goes to the first X and back to the top one. By the time he gets to the bottom one the munchkin kills the other guy and then my guy kills him.

1 Like

Ty_ler can’t help you rn cause he’s in college,but he’ll reply to you when he would have time.

1 Like

Ah, Ok I tried the code but he went to the top X marker and then went back to the starting spot.

1 Like
loop:
    self.moveXY(43, 35)
    self.moveXY(58, 52)
    enemy = self.findNearestEnemy()
    if enemy:
        self.attack(enemy)
        self.moveXY(43, 35)
        self.moveXY(58, 16)
        enemy = self.findNearestEnemy()
        if enemy:
            self.attack(enemy)

If you don’t do the else it should just go to that next command and do both commands without any problems.

1 Like

Thanks it worked perfect. I didn’t know you could get rid of else statements and replace it with and if.

1 Like

Just don’t have an else statement after an if and it will still work and be fine.