Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

26. Salted Earth

I am having issues again with the my code. The code below should work for this unless there is something that I am not seeing. @dedreous helped me before when I ran into this type of situation.
What am doing wrong? https://drive.google.com/file/d/1MZWwfmQ3ITaZK0MiUt-CyTV63bwnaKXf/view?usp=sharing (video might take a minute to render. )

while True:
    enemy = hero.findNearestEnemy()
    if enemy.type == "munchkin" or enemy.type == "thrower":
        hero.attack(enemy)
    item = hero.findNearestItem()
    # Check the item type to make sure the hero doesn't pick up poison!
    # If the item's type is "gem" or "coin":
    if item.type == "coin" or item.type == "gem":
        # 
        hero.moveXY(item.pos.x, item.pos.y)

Hi Jason…I’m here :wink:

The problem lies in your final moveXY statement. You should define the x and y variables separately. First, get the position of the item, then from that, derive the x component and then the y compotent.

You can then have the hero move to the corresponding x,y coordinates. (See the hint there?)

Thanks, I’ll see if I can get it to work. I arrpreciate your help. :slight_smile:

Hi Dedreous. I’m still struggling a bit.

while True:
    enemy = hero.findNearestEnemy()
    if enemy.type == "munchkin" or enemy.type == "thrower":
        hero.attack(enemy)
    item = hero.findNearestItem()
    if item.type == "coin" or item.type == "gem":
        pos = item.pos
        x = item.pos.x
        y= item.pos.y
        hero.moveXY(x, y)

Rather than using the moveXY method, try using move instead.

Think of moveXY as a fire and forget method…no other code will we executed, until the hero reaches the x,y coordinate. Move on the other hand is ‘step by step’ method, where the hero will take a step and any following code will be executed; on the next iteration of the loop, he takes another step…etc, etc.