这个问题可能看起来有点奇怪,所以我会在这里更好地描述:
我设法为我的游戏添加了一首歌,它完美无缺。事情是当我在菜单中单击播放后开始游戏时,歌曲开始播放,但屏幕保持黑色并且不会切换到游戏的屏幕。我必须单击右上角的退出按钮,以便它可以退出黑屏并转到游戏屏幕,音乐仍在播放。
如何在游戏中显示屏幕之后开始播放音乐,而不是之前?
你可以在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)
答案 0 :(得分:0)
只需删除此部分
即可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
函数的。
无论如何,这个循环有什么意义(除了导致你不想要的行为)?