Pygame程序在切换场景后退出(几个循环)

时间:2017-07-12 14:04:51

标签: python pygame

我的Pygame游戏存在问题。当导航到新游戏屏幕,然后返回并单击选项按钮时,游戏简单关闭并且不会给我一个错误或任何出错的迹象,但如果我点击它而不去新游戏就有效屏幕先..请告诉我我错过了什么。

以下是代码:

global new_game
global menu
global second_menu
global options_2
global galaxies

# start screen loop &variable
start_game = True
new_game = False
options_2 = False


def game_start():
    print ("hello")
    start_game = True
    options_2 = True
    while start_game:
        pygame.event.pump()
        screen.blit(background, background_rect)

        for option in menu:
            if option.rect.collidepoint(pygame.mouse.get_pos()):
                option.hovered = True
            else:
                option.hovered = False
            option.draw()
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    mouse_x, mouse_y = event.pos
                    if (mouse_x >= corner1[0]) and (mouse_x <= corner1[0]+150) and (mouse_y >=corner1[1]) and (mouse_y <= corner1[1]+25):
                        print ("You clicked New Game")
                        start_game = False
                        options_2 = False
                        global new_game
                        new_game = True

            if event.type == pygame.QUIT:
                start_game = False
                options_2 = False
                options = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    mouse_x, mouse_y = event.pos
                    if (mouse_x >= corner3[0]) and (mouse_x <= corner3[0]+150) and (mouse_y >=corner3[1]) and (mouse_y <= corner3[1]+25):
                        print ("You clicked Load Game")
                        #start_game = False



            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    mouse_x, mouse_y = event.pos
                    if (mouse_x >= corner5[0]) and (mouse_x <= corner5[0]+150) and (mouse_y >=corner5[1]) and (mouse_y <= corner5[1]+25):
                        global options_2
                        options_2 = True
                        start_game = False



game_start()                    



while options_2:
    screen.blit(background5, background4_rect)
    for option in second_menu:
        if option.rect.collidepoint(pygame.mouse.get_pos()):
            option.hovered = True
        else:
            option.hovered = False
        option.draw()
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_x, mouse_y = event.pos
                if (mouse_x >= corner1[0]) and (mouse_x <= corner1[0]+150) and (mouse_y >=corner1[1]) and (mouse_y <= corner1[1]+25):
                    current_volume = pygame.mixer.music.get_volume()
                    new_volume = current_volume + 0.1
                    pygame.mixer.music.set_volume(new_volume)

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_x, mouse_y = event.pos
                if (mouse_x >= corner3[0]) and (mouse_x <= corner3[0]+150) and (mouse_y >=corner3[1]) and (mouse_y <= corner3[1]+25):
                    current_volume1 = pygame.mixer.music.get_volume()
                    new_volume1 = current_volume1 - 0.2
                    pygame.mixer.music.set_volume(new_volume1)
                    #start_game = False
                    #running = True
                    #load_game = True

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_x, mouse_y = event.pos
                if (mouse_x >= corner5[0]) and (mouse_x <= corner5[0]+150) and (mouse_y >=corner5[1]) and (mouse_y <= corner5[1]+25):
                    os.startfile('credits.txt','edit')

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_x, mouse_y = event.pos
                if (mouse_x >= corner7[0]) and (mouse_x <= corner7[0]+150) and (mouse_y >=corner7[1]) and (mouse_y <= corner7[1]+25):
                    start_game = True
                    new_game = False
                    global options_2
                    options_2 = False
                    game_start()




        if event.type == pygame.QUIT:
                start_game = False
                options_2 = False
                options = False




# game loop
while new_game:

    # events
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_x, mouse_y = event.pos
                if (mouse_x >= corner2[0]) and (mouse_x <= corner2[0]+75) and (mouse_y >=corner2[1]) and (mouse_y <= corner2[1]+25):
                    start_game = True
                    new_game = False
                    options_2 = False
                    game_start()


        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_x, mouse_y = event.pos
                if (mouse_x >= corner4[0]) and (mouse_x <= corner4[0]+150) and (mouse_y >=corner4[1]) and (mouse_y <= corner4[1]+25):
                    screen.blit(planet3, planet3_rect)

再次感谢。

1 个答案:

答案 0 :(得分:0)

这是您的计划中发生的事情:

  1. game_start函数中的while循环正在运行。如果你点击&#34;新游戏&#34;或&#34;选项&#34;,while循环停止,并留下game_start功能。让我们点击&#34;选项&#34;第一

  2. 现在程序正在运行while options_2:循环。如果我们单击&#34;返回&#34;,则会在此while循环中调用game_start函数,因此while start_game:循环再次运行并单击&#34;选项&#34;将我们正确地返回while options_2循环。

  3. 让我们点击&#34;新游戏&#34;在开始场景中。现在跳过while options_2:循环,我们在while new_game:循环中。点击&#34;返回&#34;调用game_start并像往常一样运行其循环,但如果我们点击&#34;选项&#34;会发生什么?现在?我们在game_start()循环中调用while new_game,当game_start完成时,我们将返回到程序中的此位置。没有办法跳回上面的选项循环。

  4. 由于此时options_2new_game都是False,程序会离开最后一个while循环并完成。

  5. 您可以做的是,将options_2new_game循环包装在函数中。然后编写一个main(场景管理器)功能,在三个不同的场景功能之间切换。但是,这可以通过像simple state machine here这样的场景管理器类来完成。