如何添加暂停菜单以及如何在暂停菜单中播放没有鼓的音乐[重复]

问题描述 投票:0回答:1

我很想在这个游戏中添加一个暂停菜单,但是我希望当我放置暂停菜单时,音乐中的鼓声会从音乐中消失,而当我重新进入游戏时,鼓声会在音乐又开始了,这样可以吗?

import pygame
import time
import random
 
pygame.init()
 
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
 
dis_width = 1280
dis_height = 720
 
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('Zio peppe - Serpente Edition')
 
clock = pygame.time.Clock()
 
snake_block = 10
snake_speed = 15
 
font_style = pygame.font.SysFont("bahnschrift", 19)
score_font = pygame.font.SysFont("8514oem Normale", 35)
 
 
def Your_score(score):
    value = score_font.render("Punteggio:" + str(score), True, yellow)
    dis.blit(value, [0, 0])
 
 
 
def our_snake(snake_block, snake_list):
    for x in snake_list:
        pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
 
 
def message(msg, color):
    mesg = font_style.render(msg, True, color)
    dis.blit(mesg, [dis_width / 6, dis_height / 3])
 
pygame.mixer.init()
pygame.mixer.music.load("C:\\Users\\Ercol\\Desktop\\music.mp3") 
pygame.mixer.music.play(loops=-1)

def gameLoop():
    game_over = False
    game_close = False
 
    x1 = dis_width / 2
    y1 = dis_height / 2
 
    x1_change = 0
    y1_change = 0
 
    snake_List = []
    Length_of_snake = 1
 
    foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
    foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
 
    while not game_over:
 
        while game_close == True:
            dis.fill(blue)
            message("Che nobo. Premi E per Ricominciare o Q per uscire.", red)
            Your_score(Length_of_snake - 1)
            pygame.display.update()
 
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        pygame.mixer.music.fadeout(2000)
                        pygame.time.wait(1500)  # waits for 1 second
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_e:
                        gameLoop()
 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    x1_change = -snake_block
                    y1_change = 0
                elif event.key == pygame.K_d:
                    x1_change = snake_block
                    y1_change = 0
                elif event.key == pygame.K_w:
                    y1_change = -snake_block
                    x1_change = 0
                elif event.key == pygame.K_s:
                    y1_change = snake_block
                    x1_change = 0
 
        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change
        dis.fill(blue)
        pygame.draw.rect(dis,   11186139, [foodx, foody, snake_block, snake_block])
        snake_Head = []
        snake_Head.append(x1)
        snake_Head.append(y1)
        snake_List.append(snake_Head)
        if len(snake_List) > Length_of_snake:
            del snake_List[0]
 
        for x in snake_List[:-1]:
            if x == snake_Head:
                game_close = True
 
        our_snake(snake_block, snake_List)
        Your_score(Length_of_snake - 1)
 
        pygame.display.update()
 
        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
            foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
            Length_of_snake += 1
 
        clock.tick(snake_speed)
 
    pygame.mixer.music.fadeout(2000) # Fade out the music for 1 second
        # Fade out the screen before quitting
    fade_surface = pygame.Surface((dis_width, dis_height))
    fade_surface.fill(black)
    for alpha in range(0, 255, 5):
        fade_surface.set_alpha(alpha)
        dis.blit(fade_surface, (0, 0))
        pygame.display.update()
        pygame.time.delay(2)
        pygame.time.wait(1000)
        pygame.quit()
    quit()
gameLoop()

有人可以帮我吗?我已经有了带鼓和不带鼓(或只有鼓)的音乐文件。

python pygame menu
1个回答
-1
投票

您可以使用 pygame.mixer 库,它有很多很好的选项可以同时播放多个声音并停止它们。我建议查看文档:Pygame Mixer Docs。您可以导入声音,将其放入频道,并在代码中随时启动和停止该频道。

例如: 如果你有一个名为“Drums.mp3”或其他名称的声音文件,你可以将其用作:

#get the sound file

drums = pygame.mixer.Sound('Drums.mp3')

#play it

drums.play(-1) #play it until stopped

paused = False

#Game loop

while True:
    #Other code.......
    
    if paused: #Stop it if it's paused
        drums.stop()
    elif not paused and drums.get_num_channels() == 0:
        drums.play(-1) #Start playing again

这是一个非常简单且有限的示例,但您应该明白了。同样,如果您稍微阅读文档,将很容易将其完全集成。

© www.soinside.com 2019 - 2024. All rights reserved.