Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame程序在切换场景后退出(几个while循环)_Python_Pygame - Fatal编程技术网

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

Python Pygame程序在切换场景后退出(几个while循环),python,pygame,Python,Pygame,我的Pygame游戏有问题。当导航到新游戏屏幕,然后返回并单击“选项”按钮时,“简单游戏”将关闭,并且不会向我显示错误或任何出错迹象,但如果我没有首先进入新游戏屏幕而单击它,则会起作用。。请告诉我我错过了什么 代码如下: global new_game global menu global second_menu global options_2 global galaxies # start screen loop &variable start_game = True new_ga

我的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)
全球新游戏
全局菜单
全局第二菜单
全球选择2
全球星系
#启动屏幕循环和变量
开始游戏=正确
新游戏=错误
选项2=错误
def game_start():
打印(“你好”)
开始游戏=正确
选项2=真
开始游戏时:
pygame.event.pump()
屏幕。blit(背景,背景)
对于菜单中的选项:
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered=True
其他:
option.hovered=False
option.draw()
pygame.display.update()
对于pygame.event.get()中的事件:
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==1:
鼠标x,鼠标y=event.pos

如果(mouse_x>=corner1[0])和(mouse_x=corner1[1])和(mouse_x=corner3[0])和(mouse_x=corner5[0])和(mouse_x=corner5[1])和(mouse_x=corner1[0])和(mouse_x=corner3[0])和(mouse_x=corner3[1])和(mouse_x=corner5[0])和(mouse_x=corner5[0])和(mouse_x=corner1[7])(mouse_x=corner7[1])和(mouse_y=corner2[0])和(mouse_x=corner2[1])和(mouse_y=corner4[0])和(mouse_x=corner4[1])以及(mouse_y以下是程序中发生的事情:

  • game\u start
    功能中的while循环正在运行。如果单击“新建游戏”或“选项”,while循环将停止,而
    game\u start
    功能将保留。让我们先单击“选项”

  • 现在程序正在运行
    while options\u 2:
    循环。如果我们单击“后退”,将在此while循环中调用
    game\u start
    函数,因此
    while start\u game:
    循环将再次运行,单击“选项”将使我们正确返回
    while options\u 2
    循环

  • 让我们在开始场景中单击“新建游戏”。现在跳过
    while options\u 2:
    循环,我们进入
    while New\u Game:
    循环。单击“上一步”调用
    Game\u start
    并像往常一样运行其循环,但是如果我们现在单击“options”会发生什么?我们调用了
    Game\u start()
    while new\u game
    循环中开始,我们在
    game\u start
    完成后返回程序中的此位置。无法跳回上面的选项循环

  • 由于此时
    options_2
    new_game
    都是
    False
    ,程序将离开最后一个while循环并完成



  • 您可以做的是,将您的
    选项\u 2
    新游戏
    也包装在函数中。然后编写一个
    (场景管理器)函数,在三个不同的场景函数之间进行切换。但是,这可以通过像这样的场景管理器类更干净地完成。

    您能生成一个更简单(更短)的吗您的代码版本显示了错误。请考虑修改您在这个问题中发布的代码示例。正如它目前所说的,它的格式和范围使我们难以帮助您;这里有一个让您开始这样做的地方。- 1,不要误会。下面的投票是我们如何指示这里的内容问题;改进您的格式。ing和代码样本和我(或有人会)很高兴将其还原。祝您的代码好运!我已将代码稍微缩减了一些,很抱歉不知道我在做什么,我不太确定剩余代码中的问题在哪里。感谢您的快速回复。您只是在运行该文件,而不是通过空闲或其他IDE吗?我询问的原因是,如果是第一个,您将无法看到抛出y个错误。在空闲状态下运行会显示错误。@SH7890是的,我正在空闲状态下运行它。我认为它没有达到循环,默认退出程序。非常感谢,我会在下班后查看并修复我的程序,并发布结果。谢谢你,它工作正常。代码还远没有完成开着,但马上就要来了。