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

Treasured In Ice (python)

my hero does not move at all in this level, and for yDif it says that ‘pos’ is undefined. here is my code:

exitPosition = {"x": 150, "y": 120}
hero.wait(.1)
chest = hero.findItems()[0]
walls = [[0] * 4 for i in range(100)]
steps = []


def direction(n):
    x = 0
    y = 0
    if n == 0:
        y += 16
    elif n == 1:
        x += 16
    elif n == 2:
        y -= 16
    else:
        x -= 16
    return {"x": hero.pos.x + x, "y": hero.pos.y + y}


while True:
    jMod = 0
    doNext = True
    lastStep = (steps[-1] + 2) % 4
    yDif = hero.pos.y - chest.pos.y      # <---------- this line has an error, 'pos' not defined
    xDif = hero.pos.x - chest.pos.x
    if hero.distanceTo(chest) < 5:
        break
    if yDif > 1:
        if xDif > 1:
            jMod = 2
        else:
            jMod = 1
    else:
        if xDif > 1:
            jMod = 3
    # hero.say(jMod)
    for j in range(4):
        i = (j + jMod) % 4
        checkPoint = direction(i)
        if hero.isPathClear(hero.pos, checkPoint) and walls[len(steps)][i] == 0 and i != lastStep:
            hero.moveXY(checkPoint.x, checkPoint.y)
            steps.append(i)
            doNext = False
            break
    if doNext:
        checkPoint = direction(lastStep)
        walls[len(steps) - 1][steps[-1]] = 1
        steps.pop()
        hero.moveXY(checkPoint.x, checkPoint.y)

while hero.distanceTo(exitPosition) > 10:
    checkPoint = direction((steps[-1] + 2) % 4)
    steps.pop()
    hero.moveXY(checkPoint.x, checkPoint.y)

hero.moveXY(exitPosition.x, exitPosition.y)

As @EpicCoder9999 said, try to make your own solution if you want us to help you solve it, not copy it from github, is that clear?

Ok, thanks for your suggestions!

1 Like

here is the new code, my hero can run and stuff but it doesn’t actually go to the chest

inSearch = True

width = 19
height = 15

def coorConvert(i, dist):
    return i * 8 + 10

wall = "X"
unknown = "?"
empty = "."

maze = []
# left border
for i  in range(height):
    maze.append([wall])
for j in range(width - 1):
    maze[0].append(wall)
for i in range(height - 1):
    if i % 2:
        for j in range(width - 1):
            maze[i].append(unknown)
    else:
        for j in range(width - 1):
            if j % 2:
                maze[i].append(wall)
            else:
                maze[i].append(unknown)

for j in range(width - 1):
    maze[height-1].append(wall)
for i in range(height):
    maze.append([wall])

hero.row = 13
hero.col = 17
exitRow = 13
exitCol = 17

item = hero.findNearest(hero.findItems())
distance = 16
move = Vector(distance, 0)
direction = Vector.add(move, hero.pos)
while True:
    while not (hero.isPathClear(hero.pos, direction)):
        move = Vector.rotate(move, Math.PI / 2)
        direction = Vector.add(move, hero.pos)
    hero.moveXY(direction.x, direction.y)
    move = Vector.rotate(move, -Math.PI / 2)
    direction = Vector.add(move, hero.pos)