Drop the Flag Python Help!


#1

i have been stuck on this level for a long time. I can’t figure out what is wrong in my code.

# Put flags where you want to build traps.
# 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?
        # (Look below at how to get x and y from items.)
        
        
        self.buildXY("fire-trap", fx, fy)
        self.pickUpFlag(flag)
    else:
        item = self.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            self.moveXY(itemX, itemY)
            
    topenemy = self.findNearestEnemy()
    bottomenemy = self.findNearestEnemy()
    rightenemy = self.findNearestEnemy()
    
    
    if topenemy:
        self.buildXY("fire-trap", 30, 45)
        
    if bottomenemy:
        self.buildXY("fire-trap", 30, 16)
    
    if rightenemy:
        self.buildXY("fire-trap", 30, 31)

can someon please tell me what i am doing wrong. Thx :smile:


#2

You need to set fx and fy

You already have the flag. Can you think of a way get the position of the flag?


#Warning! Spoilers ahead!

Sadly it is currently not possible to combine spoilers and code-blocks, though here the unspoilerd code.

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

#4

i still didn’t get it. here’s the new code:

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

The ogres won’t die.


#5

i am supposed to defeat the ogres with traps.


#6

Correct.
You happily collect all the coins. From time to time, an ogre will come along. You use your flag to place a fire-trap, and then continue to collect the coins. After some time, all ogres are defeated.


#7

Thanks for your help. I appreciate it. :smile:


#8

i need help with same level


#9

Please post your code and state the difficulty you’re having. Also, state any errors you’re receiving. We can’t help you if we don’t know what the problem is.


#10

actually mind the trap


#12

Please place assistance requests in the correct thread. You are requesting assistance with the level, Mind The Trap, in the thread for, Drop The Flag. If we all did this, this board would be a complete mess. Please either revive a previous thread for Mind The Trap or create a new one. Thank you.