A little help on Drop The Flag

I have spent about an hour on the level Drop The Flag. I find it a bit difficult to solve. I am going to paste my code and point out what i am finding hard to code.

loop:
    greenFlag = self.findFlag("green")
    blackFlag = self.findFlag("black")
    violetFlag = self.findFlag("violet")
    
    if  greenFlag:
    self.findFlag("green")
    fx = 30
    fy = 31
    self.buildXY("fire-trap", fx, fy)
    self.moveXY(19, 34)

    if blackFlag:
    self.findFlag("black")
    fx = 29
    fy = 46
    self.buildXY("fire-trap", fx, fy)
    self.moveXY(19, 34)

    if violetFlag:
    self.findFlag("violet")
    fx = 30
    fy = 16
    self.buildXY("fire-trap", fx, fy)
    self.moveXY(19, 34)

else:
    item = self.findNearestItem()
    if item:
        pos = item.pos
        itemX = pos.x
        itemY = pos.y
        self.moveXY(itemX, itemY)

So that is my code. I think i put wayyy too much because when i look at other articles i just see about 10 lines of code? My basic idea was to introduce each of the flags first and then state the action that will happen when i place each flag. I put the Black flag for the top part of the map, the Green flag for the middle part, and the Violet flag for the bottom part. I introduced what fx and fy means to say where i want the fire-trap to be placed and then i put to be moved to the coordinates (19, 34). I put the coordinates because i was trying to put to self.pickUpFlag("____") option in order to pick up the flag. It didn’t work though OR i was just confused at what it was telling me. The point is that when i put that code it told me “Pass a flag object to pick up.” I did not understand what that meant so i just ignored it and deleted that self.pickUpFlag("__") code.

I really need help on this level am i doing it right or i am doing too much code?

You should be able to place the trap based on the position of the flag. Use code similar to the code for moving to the position of the item to calculate your fx and fy only use flag.pos instead of item.pos

Then you don’t have to worry about what color of flag you drop, just have it work regardless of the color of the flag.

That’s actually a really clever solution, to use the color of the flag instead of the position. The other way uses less code, but this should work.

Assuming your indentation is actually correct–the if-statements need their code indented, but I think it would throw an error the way you have it–the one thing you’re missing is to pick up the flags after you build the trap. Otherwise the flags stay there.

You also have an else: statement to pick up the coins, but it’s not clear what the else: is referring to. It goes with the last if at its level of indentation, but you don’t have one there, just a loop? Let me know if the indentation is different than what you have here.

You could do something like this:

loop:
    #... your code to find the flag colors
    if greenFlag:
        #... build the trap like you do now
        self.pickUpFlag(greenFlag)
    elif blackFlag:
        #... build the trap like you do now
        self.pickUpFlag(blackFlag)
    elif violetFlag:
        #... build the trap like you do now
        self.pickUpFlag(violetFlag)
    else:
        #... pick up the item like you do now

thanks for the help, i really appreciate it, now i even understand it more. i had figured it out yesterday though, i pm’ed someone and they managed to help me out, thanks for the help anyways :smile: