我正在嘗試爲我的遊戲獲取背景音樂,但我似乎無法完全弄清楚它。我過去使用過pygame,但是在我的遊戲中有一首歌。 我希望播放列表隨機播放每首曲目。我已經成功地在一個單獨的測試文件中工作。我將在下面發佈此代碼。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
(PS我通過學會蟒蛇肖捷思銳的「學習Python辛苦」的,所以我的遊戲結構採用從書的引擎和地圖系統)
[這是一個答案](https://stackoverflow.com/a/45597549/6220679),告訴你如何播放隨機歌曲,如果最後一首歌曲完成或按下右箭頭循環播放歌曲列表鍵。 – skrx