pygame中没有播放背景音乐 [英] background music not playing in pygame
本文介绍了pygame中没有播放背景音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行代码时,它只播放 .wav 文件
When i run the code it play only the .wav file
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 吗?
someone know what i should do for the background.ogg to be played too?
推荐答案
删除这一行:
pygame.mixer.music.stop()
基本上,您正在加载和播放背景噪音,然后立即停止它.我还建议您创建一个主循环
,如下所示:
Basically, you're loading and playing the background noise, and then immediately stopping it. I also suggest you create a main loop
, like so:
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
并退出程序).
Most of the time people just check if Escape
was pressed in their main loop (if it was pressed, they set running = False
and exit the program).
这篇关于pygame中没有播放背景音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文