背景音乐不在pygame中播放

时间:2014-08-02 21:26:12

标签: python pygame

当我运行代码时,它只播放.wav文件

import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)

pygame.mixer.music.load('background.ogg')
pygame.mixer.music.play()

soundObj = pygame.mixer.Sound('bird.wav')
soundObj.play()
pygame.mixer.music.stop()

有人知道我应该为background.ogg做什么呢?

1 个答案:

答案 0 :(得分:3)

删除此行:

pygame.mixer.music.stop()

基本上,你正在加载和播放背景噪音,然后立即停止它。我还建议你创建一个main loop,如下所示:

import pygame
from pygame.locals import *

# This makes sure that you're not importing the module.
if __name__ == "__main__":
    # Create surface
    size = width, height = (500, 400)
    # You don't need the extra arguments
    window = pygame.display.set_mode(size)
    # Do sound stuff
    pygame.mixer.music.load('background.ogg')
    pygame.mixer.music.play()

    soundObj = pygame.mixer.Sound('bird.wav')
    soundObj.play()
    # This is your main loop.
    running = True
    while running:
        # Check if escape was pressed

大多数时候,人们只是检查主循环中是否按下Escape(如果按下了,则设置running = False并退出程序。)