Backwoods Forest Code Python


#1

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.


#2

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()


#3

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


#4
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.


#5

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


#6

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


#7
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.


#8

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


#9

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