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

标签 python pygame toggle keypress

好的,这是我的代码:

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/

相关文章:

python - 为 Python 应用程序设置 Mysql 容器的 Docker

python - 如何在单元测试中验证python日志格式?

python - 如何根据用户最后输入的键更改角色的图像?

python - 游戏设计 - 处理奖金/鸭子类型(duck typing) - python

javascript - 多个复选框分别打开切换 div。同一时间只能选择一项

javascript - jquery 切换类和 mouseup 按钮问题

python - 终端上 Python 的着色异常

python - MySQLdb 属性错误 : 'module' object has no attribute 'commit'

python - 如何将图像blit到pygame中与矩形大小相同的表面

javascript - 如何设置通过淡入/淡出来工作(并简化)此显示/隐藏?