Sounds can now load from subdirectories.

This commit is contained in:
Storm Dragon
2025-03-16 17:02:18 -04:00
parent 619cb5508a
commit 1bb9e18ea2
2 changed files with 65 additions and 51 deletions

View File

@ -53,22 +53,40 @@ def initialize_gui(gameTitle):
# Enable key repeat for volume controls
pygame.key.set_repeat(500, 100)
# Load sound files
try:
from os import listdir
from os.path import isfile, join
soundFiles = [f for f in listdir("sounds/")
if isfile(join("sounds/", f))
and (f.split('.')[1].lower() in ["ogg", "wav"])]
except Exception as e:
print("No sounds found.")
Speech.get_instance().speak("No sounds found.", False)
soundFiles = []
# Create dictionary of sound objects
# Load sound files recursively including subdirectories
soundData = {}
for f in soundFiles:
soundData[f.split('.')[0]] = pygame.mixer.Sound("sounds/" + f)
try:
import os
soundDir = "sounds/"
# Walk through directory tree
for dirPath, dirNames, fileNames in os.walk(soundDir):
# Get relative path from soundDir
relPath = os.path.relpath(dirPath, soundDir)
# Process each file
for fileName in fileNames:
# Check if file is a valid sound file
if fileName.lower().endswith(('.ogg', '.wav')):
# Full path to the sound file
fullPath = os.path.join(dirPath, fileName)
# Create sound key (remove extension)
baseName = os.path.splitext(fileName)[0]
# If in root sounds dir, just use basename
if relPath == '.':
soundKey = baseName
else:
# Otherwise use relative path + basename, normalized with forward slashes
soundKey = os.path.join(relPath, baseName).replace('\\', '/')
# Load the sound
soundData[soundKey] = pygame.mixer.Sound(fullPath)
except Exception as e:
print("Error loading sounds:", e)
Speech.get_instance().speak("Error loading sounds.", False)
soundData = {}
# Play intro sound if available
from .sound import cut_scene