CodeCombat - Basin Stampede

Could somebody please help me with this level? I don’t know what’s wrong with it!

My code:

while True:
    enemy = hero.findNearestEnemy()
    xPos = hero.pos.x + 5
    yPos = 17
    if enemy:
        # Adjust y up or down to get away from yaks.
        if enemy.pos.y > hero.pos.y:
            # If the Yak is above you, subtract 3 from yPos.
            xPos = hero.pos.x
            yPos = hero.pos.y - 3
            pass
        elif enemy.pos.y < hero.pos.y:
            # If the Yak is below you, add 3 to yPos.
            xPos = hero.pos.x
            yPos = hero.pos.y + 3
            pass
    hero.moveXY(xPos, yPos)

Help, please! (20 char)

OK. I figured out what’s wrong with your code. When you see the yak, you make xPos as your hero.pos.x. This makes it so that you try to move on yourself. If you delete both xPos = hero.pos.x, you should be fine. Try that and see if it works

Does that mean I should delete “hero.pos.x?”

And then have just “xPos?”

no. you can just delete that entire line

because that lines says that your goal x point is yourself, so you don’t want that. you want to reach the end of the oasis

I tried it, but the Yaks still hit me!

My code ended up like this:

# Keep moving right, but adjust up and down as you go.

while True:
    enemy = hero.findNearestEnemy()
    xPos = hero.pos.x + 5
    yPos = 17
    if enemy:
        # Adjust y up or down to get away from yaks.
        if enemy.pos.y > hero.pos.y:
            # If the Yak is above you, subtract 3 from yPos.
            yPos = hero.pos.y - 3
            pass
        elif enemy.pos.y < hero.pos.y:
            # If the Yak is below you, add 3 to yPos.
            yPos = hero.pos.y + 3
            pass
    hero.moveXY(xPos, yPos)

Help, please! (20 char)

HELP @CryptiCrystal!!!

Anyone out there? (20 char)

You don’t want to subtract from the hero’s position. You want to subtract from yPos. Same with the addition, you want to add yPos not your hero’s position.

You need to subtract from yPos and not from hero.pos.y if I remember right. Check the comments