Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Treasured in Ice Help Please

I’m trying to do this level but my character just gets into a loop. Can someone help?

#lpx and lpy == lastPositionX and lastPositionY
x = hero.pos.x
y = hero.pos.y
while True:
    downClear = hero.isPathClear(hero.pos,{'x':hero.pos.x,'y':hero.pos.y - 16})
    if downClear and y - 16 != lpy:
        lpx = x
        lpy = y
        y-=16
        hero.moveXY(x,y)
    leftClear = hero.isPathClear(hero.pos,{'x':hero.pos.x + 16, 'y':hero.pos.y})
    if leftClear and x - 16 != lpx:
        lpx = x
        lpy = y
        x+=16
        hero.moveXY(x,y)
    rightClear = hero.isPathClear(hero.pos,{'x':hero.pos.x- 16, 'y':hero.pos.y})
    if rightClear and x + 16 != lpx:
        lpx = x
        lpy = y
        x-=16
        hero.moveXY(x,y)
    upClear= hero.isPathClear(hero.pos,{'x':hero.pos.x,'y':hero.pos.y + 16})    
    if upClear and y + 16 != lpy:
        lpx = x
        lpy = y
        y+=16
        hero.moveXY(x, y)

Hey there!
Before anything else, you’ve got a mistake on left and right checks I believe!
on the isPathClear function you pass hero’s position +16 and in the if-block after that you check if you’ve been to hero’s position -16 and the other way around for right.

:frowning: now my character is going in circles

What you could do now would be to define an array of “positions you’ve been to before” and a function that would check the position passed by iterating through the array and checking if one of these match and if not would return true. That way you’re sure to never go to a position you’ve been to before. Just an idea :smile:
Because right now, you just check the last position you’ve been to.