我怎样才能在 pygame 中平移声音?

Mat*_*ndo 3 python audio pygame python-3.x

基本上,我想在这里完成的是一种在游戏运行时平移声音的方法。我想让音量(左和右)根据玩家的位置而改变。现在我有一个简单的代码,我认为这将是一个很好的测试:

pygame.mixer.init()

self.sound = pygame.mixer.Sound("System Of A Down - DDevil #06.mp3")
print("I could get here")
self.music = self.sound.play()

self.music.set_volume(1.0, 0)
Run Code Online (Sandbox Code Playgroud)

首先,我尝试了类似的方法pygame.mixer.music,但使用 ,但我意识到没有办法以这种方式单独更改卷,或者我认为,然后我更改为此处提供的代码。现在似乎该文件无法加载,我的猜测是该文件太大而无法在 pygame 中被视为声音。知道我如何才能完成这项工作吗?

Ren*_*eld 5

您可以像这样平移频道:

from os import split, join
import pygame
import pygame.examples.aliens
pygame.init()
# get path to an example punch.wav file that comes with pygame.
sound_path = join(split(pygame.examples.aliens.__file__)[0], 'data', 'punch.wav')
sound = pygame.mixer.Sound(sound_path)
# mixer can mix several sounds together at once.
# Find a free channel to play the sound on.
channel = pygame.mixer.find_channel()
# pan volume full loudness on the left, and silent on right.
channel.set_volume(1.0, 0.0)
channel.play(sound)
Run Code Online (Sandbox Code Playgroud)

https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Channel