# Find a problem about the stage: ThunderHooves, Sarven

#1

I don’t know what’s problem with my code, but every time I run it, my character always build two fences when he passing by the second yak, then got himself stucked…
What should I do to make it right?
I’m sorry if my expression has any problem. English is not my native language but I’m learning it
Here is my code:

``````loop:
yak = self.findNearestEnemy()
if yak:
# If the yak is above you, build a fence 10m below it.
if yak.pos.y > self.pos.y:
self.buildXY("fence", yak.pos.x, yak.pos.y)
# If the yak is below you, build a fence 10m above it.
if yak.pos.y < self.pos.y:
self.buildXY("fence", yak.pos.x, yak.pos.y + 10)
pass
else:
# Move right 10m towards the oasis.
self.moveXY(self.pos.x + 10, self.pos.y)
pass``````

#2

You are on the right track. I see “+10”. Maybe you also want “-10” somewhere?

#3

Right! That’s a fault I made. Thanks:)
But the second yak come from bottom of screen, so though I fixed my code, the problem still happened as usual…

#4

Hey man, what’s up? try with differents boots, I think the problem is the speed of your character.

#5

no I dont think that is it I think he needs to build the fences closer to the yak and farther from Tharin

#6

You’re right! It is speed’s problem. But I’ve already equiped the best boot that I unlock yet. So I change my character to Hanzo. and problem solved. Thanks!

#7

I’ve tried that by change the parameter of distance, but it won’t help…so I changed a faster character Hanzo. then problem solved. Thanks anyway

#8

Glad you solved it. You don’t need hanzo but maybe you need the soft leather boots which are +2.5 on speed instead of +2 like other boots.

Another thing is that you could make your last “if” an “elif”. (my code is exactly like yours, except for that). Then you could only build a fence above or below, not both, regardless of speed. I wonder if b/c you were going too slow that you were finding the second nearest yak (not directly above you but rather above and to the right).

btw, “pass” is just a placeholder for an empty loop. You can take those out once you fill in with your real code.

#9

oh I see it now you are building the fences on both sides if you only build them on one you would not have the problem of getting stuck

#10

Thanks for the tips of code. I’ve tried it and it does work better then before. But this time some strange things happened.
When I use Tharin with soft leather boots, he missed the third yak and just go ahead. then hit by that yak. But things went right as long as I equipped boots which +2 on speed. I have no idea about what’s wrong with it: (
here’s what happened:

Thanks for the help.

#11

Yeah. I tried “elif”, then voila, problem solved:)

#12

Bringing this one up again, I am pretty sure I have the correct code, but I’m posting it here just in case, (gotta look up how to fuzz it out real quick).

My problem is, after the first yak who I successfully block whether it comes from the top or the bottom, my character runs past the second yak, ( whom ALWAYS comes from the bottom no matter how many times I’ve reloaded the page and resubmit) and I head to the bottom of the 2nd yak’s area, where I keep building walls.

To me, it looks like my character is targeting the scorpion, or bones, or something down there that can be identified as an enemy. (I don’t know if they can be or not, I’m just trying to make sense of it) If so, I’ve forgotten how to identify enemies, will targeting the yaks only fix that?

Otherwise, may I get some help as to where my code may be wrong? I’ve also tried elif on the 2nd if, and a few other changes, and I don’t have access to hanzo or other boots right now to try if a different speed will help.

code: (if indents are off, it’s because I screwed them up while putting them in here)

spoiler
``````# Get to the oasis,
# fencing off paths with yaks on them as you go.
loop:
yak = self.findNearestEnemy()
if yak:
# A yak is above you if its y is greater than your y.
# If the yak is above you, build a fence 10m below it.

if yak.pos.y > self.distanceTo(yak):
self.buildXY("fence", yak.pos.x, yak.pos.y - 10)
# If the yak is below you, build a fence 10m above it.
if yak.pos.y < self.distanceTo(yak):
self.buildXY("fence", yak.pos.x, yak.pos.y + 10)

else:
# Move right 10m towards the oasis.

self.moveXY(self.pos.x +10, self.pos.y)
``````

#13

welps, cant figure out how to work spoiler apparently, even when looking at examples.

#14

Well, you’re comparing apples to pears:

``````if yak.pos.y > self.distanceTo(yak):
``````
• `yak.pos.y` is the yak’s y-coordinate
• `self.distanceTo(yak)` is the distance between you and the yak

You need to compare your position with the yak’s position:

``````if yak.pos.y > self.pos.y:
``````

Furthermore, the second `if` statement could be simply replaced by an `else`:

``````if yak.pos.y > self.pos.y:
# do this
else:      # when yak.pos.y <= self.pos.y:
# do that
``````

#15

Yup, first thing was the prob, and now I understand. Funny how 30 mins of combing over that code yielded nothing. I’m in for a bright future!

Thanks Ant, I hope your movie does well!

#16

the problem is that ``` and [spoiler] don’t like each other. so you have to use [details=blahblah] if you want to have nicely formatted code in your spoiler. (I editted your mesage to use details)