How do you pick up an item (python)

I cant figure out which code it is to pick up an item. Can someone please help me? Thanks.

You just walk over it; you don’t need to explicitly pick it up.

So than in Rich Forager the default code says:

    elif item:
    
    # What happens when I find an item?

what should i do than?

move to the POS coordinates of the item, and it’ll automatically pick it up :slight_smile:

it still wont work. Here is my code:

# Use "if" and "else if" to handle any situation.
# Put it all together to defeat enemies and pick up coins!
# Make sure you bought great armor from the item shop! 400 health recommended.

c = item
b = self.distanceTo(item)
a = self.cleave(enemy)
self.isReady("cleave")
loop:
    flag = self.findFlag()
    enemy = self.findNearestEnemy()
    item = self.findNearestItem()

    if flag:
        self.pickUpFlag(flag)
        # What happens when I find a flag?
        
    elif enemy:
        self.cleave(enemy)    
    if not a : 
        self.attack(enemy)
        
        # What happens when I find an enemy?
        
    elif item:
        if c: 
            self.distanceTo(c)
            self.pos(b)
        # What happens when I find an item?

You got something wrong there.

TL;DR: Corrected code at the bottom. I recommend to read the whole text though.

Computers are stupid. When the program starts, they know nothing (except self and all methods of self).
Now try to play computer, and read your program from the top:

First task:
    Assign the value of ITEM to C. Let's see... I don't know any ITEM, so ITEM is undefined and C is now undefined as well.

Second task:
    Assign the distance to ITEM to B. Let's see... ITEM is undefined, so the distance to undefined is... Wait, that doesn't make sense. Better throw an error.

As you can see, the assignments don’t make sense to the computer once computed. But let’s pretend the second task ran without throwing an error.

Third task:
    Assign the result of self.cleave(enemy) to A. ENEMY is... undefined. I can't cleave something undefined, error again!
    Also, cleave doesn't return anything, so A is undefined as well.

And, for the fourth task, the computer reads the following:

Fourth task:
    Check if "cleave" is ready. Once that is done, do nothing and continue with the fifth task.

That is code that effectivly does nothing. self.isReady("cleave") only checks a variable, but doesn’t change it. As you do nothing with the result of this method-call, nothing changes.


Now inside the loop. The computer now knows flag, enemy and item. They can be undefined, but we don’t know this yet. Let’s see what the computer reads:

If there is a FLAG, pick it up.
Else, if there is an ENEMY, cleave the enemy.

Regardless of whether there was an enemy or not, check if there is not an A (rephrased: If A is undefined).
If A is undefined, attack ENEMY.
Else (A is not undefined), if there is an item, check whether C has a value that is not "undefined"
If c has a value, compute the distance to C and do nothing with it and call the method self.pos() with the argument B

The computer will take an interesting path through this. Let’s say we found no flag, an enemy and no item:

flag = undefined
enemy = "Thog"     # Represents an enemy. Enemies are of course not strings
item = undefined

If there is a flag... nope, no flag, so if there is an enemy (Jup, Thog), cleave him.
    -> it is possible, that cleave is not ready. You should check that.
If a is not defined...
    -> Let's say, for some resaon the code above ran through. a would now be undefined
...then attack enemy,
    -> But enemy could either be already dead, or never existent at all.

As you can see, a can have no other value than undefined. Therefor, elif item: ect. are never called.

But even if we could enter this, if c: would always fail, because cis undefined as well.
And even if we could enter this, we would first ask the computer to get us the distance to nothing (remeber, c is undefined), and then throw the result away, and then call the method self.pos() with the argument b. self.pos() dosn’t expect an argument, so doesn’t do anything with it. And the result of self.pos()? That is thrown away again.


Here now, as promised, the corrected code:

loop:
    flag = self.findFlag()
    enemy = self.findNearestEnemy()
    item = self.findNearestItem()

    if flag:
        self.pickUpFlag(flag)
    elif enemy:                    # First check if the enemy exists ...
        if self.isReady("cleave"): # ... and if your cleave is ready ...
            self.cleave(enemy)     # ... then cleave ...
        else:
            self.attack(enemy)     # ... or attack it.
    elif item:
        itemPos = item.pos         # First, get the postion of the item, ...
        itemX = itemPos.x          # ... then extract the x- ...
        itemY = itemPos.y          # ... and y-value ...
        self.moveXY(itemX, itemY)  # ... and move there.
2 Likes

Ohh. I see my mistake. What is elif though?

Shortcut for else if

That… wipes a tear from his eye That was beautiful. sniffs

1 Like