首页 > 解决方案 > pygame.mixer.music,只要 while 循环正在运行,我就想播放曲目

问题描述

import pygame    
import sys  
import time  
from pygame.locals import *

pygame.init()
pygame.mixer.pre_init(44100, 16, 2, 4096)

#music files variables
eatsound = ("E:\p\code\eat.mp3")
gameoversound = ("E:\p\code\gameovertrimmed.mp3")
main = ('E:\p\code\mainm.mp3')


playsurface = pygame.display.set_mode((720, 480))
pygame.display.set_caption('Snake Game!')

fpscontroller = pygame.time.Clock()

while True: 
    pygame.mixer.music.load(main)
    pygame.mixer.music.play(-1, 0.0)

我想让音轨作为背景声音播放,直到循环结束。但是,while 循环不允许它这样做

标签: pythonpython-3.xpygame

解决方案


一旦

  pygame.mixer.music.play(-1, 0.0)

方法被调用,不需要在你的while循环中一遍又一遍地调用它

初始化循环时调用它一次,然后在 while 循环退出时停止它


推荐阅读