From 173220d16725cabb56a2a85d8e6365fcd0b806a2 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Mon, 24 Feb 2025 14:32:45 -0500 Subject: [PATCH] New function for directional audio that needs to be good volume no matter position but indicate direction. --- __init__.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/__init__.py b/__init__.py index ec11b9c..d22fe8f 100755 --- a/__init__.py +++ b/__init__.py @@ -635,6 +635,40 @@ def play_random_positional(sounds, soundName, player_x, object_x): volume * right * sfxVolume) return channel +def play_directional_sound(sounds, soundName, playerPos, objPos, centerDistance=3, volume=1.0): + """Play a sound with simplified directional audio. + + For sounds that need to be heard clearly regardless of distance, but still provide + directional feedback. Sound plays at full volume but pans left/right based on relative position. + + Args: + sounds (dict): Dictionary of sound objects + soundName (str): Name of sound to play + playerPos (float): Player's x position + objPos (float): Object's x position + centerDistance (float): Distance within which sound plays center (default: 3) + volume (float): Base volume multiplier (0.0-1.0, default: 1.0) + + Returns: + pygame.mixer.Channel: The channel the sound is playing on + """ + channel = sounds[soundName].play() + if channel: + # Apply volume settings + finalVolume = volume * sfxVolume * masterVolume + + # If player is within centerDistance tiles of object, play in center + if abs(playerPos - objPos) <= centerDistance: + # Equal volume in both speakers (center) + channel.set_volume(finalVolume, finalVolume) + elif playerPos > objPos: + # Object is to the left of player + channel.set_volume(finalVolume, 0) + else: + # Object is to the right of player + channel.set_volume(0, finalVolume) + return channel + def cut_scene(sounds, soundName): """Play a sound as a cut scene, stopping other sounds.