1

如果标题不够清晰,目录中有 Developer/Pythonfiles/Pong,其中有 Pong.py 和 hit.wav。当我运行~ $python Developer/Pythonfiles/Pong/Pong.py
声音 hit.wav 时不会播放,而是播放奇怪的爆裂声。但是当我~/Developer/Pythonfiles/Pong $python Pong.py从 hit.wav 运行声音时,会按预期播放。

为什么程序 Pong.py 不搜索它所在的目录而不是我的工作目录来查找 Hit.wav?我尝试访问 hit.wavhit = pygame.mixer.Sound("hit.wav")并播放它hit.play()

4

1 回答 1

5

因为它无法自动知道这就是你想要的。因此,请自己指定所需的行为:

import os
hit = pygame.mixer.Sound(os.path.join(os.path.dirname(__file__), "hit.wav"))
于 2013-03-19T06:13:45.367 回答