A little help on this level? My character doesn’t sense the charging yaks, and therefore doens’t build the fences necessary to keep the yaks from killing me. My code is:
# Get to the oasis,
# fencing off paths with yaks on them as you go.
loop:
enemy = self.findNearestEnemy()
if enemy:
# If the yak is above you, build a fence 10m below it.
pos = enemy.pos
x = pos.x
y = pos.y
if y > 35:
self.buildXY ("fence", x, y+10)
# If the yak is below you, build a fence 10m above it.
elif y < 25:
self.buildXY ("fence", x, y+10)
pass
else:
# Move right towards the oasis.
self.moveXY (69, 32)
Ok, so you have an if else statement here. Think of it from the computer’s point of view. You start the level. Is there an enemy right now? What are your instructions if there is no enemy? Don’t forget that the character must complete each instruction before moving to the next.
Thanks BunchofCrooks! I finished the level, but I don’t think I did it the most efficient way.
Here’s my code:
loop:
enemy = self.findNearestEnemy()
if enemy:
# If the yak is above you, build a fence 10m below it.
pos = enemy.pos
xa = pos.x
ya = pos.y
if ya > 35:
self.buildXY ("fence", 59,34)
# If the yak is below you, build a fence 10m above it.
elif ya < 25:
self.buildXY ("fence", xa, ya+10)
else:
# Move right towards the oasis.
pos = self.pos
xa = pos.x
ya = pos.y
self.moveXY(xa + 5, ya)
When the yaks are above me, I just placed the wall down where I knew the yak would come from, not using the character’s if-else logic. For some reason, when I used the same code as when the yaks come from the bottom, my hero always died. Also: when all the yaks are trapped, I complete the level, but my hero says “I cant’ get there”.
I feel like there should be a way to stop people from just putting down walls where the yaks will come from, without using if-logic and moving XY w/ variables. I resorted to it in the end when I couldn’t find another way out.
After checking out the other levels, I finally figured it out; i didn’t “cheat” by using buildXY with no variables, just moved only a small distance per repetition through loop. Still says “I can’t get there” at the end, but it says that I complete the level.
I messed with this for a while and figured out that if you use “if” for both fence building statements instead of using “if first and elif second” it builds fences like its supposed to. However it wont build the fence if I use “elif”.
So what then is the difference between if and else if? when do you not use else if?
also i get the message i can’t make it to the oasis too but likewise still get credit for completing the mission.
Leesburgking, there is a really little difference between if and else if. You use if if it is the first time you are using it. Otherwise, if you have used it already, use elif (or else if for JS).
Also, the point is that you get to the oasis, not that you walk off the screen. The reason your character says that he can’t get there is because he continues to move right in increments. If you tell him to stop after a certain number of times, then that won’t happen.