Help with A* pathfinding

Help with A* pathfinding

Postby ChristianCareaga » Tue Jul 02, 2013 2:20 am

hey everyone im having some trouble using A* pathfinding in pygame

it all works until the player gets to far from the starting point of the monster

basically nodes are loaded using 10x10 pixel squares filling up the screen but since the camera is focused on the player once the player leaves the generated node the closed_list empties and an error is produced saying that it cant pop from an empty list

can anyone help with this

Code: Select all
def astar(m, startp, endp):
    width, height = 90, 65
    start_x, start_y = startp
    end_x, end_y = endp
    node = [None, start_x, start_y, 0, abs(end_x - start_x) + abs(end_y - start_y)]
    closed_list = [node]
    c_list = {}
    c_list[start_y * width + start_x] = node
    k = 0
    while c_list:
        node = closed_list.pop(0)
        x = node[1]
        y = node[2]
        l = node[3] + 1
        k += 1
        if k & 1:
            neighbours = ((x, y + 1), (x, y - 1), (x + 1, y), (x - 1, y))
        else:
            neighbours = ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1))
        for nx, ny in neighbours:
            if nx == end_x and ny == end_y:
                path = [(end_x, end_y)]
                while node:
                    path.append((node[1], node[2]))
                    node = node[0]
                return list(reversed(path))
            if 0 <= nx < width and 0 <= ny < height and m[ny][nx] == 0:
                if ny * width + nx not in c_list:
                    nn = (node, nx, ny, l, l + abs(nx - end_x) + abs(ny - end_y))
                    c_list[ny * width + nx] = nn
                    #adding to closelist ,using binary heap
                    nni = len(closed_list)
                    closed_list.append(nn)
                    while nni:
                        i = (nni - 1) >> 1
                        if closed_list[i][4] > nn[4]:
                            closed_list[i], closed_list[nni] = nn, closed_list[i]
                            nni = i
                        else:
                            break
    return 0


here is the full traceback:

Code: Select all
Traceback (most recent call last):
  File "C:\Documents and Settings\User\My Documents\Downloads\Necromonster-master\Necromonster-master\Necromonster.py", line 128, in <module>
    Necro()
  File "C:\Documents and Settings\User\My Documents\Downloads\Necromonster-master\Necromonster-master\Necromonster.py", line 57, in __init__
    self.Loop()
  File "C:\Documents and Settings\User\My Documents\Downloads\Necromonster-master\Necromonster-master\Necromonster.py", line 63, in Loop
    self.Tick()
  File "C:\Documents and Settings\User\My Documents\Downloads\Necromonster-master\Necromonster-master\Necromonster.py", line 88, in Tick
    self.EntityHandler.updateAll()
  File "class\EntityHandler.py", line 19, in updateAll
    if entity.update(index):
  File "class\monster.py", line 73, in update
    getattr(pathfind, self.path)(self, self.game)
  File "class\pathfind.py", line 90, in aggressive
    monster.path_found = astar(game.Grid.nodes, (monster.pos[0] / 10, monster.pos[1] / 10), monster.player_place)
  File "class\pathfind.py", line 126, in astar
    node = closed_list.pop(0)
IndexError: pop from empty list


full code here:

https://github.com/linkey11/Necromonster
ChristianCareaga
 
Posts: 51
Joined: Sat Jun 22, 2013 9:54 am

Return to Game Development

Who is online

Users browsing this forum: Majestic-12 [Bot] and 3 guests