我做了一个按钮类,检查是否选中了一个按钮(当鼠标悬停在按钮上时).选择,未选中或单击按钮时,它将播放一个wav文件.问题是声音播放和按钮状态变化之间存在巨大延迟.该程序应检查每一帧,看看是否已满足播放声音的条件,但fps似乎不是问题(60和600 fps给出相同的延迟).我试过减少缓冲区值,pygame.mixer.init()但也没有差别.
声音文件:
buttonSoundSelect = pygame.mixer.Sound(os.path.join(soundPath, "button1.wav"))
buttonSoundUnselect = pygame.mixer.Sound(os.path.join(soundPath, "button2.wav"))
buttonSoundClick = pygame.mixer.Sound(os.path.join(soundPath, "button3.wav"))
buttonSounds = [buttonSoundSelect, buttonSoundUnselect, buttonSoundClick]
Run Code Online (Sandbox Code Playgroud)
创建对象:
playButton = button(textInactive = "Play", font = mainFont, sounds = buttonSounds, command = playAction)
Run Code Online (Sandbox Code Playgroud)
来自按钮类的代码,用于检查是否选择了按钮(这是在.display每帧调用的方法内):
if pygame.mouse.get_pos()[0] >= self.x and pygame.mouse.get_pos()[0] <= self.x + self.width and \
pygame.mouse.get_pos()[1] >= self.y and pygame.mouse.get_pos()[1] <= self.y + self.height:
self.surfaceActive.blit(self.textSurfaceActive, (self.width / 2 - self.font.size(self.textActive)[0] / 2,
self.height / 2 - self.font.size(self.textActive)[1] / 2))
self.surface.blit(self.surfaceActive, (self.x, self.y))
if …Run Code Online (Sandbox Code Playgroud)