Need help in Drop The Flag

I am stuck. This is my code not sure what to do. Any suggestions? Thanks

# When you're not building traps, pick up coins!

loop:
    flag = self.findFlag()
    if flag:
        # How do we get fx and fy from the flag's pos?
        self.pickUpFlag(flag)
        fx, fy = 30, 47
        fx, fy = 30, 31

        self.buildXY("fire-trap", fx, fy)
        
    else:
        item = self.findNearestItem()
        if (item):
            pos = item.pos
            itemX = pos.x
            itemY = pos.y
            self.moveXY(itemX, itemY)

I’m guessing the issue is the question in the comment? If so the flag has a pos object to, i.e. you can do flag.pos.x

Does that help?

flag.pos.x = the x-value of the position of the flag.
flag.pos.y = the y-value of the position of the flag.

I tried doing flag.pos.x but it gave me error saying “fx is not define”. So I tried to define fx and fy but it is not working out.

self.pickUpFlag(flag)
fx = flag.pos.x
fy = flag.pos.y
fx, fy = 30, 46
fx, fy = 30, 32

    self.buildXY("fire-trap", fx, fy)

Now you are defining fx to be the position, then you are assigning a new value of 30. Then you assign 30 to it again. Your code will always build the trap at (30, 32).

Delete the two fx, fy = ?, ? lines of code and it should work fine.

1 Like

Thanks very much. Work just as you say.

I tried this and it still dosen’t work. Do you know how to fix it? (code below)

loop:
    flag = self.findFlag()
    if flag:
        itemII = self.findNearestItem()
        # How do we get fx and fy from the flag's pos?
        # (Look below at how to get x and y from items.)
        
        if itemII.type = "flag":
            flagPos = self.findNearestItem()
            fx = flagPos.x 
            fy = flagPos.y
            self.buildXY("fire-trap", x, y)
            self.pickUpFlag(flag)
    else:
        item = self.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            self.moveXY(itemX, itemY)

if itemII.type = "flag":
You should use == instead.

= is an assignment.
== is a comparison.


Also your code is wrong, though you were probably mislead by the comment about looking below.

flag already contains your flag. You can handle it like an item:

loop:
    flag = self.findFlag()
    if flag:
        flagPos = flag.pos
        fx = flagPos.x 
        fy = flagPos.y
        self.buildXY("fire-trap", x, y)
        self.pickUpFlag(flag)
    else:
        item = self.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            self.moveXY(itemX, itemY)

Thanks!!!, I hope it works now.