pygame音乐暂停/取消暂停切换 [英] Pygame music pause/unpause toggle
本文介绍了pygame音乐暂停/取消暂停切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,这是我的代码:
def toggleMusic():
if pygame.mixer.music.get_busy():
pygame.mixer.music.pause()
else:
pygame.mixer.music.unpause()
-事件处理----
如果按"m"键,则应切换音乐是暂停还是不暂停
if pressed 'm' it should toggle whether the music is paused and not paused
toggleMusic()
它可以暂停音乐但不能取消暂停,有任何解释吗?
It can pause the music but not unpause, any explanation?
推荐答案
它不会取消暂停音乐,因为pygame.mixer.music.pause()
不会影响pygame.mixer.music.get_busy()
的状态.
It doesn't unpause the music because pygame.mixer.music.pause()
doesn't affect the state of pygame.mixer.music.get_busy()
.
要获得所需的行为,您需要维护自己的变量,该变量可以跟踪暂停/未暂停状态.您可以在一堂课中做到这一点:
To get the behavior you are looking for you will need to your maintain your own variable which keeps track of the paused/unpaused state. You can do this in a class:
class mixerWrapper():
def __init__(self):
self.IsPaused = False
def toggleMusic(self):
if self.IsPaused:
pygame.mixer.music.unpause()
self.IsPaused = False
else:
pygame.mixer.music.pause()
self.IsPaused = True
这篇关于pygame音乐暂停/取消暂停切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文