def play_sound(self, sound: pygame.mixer.Sound): """ Plays the sound with volume proportional to distance to player If that sound is already played, it is stopped and played again with better volume This is done to remove sounds ugly overlaying :param sound: sound to play :return: None """ # The volume is calculated as a upside-down parabola # pygame converts negative volume to 1 volume = max( 0, -0.000001 * (self.game.player.pos - self.pos).length_squared() + 1) if sound.get_num_channels() == 0 or sound.get_volume() < volume: sound.stop() sound.set_volume(volume) sound.play()
def play(sound: pygame.mixer.Sound): volume = get_sound_volume() sound.set_volume(volume) sound.play()
def play(sound: pygame.mixer.Sound, volume=False) -> None: volume = get_sound_volume() if volume is False else volume/10 # 0->10 must be divided by ten to be in 0->1 range sound.set_volume(volume) sound.play()