简体   繁体   English

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

[英]How do I add the music to my game at the right time?

The question may seem a bit strange, so I'll describe it better here: 这个问题似乎有点奇怪,因此我将在这里对其进行更好的描述:

I have managed to add a song to my game, and it works perfectly. 我已经设法在游戏中添加了歌曲,并且效果很好。 The thing is that when I start the game after I click play in the menu the song starts to play, but the screen stays black and it won't switch to the game's screen. 问题是,当我单击菜单中的“播放”后开始游戏时,歌曲开始播放,但屏幕保持黑色,并且不会切换到游戏的屏幕。 I have to click the exit button on the top right corner so it can exit the black screen and go to the game screen with the music still playing. 我必须单击右上角的退出按钮,这样它才能退出黑屏并转到播放音乐的游戏屏幕。

How do I make the music start playing after it has displayed the screen with the game, and not before? 如何在音乐与游戏一起显示之后而不是之前开始播放音乐?

You can see in iniciar() the code for the music to play 您可以在iniciar()中看到播放音乐的代码

Thanks in advance fellow programmers! 在此先感谢其他程序员!

# -*- 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)

Just remove this part 只需删除这部分

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)

from the iniciar function. 来自iniciar功能。

What's the point of this loop anyway (other than causing the behaviour you don't want)? 无论如何,这个循环有什么意义(除了导致您不想要的行为之外)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM