如何在正确的时间将音乐添加到游戏中?

2024-09-30 18:24:08 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题可能有点奇怪,所以我在这里会更好地描述它:

我已经设法添加了一首歌到我的游戏,它的作品完美。问题是,当我开始游戏后,我在菜单中单击播放歌曲开始播放,但屏幕保持黑色,它不会切换到游戏的屏幕。我必须点击右上角的退出按钮,这样它就可以退出黑屏,进入游戏屏幕,音乐还在播放。你知道吗

如何使音乐在游戏屏幕上显示后开始播放,而不是之前?

您可以在iniciar()中看到要播放的音乐的代码

提前感谢各位程序员!你知道吗

# -*- coding: cp1252 -*-
import pygame
from pygame import init, display, Color, key, quit, joystick, time
from pygame.locals import *
from sys import exit
from tygame.main import StaticFrame, Button, Label, render_widgets, handle_widgets 
from Clase import *
import Funcion


def But_X_Y(size):

    global mylaby
    global perso

    mylaby = laby(size[0], size[1])
    mylaby.generate_laby()
    perso = Perso(mylaby)

def But_path():

    global perso

    perso.che_jaune = []
    perso.reverse = 0
    perso.astar(((perso.laby.w - 1), (perso.laby.h - 1)))
    camino = perso.get_astar((perso.x, perso.y), ((perso.laby.w - 1), (perso.laby.h - 1)))
    perso.go_to(chemain)


def iniciar():
    done = False
    clock = pygame.time.Clock()
    pygame.mixer.music.load('cod4.mp3')
    pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
    pygame.mixer.music.play()
    while not done:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                done = True 
            elif event.type == pygame.constants.USEREVENT:
                pygame.mixer.music.load('cod4.mp3')
                pygame.mixer.music.play()
        clock.tick(20)
    width, height = 720, 480
    Window = display.set_mode((width, height)) 
    pygame.init()

    display.set_caption("El laberinto mas pelado del mundo")
    mylaby = laby(25, 30)
    mylaby.generate_laby()
    perso = Perso(mylaby)
    perso_time = 0
    lista3 = Funcion.lista1(width, height)
    lista2 = Funcion.lista2(lista3)
    key.set_repeat(50, 55)


    while True:

        time.Clock().tick(30)
        Window.fill(const.Porange)    

        for event in handle_widgets():
            if event.type == QUIT:
                quit()
                exit()


        flechas = key.get_pressed()
        if flechas:
            if flechas[K_UP]:
                if not perso.che_jaune:
                    perso.move(const.arriba)
            if flechas[K_DOWN]:
                if not perso.che_jaune:
                    perso.move(const.abajo)
            if flechas[K_LEFT]:
                if not perso.che_jaune:
                    perso.move(const.izquierda)
            if flechas[K_RIGHT]:
                if not perso.che_jaune:
                    perso.move(const.derecha)


        if time.get_ticks() - perso_time >= const.time_perso_poll:
            perso_time = time.get_ticks()
            perso.poll()


        perso.show(Window)
        render_widgets()

        pygame.display.flip()



        if perso.x == perso.laby.w - 1 and perso.y == perso.laby.h - 1:
            time.delay(300)       

            for Bip in lista2:
                Bip.show(Window)
                display.flip()                     


            mylaby = laby(50, 50)
            mylaby.generate_laby()
            perso = Perso(mylaby)

            while True:
                Window.fill(const.Pblue)
                perso.show(Window)
                render_widgets()

                if not lista2: break

                i = 0
                while i < 24:
                    lista2.remove(choice(lista2))  
                    i += 1


                for Bip in lista2:
                    Bip.show(Window)

                display.flip()

            lista2 = Fonction.fill_lista2(lista3)

Tags: importevent游戏iftimedisplaynotwindow
1条回答
网友
1楼 · 发布于 2024-09-30 18:24:08

把这部分去掉

while not done:
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True 
        elif event.type == pygame.constants.USEREVENT:
            pygame.mixer.music.load('cod4.mp3')
            pygame.mixer.music.play()
    clock.tick(20)

iniciar函数。你知道吗

这个循环到底有什么意义(除了导致你不想要的行为之外)?你知道吗

相关问题 更多 >