I really need some help with this level, here’s my code:
yak = hero.findNearestEnemy()
if yak:
# If yak.pos.y is greater than hero.pos.y
if hero.distanceTo(yak) < 20:
x = yak.pos.x
y = yak.pos.y - 10
# buildXY a "fence" 10m below the yak.
hero.buildXY("fence",x, y)
# else:
else:
if hero.distanceTo(yak) > 20:
x = yak.pos.x
y = yak.pos.y + 10
# buildXY a "fence" 10m above the yak.
hero.buildXY("fence",x, y)
pass
else:
# moveXY right 10m towards the oasis.
x = hero.pos.x + 10
y = hero.pos.y
hero.moveXY(x, y)
The purpose of this level is to decide whether to build a fence to the north (yak.pos.y > hero.pos.y) or to the south (yak.pos.y < hero.pos.y). Your if and else conditionals are dependent on the distance from the hero to the yak, which is irrelevant.
Also, your code isn’t in a loop, so it’s only good for the first yak and one movement.
Oops i didn’t put the loop in the code. Here it is now:
while True:
yak = hero.findNearestEnemy()
if yak:
# If yak.pos.y is greater than hero.pos.y
if hero.distanceTo(yak) < 20:
x = yak.pos.x
y = yak.pos.y - 10
# buildXY a “fence” 10m below the yak.
hero.buildXY(“fence”,x, y)
# else:
else:
if hero.distanceTo(yak) > 20:
x = yak.pos.x
y = yak.pos.y + 10
# buildXY a “fence” 10m above the yak.
hero.buildXY(“fence”,x, y)
pass
else:
# moveXY right 10m towards the oasis.
x = hero.pos.x + 10
I’m not sure why but it didn’t put my post in code format sorry
Did you not read my post above? It has the answer you seek. Heck, I’ve practically written the code for you.
OK I’ll try it and see what happens
It worked out well and completed the level. Thanks!!!
1 Like