[Solved It!] Stranded in the Dunes - Error and more (Python)

EDIT: I ended up solving this myself…i’ll leave this up in case anyone runs into the same issue.

Explanation in comments.

I am working on “Stranded in the Dunes” right now. Before I give up and watch the video tut, I thought I would post here and try to get some insight.

I am setting up my code for a mix of flag movement and AI. It works fine leaving the starting area, but as soon as I move to the next area, my hero just starts walking indiscriminately to the right, until he hits the edge (ignoring any flags I put down) AND THEN he starts attacking the horde of skeletons following him until he inevitably dies.

In addition I keep getting the error, “Unhandled error: TypeError: Cannot read property ‘length’ of undefined” but nothing in the code suggests a problem (not even flashing red). I have a feeling it has something to do with my ifs vs elifs (still working on grasping that concept fully), but I really don’t know.

Here is my full code so far:

while True:
    enemy = hero.findNearestEnemy()
    move = hero.findFlag("green")
    bash = hero.findFlag("black")
    if move:
        hero.moveXY(move.pos.x, move.pos.y)
        hero.pickUpFlag(move)
    elif bash:
        hero.moveXY(bash.pos.x, bash.pos.y)
        if hero.isReady("bash"):
            hero.bash(enemy)
        hero.pickUpFlag(bash)
    elif enemy:
        if enemy.type == "sand-yak" and enemy.pos.y > hero.pos.y:
            hero.moveXY(hero.pos.x, hero.pos.y - 1)
        elif enemy.type == "sand-yak" and enemy.pos.y < hero.pos.y:
            hero.moveXY(hero.pos.x, hero.pos.y + 1)
        elif enemy.type != "sand-yak":
            hero.attack(enemy)

As you can see the only AI movement I put in was to automatically avoid the stampedes of yaks. Other than that it should be solely based on green flags.

Thanks in advance and sorry for my noobiness :wink:

1 Like

Glad you solved it! Nice write up on the question, well explained and using the appropriate format.
-Cheers

Do anyone know how can i win this level. I can’t :(.

Please do not revive dead topics. If you need help, you can post your code in a new topic.

1 Like

This can be closed now…
@Chaboi_3000