[SOLVED] Drop The Flag Level Help in Backwoods Forest

I may have confused you with the “quotations”. I was just trying to point out the period in the variable name. Your setup for the item is correct. Mimic that approach. Just trade out item for flag.

itemPos = item.pos
itemX = itemPos.x
itemY = itemPos.y

like this

Like the error tells you, you haven’t defined itemX, your code which you did that in is commented out (lines17-19). If you remove them (the quotes) then your hero will collect the item.
The other problem is the defining of flag.pos (line 9): a better way to do that whole “if flag” section would be to pick up the flag first, then build a fire trap at your pos: (hero.pos.x, hero.pos.y).
:lion: :lion: :lion:

1 Like

The item section was correct before. You don’t want the quotes. You need to copy that same set up to use for the flag section.

This is correct.

itemPos = item.pos
itemX = itemPos.x
itemY = itemPos.y

Now copy this and replace the word item with flag and move it to that section replacing the same three lines.

I dont understand

No, we’re talking about the if flag section now. Put the if item section back to what it was:

if item:
    itemPos = item.pos
    itemX = itemPos.x
    itemY = itemPos.y
    hero.moveXY(itemX, itemY)

The rest of my advice was for the if flag section.
:lion: :lion: :lion:

what do i put in the flag section

You’ve used the function to pick up a flag on line 13, you can build a fire trap at an x-y coordinate, you can do it.
:lion: :lion: :lion:

1 Like

what would that look like in my code

# Put flags where you want to build traps.
# When you're not building traps, pick up coins!

while True:
    flag = hero.findFlag()
    if flag:
        # How do we get flagX and flagY from the flag's pos?
        # (Look below at how to get x and y from items.)
        flag.Pos = flag.Pos.X and flag.Pos.Y
        flagX = flag.Pos.X
        flagY = flag.Pos.Y
        hero.buildXY("fire-trap", flagX, flagY)
        hero.pickUpFlag(flag)
    else:
        item = hero.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            hero.moveXY(itemX, itemY)

Ah, Ok. I realise the level is telling you to do something different from what I’m telling you to do.
The if item section is correct now. Good.
The if flag section should be exactly the same as the if item section (with flag instead of item), except you’re building a fire-trap at the x-y coordinates, rather than moving there. And adding a hero.pickUpFlag(flag) afterwards (which you’ve done).
So let’s look at the two sections which should be identical (except for replacing item with flag):

itemPos = item.pos
itemX = itemPos.x
itemY = itemPos.y
# even if you changed "item" to "flag" there're still differences between the two codes, can you spot them?
flag.Pos = flag.Pos.X and flag.Pos.Y
flagX = flag.Pos.X
flagY = flag.Pos.Y
HINT

Make sure you put the .s in the right places.
And what are you doing with the and?

:lion: :lion: :lion:

One is capitalized and the other one isn’t (x and y)

That’s not it, look at the HINT.

could u just give me the answer because this assignment is due today

See Brooksy’s post :arrow_down: That should help you.


1 Like

Let’t try it this way.

while True:
    flag = hero.findFlag()
    if flag:
        # How do we get flagX and flagY from the flag's pos?
        # (Look below at how to get x and y from items.)
        itemPos = item.pos  # replace the word item with flag
        itemX = itemPos.x  # replace the word item with flag
        itemY = itemPos.y  # replace the word item with flag
        hero.buildXY("fire-trap", flagX, flagY)
        hero.pickUpFlag(flag)
    else:
        item = hero.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            hero.moveXY(itemX, itemY)
1 Like

Here at CodeCombat our goal is to create a fun game that users enjoy that teaches them how to code. Giving the answer away doesn’t teach anything. Please continue to work through your problem. We in turn will continue to give you feedback and support but no direct answers will be given.

If this is an assignment I’m sure your teacher will want you to really know the code and how it works. Not just pass the level to get a good grade.

Continue to try hard and I know U’ll eventually get it!!

:fox_face::fox_face::fox_face:
-@Luke10

2 Likes

I did it thanks it was really hard

1 Like

You are very welcome!

That should show you that you can do it if you put your mind to it. Good Job to @brooksy125 for helping and also @Deadpool198. You guys rock!!

why “self” and not “hero”???!!

“Self” is old, original code from the early days of CodeCombat. Using, “hero” instead came later.

1 Like