[SOLVED] How to collect items in Salted Earth

When I try to collect items, I just attack the enemy. Here is my code.

Ogres are attacking a nearby settlement!

Be careful, though, for the ogres have sown the ground with poison.

Gather coins and defeat the ogres, but avoid the burls and poison!

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!
# Look for types: ‘gem’ and ‘coin’
if item.type == “gem” or item.type == “coin”:
hero.findNearestItem()

@MunkeyShynes, can you insert your post on posting code with beautiful formatting? thanks

Please post your code properly from now on so we can see the structure and formatting. Help us help you. Thanks.

Button01

You haven’t written any code to move to an item’s position. Yes, you have defined item and you are locating items, but unless you move to the item’s position, you aren’t going to collect items.

The last line does nothing and should be replaced with a move or moveXY method.

3 Likes
# Ogres are attacking a nearby settlement!
# Be careful, though, for the ogres have sown the ground with poison.
# Gather coins and defeat the ogres, but avoid the burls and poison!

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!
    # Look for types: 'gem' and 'coin'
    if item.type == "gem" or item.type == "coin":
        hero.findNearestItem()
        

Better?

Also, how would you collect things?

Wait, when I put a movement command, he runs into poison.

What did you use to move? If you click on any of the methods, you can see the proper format to use them.

The idea here is not to move to a position where there is poison. So, in other words, you want to move to an item’s position as long as it’s not ( != ) poison.

1 Like

I finished the level

THANKS

My hero won’t even go to the coin or poison.

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 == "coin" or item.type == "gem":
        # Then move and pick it up:
        itemX = itemPosition.x
        itemY = itemPosition.y
        hero.moveXY(itemX, itemY)

It should say, if item.type == “coin” and you’ve not defined itemX or itemY properly (item.pos.x, item.pos.y)

How should I define them then.

How would I put that into my code.

Use those in your code as the definitions of itemX and itemY.

Another way to do it is to define position first, then define X and Y so it looks like this:

pos = item.pos
x = pos.x
y = pos.y
hero.moveXY(x, y)

Another way to do it is combine all that in one line of code:

hero.moveXY(item.pos.x, item.pos.y)

Personally, I usually use this way because it’s just one line of code but they all accomplish the same thing.

1 Like

I tried both ways and both didn’t work.

Post your updated code so we can see what the problem is.

Sorry,

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 == "coin":
        # Then move and pick it up:
        pos = item.pos
        x = pos.x
        y = pos.y
        hero.moveXY(x, y)

The solution is in my first response to your original post.

1 Like

Thanks!!!

I have done this, and yet My person continuously goes into the poison

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

nevermind

<div data-theme="foo">

[en_US.composer.my_button_text]

</div>

I’m not quite sure what to do when the character I’m controlling goes to pick up the coins/gems.
Here’s my code: (Javascript)
// Ogres are attacking a nearby settlement!
// Be careful, though, for the ogres have sown the ground with poison.
// Gather coins and defeat the ogres, but avoid the burls and poison!

while(true) {
var enemy = hero.findNearestEnemy();
if(enemy.type == “munchkin” || enemy.type == “thrower”) {
hero.attack(enemy);
}
var 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 == “gem” || item.type == “coin”){

    }
    // Then move and pick it up:

}