Updated play_falling_random.
This commit is contained in:
parent
658709ebce
commit
5fa90f9e84
39
__init__.py
39
__init__.py
@ -395,8 +395,8 @@ def play_random_positional(sounds, soundName, playerX, objectX):
|
||||
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'.
|
||||
def play_random_falling(sounds, soundName, playerX, objectX, startY, currentY=0, maxY=20, existingChannel=None):
|
||||
"""Play or update a random sound with positional audio that increases in volume as it 'falls'.
|
||||
|
||||
Args:
|
||||
sounds: Dictionary of sound objects
|
||||
@ -406,26 +406,41 @@ def play_random_falling(sounds, soundName, playerX, objectX, startY, currentY=0,
|
||||
startY: Starting Y position (0-20, higher = quieter start)
|
||||
currentY: Current Y position (0 = ground level)
|
||||
maxY: Maximum Y value (default 20)
|
||||
existingChannel: Existing sound channel to update instead of creating new one (default None)
|
||||
|
||||
Returns:
|
||||
The sound channel object for updating position/volume
|
||||
The sound channel object for updating position/volume, or None if sound should stop
|
||||
"""
|
||||
# Calculate horizontal positioning
|
||||
volume, left, right = calculate_volume_and_pan(playerX, objectX)
|
||||
|
||||
# Calculate vertical fall volume multiplier (0 at maxY, 1 at y=0)
|
||||
fallMultiplier = 1 - (currentY / maxY)
|
||||
|
||||
# Adjust final volumes
|
||||
finalVolume = volume * fallMultiplier
|
||||
finalLeft = left * finalVolume
|
||||
finalRight = right * finalVolume
|
||||
|
||||
if existingChannel is not None:
|
||||
if volume == 0: # Out of audible range
|
||||
existingChannel.stop()
|
||||
return None
|
||||
existingChannel.set_volume(finalLeft, finalRight)
|
||||
return existingChannel
|
||||
else: # Need to create new channel
|
||||
if volume == 0: # Don't start if out of range
|
||||
return None
|
||||
|
||||
# Find matching sound files
|
||||
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)
|
||||
channel.set_volume(finalLeft, finalRight)
|
||||
return channel
|
||||
|
||||
def instructions():
|
||||
|
Loading…
x
Reference in New Issue
Block a user