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.

This commit is contained in:
Storm Dragon
2025-09-26 17:41:32 -04:00
parent 28856d2662
commit 4681dc5a2e
3 changed files with 24 additions and 31 deletions

View File

@@ -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)

View File

@@ -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")

View File

@@ -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')