首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pygame Playlist在后台连续播放

Pygame Playlist在后台连续播放
EN

Stack Overflow用户
提问于 2017-09-28 09:48:50
回答 2查看 2.1K关注 0票数 3

我正在尝试为我的游戏获取背景音乐,但我似乎不能完美地弄清楚。我过去用过pygame,但它只在我的游戏演职员期的一首歌中使用过。我想让播放列表连续播放,随机挑选每首曲目。我已经设法让它在一个单独的测试文件中工作。我将在下面发布这段代码。

问题是,当我在我的主游戏中调用这个函数时,音乐播放第一首曲目,然后停止。如果我输入

代码语言:javascript
复制
while pygame.mixer.music.get_busy():
    continue

它只是播放音乐,不让我玩游戏。我想让它在用户玩游戏时不断循环播放列表(这是一个基于文本的游戏,所以它经常使用raw_input()

下面是我的代码:

代码语言:javascript
复制
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

(附注:我通过Zed Shaw的“以艰难的方式学习python”学习了Python,所以我的游戏结构使用了书中的引擎和地图系统)

EN

回答 2

Stack Overflow用户

发布于 2017-09-28 10:03:48

您可以使用线程在后台播放音乐。

代码语言:javascript
复制
import threading
musicThread = threading.Thread(target=music)
musicThread.start()

如果你想在不关闭游戏的情况下停止音乐,你应该杀死这个线程。

票数 2
EN

Stack Overflow用户

发布于 2017-09-28 14:04:09

您可以设置一个在音乐结束时发布到事件队列中的pygame.mixer.music.set_endevent()。然后你只需选择另一首歌。大致是这样的:

代码语言:javascript
复制
import os
import pygame
pygame.init()
pygame.mixer.init()

SIZE = WIDTH, HEIGHT = 720, 460
screen = pygame.display.set_mode(SIZE)

MUSIC_ENDED = pygame.USEREVENT
pygame.mixer.music.set_endevent(MUSIC_ENDED)


BACKGROUND = pygame.Color('black')


class Player:

    def __init__(self, position):
        self.position = pygame.math.Vector2(position)
        self.velocity = pygame.math.Vector2()

        self.image = pygame.Surface((32, 32))
        self.rect =  self.image.get_rect(topleft=self.position)

        self.image.fill(pygame.Color('red'))

    def update(self, dt):
        self.position += self.velocity * dt
        self.rect.topleft = self.position


def load_music(path):
    songs = []
    for filename in os.listdir(path):
        if filename.endswith('.wav'):
            songs.append(os.path.join(path, filename))
    return songs


def run():
    songs = load_music(path='/Users/Me/Music/AwesomeTracks')

    song_index = 0  # The current song to load
    pygame.mixer.music.load(songs[song_index])
    pygame.mixer.music.play()
    song_index += 1

    clock = pygame.time.Clock()
    player = Player(position=(WIDTH / 2, HEIGHT / 2))

    while True:
        dt = clock.tick(30) / 1000

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    player.velocity.x = -200
                elif event.key == pygame.K_d:
                    player.velocity.x = 200
                elif event.key == pygame.K_w:
                    player.velocity.y = -200
                elif event.key == pygame.K_s:
                    player.velocity.y = 200
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_d:
                    player.velocity.x = 0
                elif event.key == pygame.K_w or event.key == pygame.K_s:
                    player.velocity.y = 0
            elif event.type == MUSIC_ENDED:
                song_index = (song_index + 1) % len(songs)  # Go to the next song (or first if at last).
                pygame.mixer.music.load(songs[song_index])
                pygame.mixer.music.play()

        screen.fill(BACKGROUND)

        player.update(dt)
        screen.blit(player.image, player.rect)

        pygame.display.update()

run()

所以实际的解决方案只有3个部分

对于event queue for event in pygame.event.get(): if event.type == MUSIC_ENDED:中的事件,当一首歌曲完成pygame.mixer.music.set_endevent(MUSIC_ENDED)

  • Check时,
  1. 会创建一个event pygame.mixer.music.set_endevent(MUSIC_ENDED)
  2. Check pygame来发布事件

然后你就可以自由地做任何你想做的事情。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46459719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档