[英]PyGame: Unable to open file
我正在尝试《用Python和Pygame制作游戏》一书中的一些基本示例,但是我遇到了一个奇怪的问题。 这是示例源:
import pygame, time
soundObj = pygame.mixer.Sound('beep.wav')
soundObj.play()
time.sleep(1) # wait and let the sound play for 1 second
soundObj.stop()
此源产生以下错误:
追溯(最近一次通话):文件“ C:/Users/Thiago/PycharmProjects/PyGame/Sound/app.py”,第3行,在soundObj = pygame.mixer.Sound('beep.wav')pygame.error中:无法打开文件'beep.wav'
beep.wav文件已正确保存在我的Python脚本的同一文件夹中。 我尝试了os.listdir()
命令,它返回了wav文件。 是否有任何问题,已知错误或我做错了什么?
这是我的环境:
您必须先初始化模块或所有pygame。 有一个pygame_init()初始化程序可以帮助您。 你可以在这里找到
我解决了这个问题,添加了pygame.init()
初始化Pygame和pygame.display.set_mode()
来创建窗口。
import pygame, time
pygame.init() # initialize the pygame
soundObj = pygame.mixer.Sound('beep.wav')
DISPLAYSURF = pygame.display.set_mode((400, 300)) # create the game window
soundObj.play()
time.sleep(1) # wait and let the sound play for 1 second
soundObj.stop()
brew install libogg brew install libvorbis brew install sdl_mixer --with-libvorbis brew reinstall sdl_mixer --with-libvorbis
对我来说这个功能:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.