Double gaps - grasp ogre position (python)

Hi guys, this is my code but I don’t why didn’t work:

Get the hero and the peasant to the south.

The function move your hero down along the center line.

def moveDownCenter():
x = 40
y = hero.pos.y - 12
hero.moveXY(x, y)

The function build a fence on the right of something.

def buildRightOf(something):
# buildXY a “fence” 20 meters to something’s right.
x = 40 + 20
y = hero.pos.y
hero.buildXY(“fence”, x, y)
pass

The function build a fence on the left of something.

def buildLeftOf(something):
# buildXY a “fence” 20 meters to something’s left.
x = 40 - 20
y = hero.pos.y
hero.buildXY(“fence”, x, y)
pass

while True:
ogre = hero.findNearestEnemy()
coin = hero.findNearestItem()

if ogre.pos.x < 40:
    buildRightOf(ogre)
elif:
    buildLeftOf(ogre)
elif coin:
    hero.move(coin)
moveDownCenter()
pass

Welcome to the forum @Samu2015 ! :partying_face: This is a friendly place where you can ask help on levels, report bugs, or just chat with other coders! Don’t forget to read the guidelines if you haven’t yet. Have a great time!

Please for the next time, format your full code using this button to better help you :slight_smile:

As for this time, it’s fine

Get the hero and the peasant to the south.
The function move your hero down along the center line.
def moveDownCenter():
    x = 40
    y = hero.pos.y - 12
    hero.moveXY(x, y)

The function build a fence on the right of something.
def buildRightOf(something):
    # buildXY a “fence” 20 meters to something’s right.
    x = 40 + 20
    y = hero.pos.y
    hero.buildXY(“fence”, x, y)
    pass

The function build a fence on the left of something.
def buildLeftOf(something):
    # buildXY a “fence” 20 meters to something’s left.
    x = 40 - 20
    y = hero.pos.y
    hero.buildXY(“fence”, x, y)
    pass

while True:
    ogre = hero.findNearestEnemy()
    coin = hero.findNearestItem()

    if ogre.pos.x < 40:
        buildRightOf(ogre)
    elif:
        buildLeftOf(ogre)
    elif coin:
        hero.move(coin)
    moveDownCenter()
pass

So in both of the functions that you build in them, the x should be +20 or -20 the position of the parameter you entered in paranthesis called “something”, try that, if it didn’t work, then please post your code after editing :slight_smile:

1 Like

Please format your code, I’ve done it for you this time

# Get the hero and the peasant to the south.
# The function move your hero down along the center line.
def moveDownCenter():
    x = 40
    y = hero.pos.y - 12
    hero.moveXY(x, y)

# The function build a fence on the right of something.
def buildRightOf(something):
    # buildXY a "fence" 20 meters to something’s right.
    x = 40 + 20
    y = hero.pos.y
    hero.buildXY("fence", x, y)
    pass

# The function build a fence on the left of something.
def buildLeftOf(something):
    # buildXY a "fence" 20 meters to something’s left.
    x = 40 - 20
    y = hero.pos.y
    hero.buildXY("fence", x, y)
    pass

while True:
    ogre = hero.findNearestEnemy()
    coin = hero.findNearestItem()
    if ogre.pos.x < 40:
        buildRightOf(ogre)
    elif:
        buildLeftOf(ogre)
    elif coin:
        hero.move(coin)
    moveDownCenter()
pass

The “something” is a parameter passed in a argument later when the function is called. So you need to use something.pos.x instead of 40
I created the code for the function buildRightOf(something), you will do the same for the buildLeftOf(something)

def buildRightOf(something):
    # buildXY a "fence" 20 meters to something’s right.
    somePos = something.pos   # Find the position of something
    x = somePos.x + 20
    y = somePos.y
    hero.buildXY("fence", x, y)
    pass

Also, for the while True part, you should build for BOTH coins and ogres, instead of collecting it.

while True:
    ogre = hero.findNearestEnemy()
    coin = hero.findNearestItem()
    if ogre:
        buildLeftOf(ogre)  # ogre is an argument when the function is called
    elif coin:  # elif statment shouldn't be empty
        #???
    moveDownCenter()

I left the elif statment empty, you should build right of the coin. You can do it by yourself

Aya already did that though.

1 Like

He did not correct the while loop part.

1 Like

What? I don’t know what you mean.

The whole while-True structure is wrong, you should see he had an empty elif.

Aya’s code is the same as yours.

Can you please drop it, I do not want this to develop into a fight. Our purpose is to help him, not fighting who’s right and who’s wrong.

1 Like