Sounds can now load from subdirectories.
This commit is contained in:
parent
619cb5508a
commit
1bb9e18ea2
48
display.py
48
display.py
@ -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 = {}
|
||||
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 = {}
|
||||
for f in soundFiles:
|
||||
soundData[f.split('.')[0]] = pygame.mixer.Sound("sounds/" + f)
|
||||
|
||||
# Play intro sound if available
|
||||
from .sound import cut_scene
|
||||
|
66
sound.py
66
sound.py
@ -8,6 +8,7 @@ Provides functionality for:
|
||||
- Volume controls
|
||||
"""
|
||||
|
||||
import os
|
||||
import pygame
|
||||
import random
|
||||
import re
|
||||
@ -44,46 +45,41 @@ class Sound:
|
||||
self.load_sounds()
|
||||
|
||||
def load_sounds(self):
|
||||
"""Load all sound files from the sound directory."""
|
||||
try:
|
||||
soundFiles = [f for f in listdir(self.soundDir)
|
||||
if isfile(join(self.soundDir, f))
|
||||
and (f.split('.')[1].lower() in ["ogg", "wav"])]
|
||||
"""Load all sound files from the sound directory and its subdirectories.
|
||||
|
||||
# Create dictionary of sound objects
|
||||
for f in soundFiles:
|
||||
self.sounds[f.split('.')[0]] = pygame.mixer.Sound(join(self.soundDir, f))
|
||||
Searches recursively through subdirectories and loads all sound files with
|
||||
.ogg or .wav extensions. Sound names are stored as relative paths from the
|
||||
sound directory, with directory separators replaced by forward slashes.
|
||||
"""
|
||||
try:
|
||||
# Walk through directory tree
|
||||
for dirPath, dirNames, fileNames in os.walk(self.soundDir):
|
||||
# Get relative path from soundDir
|
||||
relPath = os.path.relpath(dirPath, self.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
|
||||
self.sounds[soundKey] = pygame.mixer.Sound(fullPath)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error loading sounds: {e}")
|
||||
|
||||
def play_intro(self):
|
||||
"""Play the game intro sound if available."""
|
||||
if 'game-intro' in self.sounds:
|
||||
self.cut_scene('game-intro')
|
||||
|
||||
def get_sounds(self):
|
||||
"""Get the dictionary of loaded sound objects.
|
||||
|
||||
Returns:
|
||||
dict: Dictionary of loaded sound objects
|
||||
"""
|
||||
return self.sounds
|
||||
|
||||
def play_bgm(self, musicFile):
|
||||
"""Play background music with proper volume settings.
|
||||
|
||||
Args:
|
||||
musicFile (str): Path to the music file to play
|
||||
"""
|
||||
try:
|
||||
pygame.mixer.music.stop()
|
||||
pygame.mixer.music.load(musicFile)
|
||||
pygame.mixer.music.set_volume(self.volumeService.get_bgm_volume())
|
||||
pygame.mixer.music.play(-1) # Loop indefinitely
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
def play_sound(self, soundName, volume=1.0):
|
||||
"""Play a sound with current volume settings applied.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user