Thunderhooves question,help

i write the code ,when i run it ,the system said the" epx=enemy.pos.x was wrong ,try self.pos"

loop:

    enemy = self.findNearest(self.findEnemies())

    epx=enemy.pos.x

    epy=enemy.pos.y

    opos=self.pos

    ox=opos.x

    oy=opos.y

    if enemy:

        if epx>ox:

            self.buildXY("fence", epx-10, oy)

        elif spy>oy:

            self.buildXY("fence", ox, epy-10)

        elif spx<ox:

            self.buildXY("fence", epx+10, oy)

    else:

        self.moveXY(ox, 30)

Are you sure there is an enemy around at the time where this checks happens? You should check if enemy: first, not only after the whole position-getting.

EDIT: And you should use more descriptive variablenames, the conditions where you check if the yak is above or below you, you use spy and spx instead of epy and epx.

1 Like

ok,now i have solved the problem. this is the new code .

 loop:
    enemy = self.findNearest(self.findEnemies())
    if enemy:
        yackpos=enemy.pos
        yackpx=yackpos.x
        yackpy=yackpos.y
        opos=self.pos
        ox=opos.x
        oy=opos.y
        if yackpy>oy:
            self.buildXY("fence", ox, yackpy-10)
        elif yackpy<oy:
            self.buildXY("fence", ox, yackpy+10)
        elif yackpx<ox:
            self.buildXY("fence", yackpx+10, oy)
    else:
        opos=self.pos
        ox=opos.x
        oy=opos.y
        self.moveXY(ox+10, oy)

You can display your code in a nice way with the help of three backticks (```) before and after the code. This is also written in the FAQ, which you should read anyway before posting.
I did it for you this time.


Looks good to me.


Here’s a tip from me. Please don’t take it as if you made something wrong. You just happened to trigger my advice-mode, where I give random hints to other people.

If your variablename is composed of multiple words, you can also write it in CamelCase. This is a form of labeling that improves readability even more, and helps you to grab the meaning of a variable in one glance.

yakposx isthexpositionoftheyak

yakPosX is something about the Yak. Ah, the position. Ah, the x.

The uppercase-letters simply help to read the name. The human eye usually does not read the whole text, but only the landmarks of a text. This includes Uppercase letters, unusal fonts (by mixing two fonts), indentations, other text colors (you noticed the hyperlink immediatly, right?) and some more.
Sadly this also applies to code-reading. Just yesterday I checked the code of someone, and didn’t noticed an error, just because I started to scan over it. That was not the fault of the code. But code can be written in a way that makes it more interesting to read, so readers are less likely to fall in scan-mode.

1 Like

//enter humor mode

Wait, what? that “yakposx” wasn’t “yakposix” the design standards of yaks?? Well, I’ll be.

(Psst, I tend to yakx instead of yakX myself (stupid shift key is soooo hard).) :smiley:

//make own choice about leaving humor mode…

thelongerthevarable theharderitis toparse. (“top-arse” or “to-parse”)

J_F_B_M is the hintmaster. It’s what we pay him(her?) to do :wink:

Also, I did not know that about CamelCase!! Merci!

If just they would. Also these are my initials, as you can see in my profile it’s Joachim F. Brehmer-M. (I’ll keep the F and M secret for now ;))

1 Like