Fix Crash on Pathfinding error

This commit is contained in:
Michał Gdula 2023-01-28 19:49:30 +00:00
parent 41a0ec665b
commit 760b29a761
4 changed files with 15 additions and 13 deletions

View file

@ -1,5 +1,4 @@
import pygame as pg
import sys
from settings import *
from map import *
from player import *

View file

@ -28,7 +28,7 @@ class ObjectHandler:
# NPC Map
#add_npc(NPC(game))
add_npc(NPC(game, path=self.npc_sprite_path + 'baller/0.png', pos=(14.5, 5.5), scale=0.6, shift=0.38))
add_npc(NPC(game, path=self.npc_sprite_path + 'baller/0.png', pos=(5, 18), scale=0.6, shift=0.38))
def update(self):

View file

@ -23,17 +23,20 @@ class PathFinding:
queue = deque([start])
visited = {start: None}
while queue:
cur_node = queue.popleft()
if cur_node == goal:
break
next_nodes = graph[cur_node]
try:
while queue:
cur_node = queue.popleft()
if cur_node == goal:
break
next_nodes = graph[cur_node]
for next_node in next_nodes:
if next_node not in visited and next_node not in self.game.object_handler.npc_positions:
queue.append(next_node)
visited[next_node] = cur_node
return visited
for next_node in next_nodes:
if next_node not in visited and next_node not in self.game.object_handler.npc_positions:
queue.append(next_node)
visited[next_node] = cur_node
return visited
except KeyError:
return visited
def get_next_nodes(self, x, y):
return [(x + dx, y + dy) for dx, dy in self.ways if (x + dx, y + dy) not in self.game.map.world_map]

View file

@ -7,7 +7,7 @@ HALF_WIDTH = WIDTH // 2
HALF_HEIGHT = HEIGHT // 2
FPS = 0 # 0 = unlimited
PLAYER_POS = 1.5, 5 # player position on the map
PLAYER_POS = 5, 18 # player position on the map
PLAYER_ROT = 0
PLAYER_SPEED = 0.002 # player speed
PLAYER_ROT_SPEED = 0.003 # player rotation speed