From 0c73e988769740f1586861a383338307f7d6769d Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 5 Jul 2024 17:24:23 -0400 Subject: [PATCH] Improved sound while walking on left/right only. --- __init__.py | 73 +++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/__init__.py b/__init__.py index 901d315..5107341 100755 --- a/__init__.py +++ b/__init__.py @@ -173,45 +173,46 @@ def cut_scene(sounds, soundName): pygame.mixer.stop() pygame.event.pump() -def obj_play(sounds, soundName, playerPos, objPos): - distance = playerPos - objPos - if distance > 9 or distance < -9: - # The item is out of range, so play it at 0 - left = 0 - right = 0 - elif distance == 0: - left = 0.9 - right = 0.9 + +def calculate_volume_and_pan(player_pos, obj_pos): + distance = abs(player_pos - obj_pos) + max_distance = 12 # Maximum audible distance + if distance > max_distance: + return 0, 0, 0 # No sound if out of range + # Calculate volume (non-linear scaling for more noticeable changes) + volume = ((max_distance - distance) / max_distance) ** 1.5 + # Determine left/right based on relative position + if player_pos < obj_pos: + # Object is to the right + left = max(0, 1 - (obj_pos - player_pos) / max_distance) + right = 1 + elif player_pos > obj_pos: + # Object is to the left + left = 1 + right = max(0, 1 - (player_pos - obj_pos) / max_distance) else: - angle = math.radians(distance * 5) - left = math.sqrt(2)/2.0 * (math.cos(angle) + math.sin(angle)) - right = math.sqrt(2)/2.0 * (math.cos(angle) - math.sin(angle)) - if left < 0: left *= -1 - if right < 0: right *= -1 - # x is the channel for the sound + # Player is on the object + left = right = 1 + return volume, left, right + +def obj_play(sounds, soundName, player_pos, obj_pos): + volume, left, right = calculate_volume_and_pan(player_pos, obj_pos) + if volume == 0: + return None # Don't play if out of range + # Play the sound on a new channel x = sounds[soundName].play(-1) - # Apply the position information to the channel - x.set_volume(left, right) - # return the channel so that it can be used in the update and stop functions. + # Apply the volume and pan + x.set_volume(volume * left, volume * right) return x - -def obj_update(x, playerPos, objPos): - distance = playerPos - objPos - if distance > 9 or distance < -9: - left = 0 - right = 0 - elif distance == 0: - left = 0.9 - right = 0.9 - else: - angle = math.radians(distance * 5) - left = math.sqrt(2)/2.0 * (math.cos(angle) + math.sin(angle)) - right = math.sqrt(2)/2.0 * (math.cos(angle) - math.sin(angle)) - if left < 0: left *= -1 - if right < 0: right *= -1 - # Apply the position information to the channel - x.set_volume(left, right) - # return the channel +def obj_update(x, player_pos, obj_pos): + if x is None: + return None + volume, left, right = calculate_volume_and_pan(player_pos, obj_pos) + if volume == 0: + x.stop() + return None + # Apply the volume and pan + x.set_volume(volume * left, volume * right) return x def obj_stop(x):