我正在学习用Kivy制作一个游戏来编写Python程序,但是在实现不同事件的声音时遇到了困难(例如,shield_正在播放()当盾牌项目被拿起。)因为游戏更新循环似乎暂停了一段时间,直到声音结束播放。我在这里做了一个简短的相关代码。。。在
shield_on = soundLoader('shield_on.wav')
class game(Widget):
#...loads of other stuff...
def update_loop(foo):
self.player_one.update()
self.player_two.update()
self.player_item_collision_detector()
if "game_file_says_player_one's_shields_are on":
self.player_one.drawShield()
shield_on.play()
现在,我只需将我的声音加载到全局。我知道这很糟糕,但他们也是我唯一的全球新闻。然后有一个包含游戏本身的小部件,它有很多东西和一个更新循环。。。它会更新玩家的位置,检查是否与物品发生碰撞,在碰撞时,这个盾牌在游戏文件中注册为“开启”。然后更新循环会检查游戏文件中“护盾”的状态,看到它们打开了,应该会播放声音。在
声音播放得很好,但是循环似乎停止,直到它播放完声音。基本上,玩家会停一微秒。如何使更新循环不等待声音结束。。。?在
PyGame的解释和解决方法:
问题原因如下:github.com/kivy/kivy/issues/2728 基本上声音加载程序.load()函数应返回最适合播放传递给它的声音文件的类。它最终没有做到这一点,据我所知,问题不在于Kivy而是GStreamer。这会导致应用程序的帧速率出现显著的临时下降—无论您在何处调用.play()方法。在
两种可能的解决方案是github线程中的offerend; 1) 或者使用SoundSDL2确保直接返回合适的类 2) 改用PyGame
我实现了后者,效果很好。在
希望这对其他人有所帮助!在
我想说,上面的答案也很有用,因为它使我能够确定真正的问题。我会投一票,但我在这里还没有名声。在
Multithreading可能是您的解决方案。在
相关问题 更多 >
编程相关推荐