Drop-the-Flag Help please

I am on the Drop-the-Flag level and I need help with the movement to the flag to place the fire trap, I’ve tried flagPos.x and flagPos.y but it didnt work if you have an answer I would be obliged to use it.

Here is my code (python)

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", flagPos.x, flagPos.y)
    self.pickUpFlag(flag)
else:
    item = self.findNearest(self.findItems())
    if item:
        itemPos = item.pos
        itemX = itemPos.x
        itemY = itemPos.y
        self.moveXY(itemX, itemY)

You didn’t define flagPos as the example wanted you to do. You check if there is a flag, that’s good :

if flag:

but you don’t replicate correctly the example below.

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

My only concern with what this level is trying to teach is that it instructs the programmer to write 3 extra lines of code to define variables when, clearly, there is no need to. The item that gives pos functionality clearly stores position (x, y) data.

We assign item.pos to itemPos:

var itemPos = item.pos;

we then create two extra variables, one each for x and y:

var itemX = itemPos.x;
var itemY = itemPos.y;

This is quite redundant, as we are effectively saying:

var itemX = item.pos.x;
var itemY = item.pos.y;

Knowing that,

this.moveXY(item.pos.x, item.pos.y);

is all we need, and we don’t waste time with creating redundant variables, right?

That said, to establish the coordinates for the flag in order to build the trap we can do the same:

this.buildXY("fire-trap", flag.pos.x, flag.pos.y);

Well… Your answer is correct. But you’re talking to people learning what coding is, not perfecting it. And if Larkin can’t find out why a variable that isn’t declared is a problem, maybe adressing the redundancy is several steps too early.

Reading a fully understandable concept and repeating it is probably the most ancient way to learn, from kindergarden to college. So, my opinion is : this level is fine.

Also, your code is in JavaScript, the question was asked in Python.

So do the same as the item position just with the flags?

The example shows you :

item = self.findNearest(self.findItems()) # defining the variable item
if item: # if this variable isn't empty
    itemPos = item.pos

It means we define the position of the item in a new object by extracting the .pos property of item. Then :

    itemX = itemPos.x  
    itemY = itemPos.y

we define two coordinates X and Y of the item, extracted from the position object by the property .x and .y

    self.moveXY(itemX, itemY)

finally we move to the position we just extracted from the item.

So yea, in your code, the position object named : flagPos isn’t defined. You have to define it with

flagPos = flag.pos

because the flag objects, as any thang in the game (coins, friends, enemies, missiles etc), have a .pos property that you can extract. Only then you’ll be able to use the line :

self.buildXY("fire-trap", flagPos.x, flagPos.y)
1 Like

I added the position but he still goes for coins.

This is my code so far :

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.)

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

GOT it! :smile: THANKS VincentMaths!!!

1 Like

wanted: ability to place flag

The help video shows user placing flags with cursor. I am unable to get same functionality.

chrome
Version 49.0.2623.75 (64-bit)

re: formatting
I did place the code wrap but the formatting didn’t change. see screengrab

here is the code:

code:

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.)
        flagPos = flag.pos
        flagX = flag.x
        flagY = flag.y
        hero.moveXY(flagX, flagY)
        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)

@pleabargain For code blocks indented with 4 spaces, you need a blank line (2 line breaks) above the code. I’ve fixed that for you.

re: two blank lines
Ah! Ok. I didn’t know that! Thanks!

re:flag on click not working
Can you offer suggestions as to setting flag on click?

thank you
Dennis

Oh my bad, didn’t notice you were having issues with placing flags.

Do you have a flag equipped? If so, there should be three flag icons in the bottom-left of the game screen when you Submit* the code. You can click in the flag type you want to place (or press the corresponding hotkey: G -> Green, V -> Violet, B -> Black) and then click in the map to place it.

* The flags only appear when you click “Submit”, not “Run”.

Ok.
first:
Flags do not show up in ‘run’ only on submit! :frowning:
https://drive.google.com/open?id=0B19mmCKTPBRYcGFDUDUtVmtfYjQ
as well, flags don’t show up in run on, only on submit.


suggestion: flags show up on run so users can ‘see’ code run in ‘real time’ while placing flags

second:
instructions do NOT mention need to PREequip with flags. :expressionless:

third and unrelated:
Out of curiosity, has the codecombat folks made instructional videos on setting up on git? If yes, where are they posted?

I found this one: https://www.youtube.com/watch?v=usN85KSiWUM but it’s not from you guys.

Only a handful of how to vids on YT and Vimeo on codecombat. I’m a little surprised, if you are trying to market to school age … video is where it’s at.

Not fair to compare but roblox has 100s

with thanks and respect,
Dennis

I’m not exactly sure why it is that way, but there are likely technical constraints and challenges to that. Like how you can pause and rewind the game simulation in “Run” mode while “Submit” always plays in real time. I believe rewinding the game and changing flag locations would break the simulation and force it to start over. There are likely other issues, maybe @nick can fill you in.

As far as I can see, flags are a required item to start the level. You supposedly can’t start the level without equipping flags, as far as I can see.

What part of “setting up” are you interested in? Installing Git proper, first steps using Git, contributing back to upstream repositories? If you are completely new to Git, I suggest checking the 15 minutes tutorial Try Git.

# Размещай флаги в те места, где хочешь строить ловушки.
# Когда ты не строишь ловушки, собирай монеты!

while True:
    flag = hero.findFlag()
    if flag:
        # Как получить координаты флага flagX и flagY?
        # (Внизу показан способ получения координат предмета)
        flagPos = flag.pos
        flagX = flagPos.x
        flagY = flagPos.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)

В чем проблема,как мне кажется код верный, а он не работает???

Ваш код работает для меня. Что происходит, когда вы его отправляете?

Он просто собирает монеты и не реагирует на врагов,не выбрасывает флажков