Fix an error with sound positioning in play_sound.

This commit is contained in:
Storm Dragon 2025-03-17 20:47:41 -04:00
parent d513927d52
commit e0fab91260

View File

@ -84,27 +84,28 @@ class Sound:
def play_sound(self, soundName, volume=1.0, loop=False): def play_sound(self, soundName, volume=1.0, loop=False):
"""Play a sound with current volume settings applied. """Play a sound with current volume settings applied.
Args: Args:
soundName (str): Name of sound to play soundName (str): Name of sound to play
volume (float): Base volume for the sound (0.0-1.0, default: 1.0) volume (float): Base volume for the sound (0.0-1.0, default: 1.0)
loop (bool): Whether to loop the sound (default: False) loop (bool): Whether to loop the sound (default: False)
Returns: Returns:
pygame.mixer.Channel: The channel the sound is playing on pygame.mixer.Channel: The channel the sound is playing on
""" """
if soundName not in self.sounds: if soundName not in self.sounds:
return None return None
sound = self.sounds[soundName] sound = self.sounds[soundName]
# Set loop parameter to -1 for infinite loop or 0 for no loop # Set loop parameter to -1 for infinite loop or 0 for no loop
loopParam = -1 if loop else 0 loopParam = -1 if loop else 0
channel = sound.play(loopParam) channel = sound.play(loopParam)
if channel: if channel:
channel.set_volume(volume * self.volumeService.get_sfx_volume()) sfx_volume = volume * self.volumeService.get_sfx_volume()
channel.set_volume(sfx_volume, sfx_volume) # Explicitly set both channels to ensure centered sound
# Store in active loops if looping # Store in active loops if looping
if loop: if loop:
self.activeLoops[soundName] = { self.activeLoops[soundName] = {
@ -112,7 +113,7 @@ class Sound:
'sound': sound, 'sound': sound,
'volume': volume 'volume': volume
} }
return channel return channel
def stop_sound(self, soundName=None, channel=None): def stop_sound(self, soundName=None, channel=None):
@ -836,7 +837,8 @@ def play_sound(sound, volume=1.0, loop=False):
channel = sound.play(loopParam) channel = sound.play(loopParam)
if channel: if channel:
channel.set_volume(volume * volumeService.get_sfx_volume()) sfx_volume = volume * volumeService.get_sfx_volume()
channel.set_volume(sfx_volume, sfx_volume) # Explicitly set both channels to ensure centered sound
return channel return channel
def obj_play(sounds, soundName, playerPos, objPos, playerY=0, objY=0, loop=False): def obj_play(sounds, soundName, playerPos, objPos, playerY=0, objY=0, loop=False):