<分区>
我正在尝试为我的游戏获取背景音乐,但我似乎无法完全弄清楚。我过去使用过 pygame,但在我的游戏演职员表中它只用于一首歌。 我希望播放列表随机播放每首轨道。我已经设法让它在一个单独的测试文件中工作。我将在下面发布此代码。
问题是当我在我的主游戏中调用这个函数时,音乐播放第一首轨道,然后停止。如果我输入
while pygame.mixer.music.get_busy():
continue
它只播放音乐,不让我玩游戏。我希望它在用户玩游戏时不断循环播放列表(这是一个基于文本的游戏,所以它经常使用 raw_input()
。
这是我的代码:
import pygame
import random
pygame.mixer.init()
_songs = [songs, are, here]
_currently_playing_song = None
def music():
global _currently_playing_song, _songs
next_song = random.choice(_songs)
while next_song == _currently_playing_song:
next_song = random.choice(_songs)
_currently_playing_song = next_song
pygame.mixer.music.load(next_song)
pygame.mixer.music.play()
while True: ## This part works for the test, but will not meet my needs
music() ## for the full game.
while pygame.mixer.music.get_busy():
continue
(P.S. 我是通过 Zed Shaw 的“Learn Python The Hard Way”学习 Python 的,所以我的游戏结构使用了书中的 Engine 和 Map 系统)