From 760b29a761992d0b4545aa42616918352770230e Mon Sep 17 00:00:00 2001 From: Fluffy-Bean Date: Sat, 28 Jan 2023 19:49:30 +0000 Subject: [PATCH] Fix Crash on Pathfinding error --- game.py => main.py | 1 - object_handler.py | 2 +- pathfinding.py | 23 +++++++++++++---------- settings.py | 2 +- 4 files changed, 15 insertions(+), 13 deletions(-) rename game.py => main.py (99%) diff --git a/game.py b/main.py similarity index 99% rename from game.py rename to main.py index 5628b0a..d016f06 100644 --- a/game.py +++ b/main.py @@ -1,5 +1,4 @@ import pygame as pg -import sys from settings import * from map import * from player import * diff --git a/object_handler.py b/object_handler.py index 8ecaa1e..e53d185 100644 --- a/object_handler.py +++ b/object_handler.py @@ -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): diff --git a/pathfinding.py b/pathfinding.py index 2df004c..1204ae8 100644 --- a/pathfinding.py +++ b/pathfinding.py @@ -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] diff --git a/settings.py b/settings.py index c30ef8b..36de065 100644 --- a/settings.py +++ b/settings.py @@ -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