gpt4 book ai didi

python - Pygame 音乐暂停/取消暂停切换

转载 作者:太空宇宙 更新时间:2023-11-03 18:14:30 25 4
gpt4 key购买 nike

好的,这是我的代码:

def toggleMusic():

if pygame.mixer.music.get_busy():
pygame.mixer.music.pause()

else:
pygame.mixer.music.unpause()

---事件处理---

如果按下“m”,它应该切换音乐是暂停还是不暂停

toggleMusic()

它可以暂停音乐但不能取消暂停,有什么解释吗?

最佳答案

遇到同样的问题。为了供其他人引用,我的解决方案是使用一个简单的类。

class Pause(object):

def __init__(self):
self.paused = pygame.mixer.music.get_busy()

def toggle(self):
if self.paused:
pygame.mixer.music.unpause()
if not self.paused:
pygame.mixer.music.pause()
self.paused = not self.paused

# Instantiate.

PAUSE = Pause()

# Detect a key. Call toggle method.

PAUSE.toggle()

关于python - Pygame 音乐暂停/取消暂停切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25221036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com