Stuck on Oasis Level


#1

Hey guys sorry to bug you but I can’t figure this one out. My code seems to be right but it says I am not defining x. Here is my code.

while True:
    newX = hero.pos.x 
    newY = hero.pos.y
    enemy = hero.findNearestEnemy()
    if enemy and hero.distanceTo(enemy) < 10:
        # Subtract 10 from x to move left.
        hero.moveXY(x - 10, y)
        # Use moveXY to move to the new x, y position.
        hero.moveXY(newX, newY)
        pass
    else:
        # Add 10 to x to move right.
        hero.moveXY(x + 10, y)
        # Use moveXY to move to the new x, y position.
        hero.moveXY(newX, newY)
        pass

Help would be appreciated

Thanks!


#2

Do you mean to use newX instead of x? :wink:


#3

The same applies for y you are using x and y which aren’t defined try either defining x and y or using newX and newY instead.


#4

First add 10 to newX with += to find where you need to move.
Then, you could use something like hero.moveXY(newX, newY).