Thunderhooves plz help (python)


#1

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)

#2

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.


#3

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


#4

I’m not sure why but it didn’t put my post in code format sorry


#5

Did you not read my post above? It has the answer you seek. Heck, I’ve practically written the code for you.


#6

OK I’ll try it and see what happens


#7

It worked out well and completed the level. Thanks!!!