Help completeing Crux of the dessert


#1

Working on tyring to complete Crux of the dessert. I have tried it with and without the hero.movexy code after building a fire trap and get the following results:
Without the code the hero tries to move to the next X location to build a trap (not checking for enemy
With the code the hero returns to the center of the crux but then goes down the next line and builds a fire trap.

Thoughts?

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # An enemy is to the left if the hero's x is greater than the enemy's.
        isLeft = hero.pos.x  > enemy.pos.x
        # An enemy is above if the enemy's y is greater than the hero's.
        isAbove = hero.pos.y < enemy.pos.y
        # Check if the enemy is to the right of the hero:
        isRight = hero.pos.x > enemy.pos.x
        # Check if the enemy is below the hero.
        isBelow = hero.pos.y < enemy.pos.y
        # If an enemy is above and to the left, build a fire-trap accordingly.
        if isLeft and isAbove:
           hero.buildXY("fire-trap", 40 - 20, 34 + 17)
        
        # Check if an enemy is above and to the right:
        if isRight and isAbove:
           hero.buildXY("fire-trap", 40 + 20, 34 + 17)
          
        # Check if an enemy is below and to the left:
        if isLeft and isBelow:
           hero.buildXY("fire-trap", 40 - 20, 34 - 17)
           
        # Check if an enemy is below and to the right:
        if isRight and isBelow:
           hero.buildXY("fire-trap", 40 + 20, 34 - 17)
        
        
    else:
        hero.moveXY(40, 34)

#2

Well I would try to help you more but I can not for the life of me find the level “Crux of the Dessert”. However I think I found one error at least. You have the wrong opperator direction. The isRight and isLeft actualy are the same statement i.e. both will be true or false. The same goes with the Above and Below sections. Your symbols are mixed up a little.
Should look like this:

isLeft = hero.pos.x  < enemy.pos.x
isAbove = hero.pos.y < enemy.pos.y
isRight = hero.pos.x > enemy.pos.x
isBelow = hero.pos.y > enemy.pos.y

-Cheers


#3

Opps crux of the desert.

When I try your suggestion my Hero moves to set traps at each location but doesn’t seem to be checking for an enemy. Just placing traps. The hero ends up getting stuck at 27,51 and being killed.

Got it. now. You were right I had the symbols mixed up and was missing a move at the end.

thanks.


#4

Yep, no problem happy codeing!
-Cheers

Edit: No worries about spelling. I actualy did not catch the mistake, still cant find the level. Rather silly, but true :wink:


#5

This is my code, and I don’t know what’s wrong with it. My character just builds one firetrap, then sits in the middle.

# Figure out which direction the ogres are coming from.

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Left: enemy.pos.x is less than hero.pos.x
        isLeft = enemy.pos.x  < hero.pos.x
        # Above: enemy.pos.y is greater than hero.pos
        isAbove = enemy.pos.y > hero.pos.y
        # Right: enemy.pos.x is greater than hero.pos.
        isRight = enemy.pos.x > hero.pos.x
        
        # Below: enemy.pos.y is less than hero.pos.y
        isBelow = enemy.pos.y < hero.pos.y
        # If enemy isAbove and isLeft:
        # buildXY() a "fire-trap" at the X mark.
        if isLeft and isAbove:
            hero.buildXY("fire-trap", 40 - 20, 34 + 17)
        # If enemy isAbove and isRight:
        # buildXY() a "fire-trap" at the X mark.
        if isLeft and isAbove:
            hero.buildXY("fire-trap", 60, 51)
        # If enemy isBelow and isLeft:
        # buildXY() a "fire-trap" at the X mark.
        if isBelow and isLeft:
            hero.buildXY("fire-trap", 20, 17)
        # If enemy isBelow and isRight:
        # buildXY() a "fire-trap" at the X mark.
        if isBelow and isRight:
            hero.buildXY("fire-trap", 60, 17)
        hero.moveXY(40, 34)
    else:
        hero.moveXY(40, 34)

#6

You surrounded your code with 3x (~), surround it instead with 3x ( ` ). It’s usually the same key, but don’t hold down shift.


#7

ok, thx

# Figure out which direction the ogres are coming from.

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Left: enemy.pos.x is less than hero.pos.x
        isLeft = enemy.pos.x  < hero.pos.x
        # Above: enemy.pos.y is greater than hero.pos
        isAbove = enemy.pos.y > hero.pos.y
        # Right: enemy.pos.x is greater than hero.pos.
        isRight = enemy.pos.x > hero.pos.x
        
        # Below: enemy.pos.y is less than hero.pos.y
        isBelow = enemy.pos.y < hero.pos.y
        # If enemy isAbove and isLeft:
        # buildXY() a "fire-trap" at the X mark.
        if isLeft and isAbove:
            hero.buildXY("fire-trap", 40 - 20, 34 + 17)
        # If enemy isAbove and isRight:
        # buildXY() a "fire-trap" at the X mark.
        if isLeft and isAbove:
            hero.buildXY("fire-trap", 60, 51)
        # If enemy isBelow and isLeft:
        # buildXY() a "fire-trap" at the X mark.
        if isBelow and isLeft:
            hero.buildXY("fire-trap", 20, 17)
        # If enemy isBelow and isRight:
        # buildXY() a "fire-trap" at the X mark.
        if isBelow and isRight:
            hero.buildXY("fire-trap", 60, 17)
        hero.moveXY(40, 34)
    else:
        hero.moveXY(40, 34)

#8

This, does not work, but I thought it should be fine.


#9

Check the conditionals inside your if-statements. Specifically the first and second if-statements inside if enemy.


#10

Recheck this portion of your code closely. Then compare to the if statement above it.

# If enemy isAbove and isRight:
# buildXY() a "fire-trap" at the X mark.
if isLeft and isAbove:
   hero.buildXY("fire-trap", 60, 51)


#11

Thank you for the feedback, I succeeded, even if my character blows himself up. :joy: