因此,我一直在创建此游戏,并且我正在从加载屏幕过渡到主菜单屏幕,到目前为止,加载屏幕确实可以很好地过渡到主菜单屏幕,但是我不确定如何播放音乐进入主菜单屏幕。我尝试了一种方法,但结果只是音乐从一开始就不断播放。
我制作了一个名为“主菜单”的功能,该功能在背景上显示主菜单的图片,我也尝试定义变量并在同一功能上播放音乐文件。
#Importing some stuff...
#initialising python
pygame.init()
#pygame.mixer.init()
pygame.mixer.pre_init(44100,16,2,4096)
mixer.init()
#define display
W, H = 1600,900
HW, HH = (W/2), (H/2)
AREA = W * H
#MusicBoolean
MenuMusic = pygame.mixer.music.load("MainMenu.mp3")
MenuMusic = pygame.mixer.music.set_volume(0.45)
#initialising display
CLOCK = pygame.time.Clock()
DS = pygame.display.set_mode((W, H))
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
FPS = 54
progress = 0
background = pygame.Surface(DS.get_size())
smallfont = pygame.font.SysFont("century gothic",25)
#background image
bg = pygame.image.load("Daytime.jpg").convert()
loadingimg = pygame.image.load("LoadingScreen.png").convert()
pause = pygame.image.load("Pause screen.png").convert()
gameover = pygame.image.load("Game Over.png").convert()
mainmenu = pygame.image.load("Main_Menu4.png").convert()
#mainmenu = pygame.transform.smoothscale(mainmenu, (W,H))
loadingimg = pygame.transform.smoothscale(loadingimg, (W,H))
#define some colours
BLACK = (0,0,0,255)
WHITE = (255,255,255,255)
green = (0,140,0)
grey = (180,180,180)
def text_objects(text, color, size):
if size == "small":
textSurface = smallfont.render(text, True, color)
return textSurface, textSurface.get_rect()
def loading(progress):
if progress < 100:
text = smallfont.render("Loading: " + str(int(progress)) + "%", True, WHITE)
else:
text = smallfont.render("Loading: " + str(100) + "%", True, WHITE)
DS.blit(text, [50, 660])
def message_to_screen(msh, color, y_displace = 0, size = "small"):
textSurf, textRect = text_objects(msg, color, size)
textRect.center = HW, HH + y_displace
DS.blit(textSurf, textRect)
while (progress/4) < 100:
event_handler()
DS.blit(loadingimg, (0,0))
time_count = (random.randint(1,1))
increase = random.randint(1,20)
progress += increase
pygame.draw.rect(DS, green, [50, 700, 402, 29])
pygame.draw.rect(DS, grey, [50, 701, 401, 27])
if (progress/4) > 100:
pygame.draw.rect(DS, green, [50, 700, 401, 28])
else:
pygame.draw.rect(DS, green, [50, 700, progress, 28])
loading(progress/4)
pygame.display.flip()
time.sleep(time_count)
def main_menu():
DS.blit(mainmenu, (0, 0))
pygame.display.update()
MenuMusic = pygame.mixer.music.play()
#mainloop
while run:
#some code...
main_menu()
由于启动主菜单过渡效果很好,我希望主菜单开始在其上播放背景音乐。 但是,相反,我要么在后台播放奇怪的声音,要么根本没有声音播放,而且python / pygame shell也没有给出错误。