[Solved] Copper Meadows - Python

The problem is, my character goes and collects the coins in the part of the forest he’s in, I place a flag, and then he gets stuck moving in the opposite direction of my flag. Here’s my code:

# Collect all the coins in each meadow.
# Use flags to move between meadows.
# Press Submit when you are ready to place flags.

while True:
    flag = hero.findFlag()
    if flag:
        pass  # `pass` is a placeholder, it has no effect.
        # Pick up the flag.
        hero.moveXY(x, y)
    else:
        # Automatically move to the nearest item you see.
        item = hero.findNearestItem()
        if item:
            position = item.pos
            x = position.x
            y = position.y
            hero.moveXY(x, y)

If he gets stuck in the middle of a meadow, he does his walking animation and jitters back and forth, but very extremely slowly.

You should have done pass after hero.moveXY and you shouldn’t have used hero.pickUpFlag(greenFlag)

Ok, thanks Milton!!!

I think the code is right

Don’t use hero.moveXY(x, y) at the flag part, use hero.pickUpFlag(). Also I think that flag = hero.findFlag() is supposed to be this flag = hero.findFlag("green") or whatever color flag you are using.

1 Like

Thanks ABC, gonna change my code now.

@AlwaysConfused first try figure and figure it out yourself then look at hints then the discourse. Got it

I think you gotta delete this.

You can change these to flag.pos.x and flag.pos.y

Lydia

@abc and @milton.jinich are right about using the hero.pickUpFlag() function as an easier way to manage this code. While you don’t need to clarify the flag color while finding it, it helps to ensure you are finding the correct one when you start using multiple flags. You can also look for the color afterward like the example.

hero.flag

To explain why your hero is moving in the wrong direction with your posted code, your variables x and y are used for the item position. Even though you have the flag, your are telling your hero to move to the item position and not the flag position. @Lydia_Song has a good approach to clearly identify the x and y position of your flag when using flag as your variable. Learning to keep your variables distinctly named and isolated is a very important aspect of coding.

The pass doesn’t impact the code that is added, but is needed until code is added or it will flag an error. Deleting it does keep your code a little cleaner, but doesn’t cause any problems if there.

2 Likes

Yeah, I like it to delete it, makes my code cleaner, but you don’t have to delete it.
Lydia

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.