是pygame.mixer.music.pause应该会妨碍我的工作



我正在开发一款用python 3 pygame编写的游戏。我想介绍一个功能,玩家可以按S暂停游戏进度(场景和音乐),然后再按一次继续。

但我发现pygame.mixer.music.pause()从来都不起作用,正因为如此,pygame.mixer.musical.get_busy()总是正确的。

stop()有效,但我仍然需要pause(),这样我就可以从音乐暂停的地方继续。

以下是我为缩小问题的可能范围而编写的程序:

import pygame
import random,sys
from pygame.locals import *
''''conclusion: pause() is not working(seems it triggers nothing, and 
as a result, get_busy() is not working with pause() )
'''
# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
red      = ( 255,   0,   0)
blue     = (   0,   0, 255)
purple = (159, 0, 197)
bgColor= (0,   0,   0)
textColor = (250, 250, 255)
width = 600 
height = 600
def end():
    pygame.quit()
    sys.exit()
def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, textColor)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)
pygame.init()
screen = pygame.display.set_mode([width,height])
pygame.mixer.music.load('background.mid')  
font = pygame.font.SysFont(None, 48)
clock = pygame.time.Clock()
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.5)
paused=False
while True:  #game loop when the game is playing.    
    if not pygame.mixer.music.get_busy(): #is not playing
        print("Not playing")
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            end();       
        elif event.type == KEYUP:    
            if event.key == K_ESCAPE:
                end()   
            elif event.key == K_s:    
                print("pause.")
                #pygame.mixer.music.pause() 
                pygame.mixer.music.stop()

    screen.fill(bgColor)
    pygame.display.flip() 
    clock.tick(40)         

最后,我尝试了其他格式的背景音乐。我把音乐格式从midi改成了mp3或ogg,pause()起了作用。所以这里的问题是格式。

此外,get_busy()并没有像pygame文档所说的那样:pause()和unuse()不会更改get_busy()的返回值。

最新更新