def stop_playing(self, sound: pygame.mixer.Sound, fade_ms=0): if not PLAY_SOUNDS: return if sound not in self.playing_sounds: return if self.playing_sounds[sound].get_busy(): if fade_ms: sound.fadeout(fade_ms) else: sound.stop()
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 pause_audio(audio: pygame.mixer.Sound) -> None: audio.stop()