Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Need help on Sarven Sentry


#1

Need help on Sarven Sentry. Something is wrong with my code, please help.

Merged Doublepost

My code is:

loop:
    

    flagGreen = self.findFlag("green")
    flagBlack = self.findFlag("black")
    flagViolet = self.findFlag("violet")

    self.findFlag("black")
    if flagBlack:
        pos = flagBlack.pos
        x = pos.x
        y = pos.y
        self.buildXY("fire-trap", x, y)
        self.pickUpFlag(flagBlack)
    self.findFlag("green")
    if flagGreen:
        pos = flagGreen.pos
        x = pos.x
        y = pos.y
        flagGreen = self.buildXY("fence", x, y)
        self.pickUpFlag(flagGreen)
    self.findFlag("violet")
    if flagViolet:
        pos = flagViolet.pos
        x = pos.x
        y = pos.y
        self.moveXY(x, y)
        self.pickUpFlag(flagViolet)

#3

This looks like a serious case of life-destroying indentation-problems. I used to say Indentations save lives, and you’ll need a lot of them (unless the copy-paste failed somehow, then I apologize in advance).
Please look that your complete code is indented correctly before continuing, as Python is pretty picky about it.


Before each if you call self.findFlag(color). As you do not assign the result of this call to anything, they do nothing. You can simply delete them.


You assign the result of self.buildXY('fence', x, y) to flagGreen. self.buildXY doesn’t return anything, so flagGreen is undefined afterwards. You then try to collect it. Simply don’t assign like in the case when you build a fire-trap.


Rest looks good.


#4

I tried that, but it doesn’t work.

Here is what I tried:

loop:

flagGreen = self.findFlag("green")
flagBlack = self.findFlag("black")
flagViolet = self.findFlag("violet")

self.findFlag("black")
if flagBlack:
    pos = flagBlack.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("green")
    self.buildXY("fire-trap", x, y)
    self.pickUpFlag(flag)
self.findFlag("green")
if flagGreen:
    pos = flagGreen.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("green")
    flagGreen = self.buildXY("fence", x, y)
    self.pickUpFlag(flag)
self.findFlag("violet")
if flagViolet:
    pos = flagViolet.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("green")
    self.moveXY(x, y)
    self.pickUpFlag(flag)

Error is: “Line 31:Pass a flag object to pick up”


#5

You are doing something equivalent to:

if flagViolet:
    flag = self.findFlag("green")
    self.pickUpFlag(flag)

Which means: if I found a violet flag, I am going to pick up the green flag (which probably does not exist).

Why you have some many self.findFlag() all over your program?
You only need the 3 findFlag() at the top. All other can be deleted.
Example:

if flagBlack:
    pos = flagBlack.pos
    x = pos.x
    y = pos.y
    # no need flag = self.findFlag("green")
    self.buildXY("fire-trap", x, y)
    self.pickUpFlag(flagBlack) #just use flagBlack
```

#6

Oh. Thank You!:grinning:


#7

loop:

flagGreen = self.findFlag("green")
flagBlack = self.findFlag("black")
flagViolet = self.findFlag("violet")

self.findFlag("black")
if flagBlack:
    pos = flagBlack.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("black")
    self.buildXY("fire-trap", x, y)
    self.pickUpFlag(flag)
self.findFlag("green")
if flagGreen:
    pos = flagGreen.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("green")
    flagGreen = self.buildXY("fence", x, y)
    self.pickUpFlag(flag)
self.findFlag("violet")
if flagViolet:
    pos = flagViolet.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("violet")
    self.moveXY(x, y)
    self.pickUpFlag(flag)

Like this?


#8

It will probably run without errors.

But you still have a lot of self.findFlag(), self.FindFlag(“violet”), etc. that are not really needed

Just keep the 3 self.findFlag at the top and replace every variable flag in your program with
flagBlack, flagGreen or flagViolet depending inside which if you are.


#9
# Use different colored flags to perform different tasks.

while True:
    flagGreen = hero.findFlag("green")
    :fireworks: = hero.find:fireworks:)
    flag = hero.findFlag("green")
    # If there's a flagGreen...
    if flagGreen:
        # Build a "fence" at flagGreen's position.
        hero.buildXY("fence")
        # Pick up the flag!
        hero.pickUpFlag(flag)
        flag = hero.find:fireworks:
    # If there's a :fireworks:...
    if :fireworks:
        # Build a "fire-trap" at :fireworks: position.
        hero.buildXY("fire-trap")
        # Pick up the flag!
        hero.pickUp:firecracker:
    # Move back to the center.
    hero.moveXY(43, 31)
    
I need help! (Don't mind the emoji names; they cover up things that aren't allowed).

#10

If you click on the methods in the middle of the screen, it shows how to properly use each one. Your use of the buildXY method is not complete. Also, you’ve defined a green flag twice.

buildXY


#11

I don’t see where I defined green flag twice.


#12

are you there? I need help!


#13

My hero doesn’t do what i want it to do. I put a flag in one place and put it’s location, but the locations of the enemies changes every time.


#14

That’s why we use variables. We wouldn’t need variables if everything in life was a fixed point or consistent. Variables compensate/adapt for a changing environment.