Random play functions for positional audio and positional falling audio.

This commit is contained in:
Storm Dragon 2025-02-02 17:04:04 -05:00
parent d5c79c0770
commit 658709ebce

View File

@ -369,6 +369,65 @@ def play_random(sounds, soundName, pause = False, interrupt = False):
if pause == True:
time.sleep(sounds[randomKey].get_length())
def play_random_positional(sounds, soundName, playerX, objectX):
"""Play a random sound with positional audio.
Args:
sounds: Dictionary of sound objects
soundName: Base name of sound (e.g. 'falling_skull' will match 'falling_skull1', 'falling_skull2', etc.)
playerX: Player's x position for audio panning
objectX: Object's x position for audio panning
Returns:
The sound channel object for updating position
"""
keys = [k for k in sounds.keys() if k.startswith(soundName)]
if not keys:
return None
randomKey = random.choice(keys)
volume, left, right = calculate_volume_and_pan(playerX, objectX)
if volume == 0:
return None
channel = sounds[randomKey].play()
if channel:
channel.set_volume(volume * left, volume * right)
return channel
def play_random_falling(sounds, soundName, playerX, objectX, startY, currentY=0, maxY=20):
"""Play a random sound with positional audio that increases in volume as it 'falls'.
Args:
sounds: Dictionary of sound objects
soundName: Base name of sound (e.g. 'falling_skull' will match 'falling_skull1', 'falling_skull2', etc.)
playerX: Player's x position for audio panning
objectX: Object's x position for audio panning
startY: Starting Y position (0-20, higher = quieter start)
currentY: Current Y position (0 = ground level)
maxY: Maximum Y value (default 20)
Returns:
The sound channel object for updating position/volume
"""
keys = [k for k in sounds.keys() if k.startswith(soundName)]
if not keys:
return None
randomKey = random.choice(keys)
# Calculate horizontal positioning
volume, left, right = calculate_volume_and_pan(playerX, objectX)
if volume == 0:
return None
# Calculate vertical fall volume multiplier (0 at maxY, 1 at y=0)
fallMultiplier = 1 - (currentY / maxY)
channel = sounds[randomKey].play()
if channel:
channel.set_volume(volume * left * fallMultiplier, volume * right * fallMultiplier)
return channel
def instructions():
# Read in the instructions file
try: