首页 > 解决方案 > 如何在我的 py 游戏的背景中播放音乐?

问题描述

import random

time = 120

WIDTH = 800
HEIGHT = 500

background = Actor("background")
player = Actor("player")
enemy = Actor("enemy")
money = Actor("money", pos=(300, 300))
player2 = Actor("alien")
score = 0
player.x = 200
player.y = 200
player2.x = 400
player2.y = 400

def draw():
    screen.clear()
    background.draw()
    player.draw()
    enemy.draw()
    money.draw()
    player2.draw()
    screen.draw.text("YFF-Spill score:", (200, 0), color="white")
    score_string = str(score)
    screen.draw.text(score_string, (327, 0), color="red")
    time_string = str(round(time))
    screen.draw.text(time_string, (50, 0), color="black")


def update(delta):
    global score, time
    time = time - delta
    if time <= 0:
        exit()
    if keyboard.right:
        player.x = player.x + 4
    if keyboard.left:
        player.x = player.x - 4
    if keyboard.down:
        player.y = player.y + 4
    if keyboard.up:
        player.y = player.y - 4
    if player.x > WIDTH:
        player.x = WIDTH
    if player.x < 0:
        player.x = 0
    if player.y < 0:
        player.y = 0
    if player.y > HEIGHT:
        player.y = HEIGHT
    if keyboard.d:
        player2.x = player2.x + 4
    if keyboard.a:
        player2.x = player2.x - 4
    if keyboard.s:
        player2.y = player2.y + 4
    if keyboard.w:
        player2.y = player2.y - 4
    if player.colliderect(player2):
        exit()
    if player2.x > WIDTH:
        player2.x = WIDTH
    if player2.x < 0:
        player2.x = 0
    if player2.y < 0:
        player2.y = 0
    if player2.y > HEIGHT:
        player2.y = HEIGHT
    dx1, dy1 = player.x - enemy.x, player.y - enemy.y
    dx2, dy2 = player2.x - enemy.x, player2.y - enemy.y
    dist1sq = dx1 * dx1 + dy1 * dy1
    dist2sq = dx2 * dx2 + dy2 * dy2

    player_near = player if dist1sq < dist2sq else player2

    if enemy.x < player_near.x:
        enemy.x += 1
    if enemy.x > player_near.x:
        enemy.x -= 1
    if enemy.y < player_near.y:
        enemy.y += 1
    if enemy.y > player_near.y:
        enemy.y -= 1
    if player.colliderect(enemy):
        exit()
    if player2.colliderect(enemy):
        exit()
    if money.colliderect(player):
        money.x = random.randint(0, WIDTH)
        money.y = random.randint(0, HEIGHT)
        score = score + 1
    if money.colliderect(player2):
        money.x = random.randint(0, WIDTH)
        money.y = random.randint(0, HEIGHT)
        score += 1
    if keyboard.d:
        player2.x = player2.x + 4
    if keyboard.a:
        player2.x = player2.x - 4
    if keyboard.s:
        player2.y = player2.y + 4
    if keyboard.w:
        player2.y = player2.y - 4
    if player.colliderect(player2):
        exit()
    if player2.x > WIDTH:
        player2.x = WIDTH
    if player2.x < 0:
        player2.x = 0
    if player2.y < 0:
        player2.y = 0
    if player2.y > HEIGHT:
        player2.y = HEIGHT

这是我在 mu 编辑器中制作的 pygame,有 2 名玩家试图躲避敌人并试图收钱。我想在我的 pygame 的背景中添加一首正在播放的歌曲,我该怎么做?我试图在 mu 编辑器的音乐文件夹中放置一个 mp3 文件,但我不确定要放入什么代码才能使其在游戏中播放。

标签: pythonpygamegame-developmentmu

解决方案


使用 PyGame 的混音器功能添加音乐非常容易。使用一个频道播放音乐,使用另一个频道播放任何音效。这些“混合”在一起。一个“问题”是混音器不喜欢 MP3 文件,因此您需要将声音转换为 .OGG 或其他支持的格式(使用Audacity之类的格式)。这是由于 MP3 格式受专利保护(是否已过期?)。

无论如何,所以想法是制作两个pygame.mixer.Channel,一个用于音乐,一个用于声音。

pygame.mixer.init()

# create separate Channel objects for simultaneous playback
channel1 = pygame.mixer.Channel(0)        # argument must be int
channel2 = pygame.mixer.Channel(1)

# Load and play a continuously looping music
background_sound = pygame.mixer.Sound( 'background_music.ogg' )
channel1.play( background_sound, -1 )        # loop the music forever

所以现在背景音乐将永远循环播放channel1

要播放单独的基于事件的声音,请使用channel2

whack_sound = pygame.mixer.Sound( 'whack.ogg' )
bloop_sound = pygame.mixer.Sound( 'bloop-bloop.ogg' )

# In main loop
    [...]
    channel2.play( bloop_sound )

推荐阅读