From 4681dc5a2e9bcf373a57e4cfdcc8a8265e5a4428 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 26 Sep 2025 17:41:32 -0400 Subject: [PATCH] Found some differences in how levels are called that required the levels directory be in both _internal and in source directory. This fix hopefully makes them work from a single location. --- src/game_selection.py | 47 +++++++++++++++++---------------------- src/survival_generator.py | 4 ++-- wicked_quest.py | 4 ++-- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/game_selection.py b/src/game_selection.py index 50bfe15..89661ae 100644 --- a/src/game_selection.py +++ b/src/game_selection.py @@ -7,6 +7,20 @@ from os.path import isdir, join from libstormgames import speak, instruction_menu +def get_levels_base_path(): + """Get base path for levels directory, handling PyInstaller paths. + + Returns: + str: Base path where levels directory is located + """ + if hasattr(sys, "_MEIPASS"): + # Running as PyInstaller executable + return sys._MEIPASS + else: + # Running as script + return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + def get_available_games(): """Get list of available game directories in levels folder. @@ -14,14 +28,7 @@ def get_available_games(): list: List of game directory names """ try: - # Handle PyInstaller path issues - if hasattr(sys, "_MEIPASS"): - # Running as PyInstaller executable - base_path = sys._MEIPASS - else: - # Running as script - base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - + base_path = get_levels_base_path() levels_path = os.path.join(base_path, "levels") return [d for d in os.listdir(levels_path) if isdir(join(levels_path, d)) and not d.endswith(".md")] except FileNotFoundError: @@ -85,36 +92,22 @@ def get_level_path(game_dir, level_num): if game_dir is None: raise ValueError("game_dir cannot be None") - # Handle PyInstaller path issues - if hasattr(sys, "_MEIPASS"): - # Running as PyInstaller executable - base_path = sys._MEIPASS - else: - # Running as script - base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - + base_path = get_levels_base_path() level_path = os.path.join(base_path, "levels", game_dir, f"{level_num}.json") return level_path def get_game_dir_path(game_dir): """Get full path to game directory for end.ogg and other game files. - + Args: game_dir (str): Game directory name - + Returns: str: Full path to game directory """ if game_dir is None: raise ValueError("game_dir cannot be None") - - # Handle PyInstaller path issues - same logic as get_level_path - if hasattr(sys, "_MEIPASS"): - # Running as PyInstaller executable - base_path = sys._MEIPASS - else: - # Running as script - base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - + + base_path = get_levels_base_path() return os.path.join(base_path, "levels", game_dir) diff --git a/src/survival_generator.py b/src/survival_generator.py index 5c29141..06f30a6 100644 --- a/src/survival_generator.py +++ b/src/survival_generator.py @@ -4,7 +4,7 @@ import json import os import random import copy -from src.game_selection import get_level_path +from src.game_selection import get_level_path, get_levels_base_path class SurvivalGenerator: @@ -31,7 +31,7 @@ class SurvivalGenerator: def loadLevelData(self): """Load all level JSON files from the game pack.""" levelFiles = [] - packPath = os.path.join("levels", self.gamePack) + packPath = os.path.join(get_levels_base_path(), "levels", self.gamePack) if not os.path.exists(packPath): raise FileNotFoundError(f"Game pack '{self.gamePack}' not found") diff --git a/wicked_quest.py b/wicked_quest.py index 3bc223c..78618c7 100755 --- a/wicked_quest.py +++ b/wicked_quest.py @@ -669,9 +669,9 @@ class WickedQuest: # Game complete - use gameStartTime for total totalTime = pygame.time.get_ticks() - self.gameStartTime if self.player.xPos >= self.currentLevel.rightBoundary: - # Check for end of game scene using relative path like other sounds + # Check for end of game scene using unified path resolution for ext in ['.wav', '.ogg', '.mp3']: - endFile = os.path.join("levels", self.currentGame, f'end{ext}') + endFile = os.path.join(get_game_dir_path(self.currentGame), f'end{ext}') if os.path.exists(endFile): self.get_sounds()['end_scene'] = pygame.mixer.Sound(endFile) cut_scene(self.get_sounds(), 'end_scene')