Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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制作的游戏中实现菜单_Python_Pygame - Fatal编程技术网

Python 在pygame制作的游戏中实现菜单

Python 在pygame制作的游戏中实现菜单,python,pygame,Python,Pygame,我一直在为我的游戏制作菜单。它在我第一次打开游戏时起作用,当玩家死亡时,它会按预期进入菜单()。但是,当我在玩家死后按“开始游戏”一次时,它会执行main()不到一秒钟,然后返回menu()。我是这样做菜单的 def menu(): controls = pygame.image.load(r"C:\Users\rahul\OneDrive\Documents\A level python codes\final game\controls.png").convert() con

我一直在为我的游戏制作菜单。它在我第一次打开游戏时起作用,当玩家死亡时,它会按预期进入
菜单()。但是,当我在玩家死后按“开始游戏”一次时,它会执行
main()
不到一秒钟,然后返回
menu()
。我是这样做菜单的

def menu():
    controls = pygame.image.load(r"C:\Users\rahul\OneDrive\Documents\A level python codes\final game\controls.png").convert()
    controls.set_colorkey((0, 0, 0))
    controls = pygame.transform.scale(controls, (1200, 600))
    back_rect = pygame.Rect(800, 550, 400, 50)
    back_rect_border = pygame.Rect(800, 550, 400, 50)
    start_rect = pygame.Rect( 400, 200, 400, 100)
    start_rect_border = pygame.Rect( 400, 200, 400, 100)
    controls_rect = pygame.Rect(400, 310, 400, 100)
    controls_rect_border = pygame.Rect(400, 310, 400, 100)
    x = 1200
    loop = True
    display_controls = False
    while loop:
        D.fill((255,173,96))
        events = pygame.event.get()
        mouse_pos = pygame.mouse.get_pos()

        for event in events:
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        if not display_controls:
            if start_rect.collidepoint(mouse_pos):
                start_color = (200, 150, 0)
                for event in events:
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        loop = False
                        run_menu = False


            else:
                start_color = (100, 200, 150)

            if controls_rect.collidepoint(mouse_pos):
                control_color = (200, 150, 0)
                for event in events:
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        display_controls = True

            else:
                control_color = (100, 100, 150)

            pygame.draw.rect(D, start_color, (start_rect))
            pygame.draw.rect(D, control_color, (controls_rect))
            pygame.draw.rect(D, (0, 0, 0), (start_rect_border), 3)
            pygame.draw.rect(D, (0, 0, 0), (controls_rect_border), 3)
            write(50, "START  GAME", (255, 255, 255), 404, 220)
            write(50, "CONTROLS", (255, 255, 255), 404, 330)



        if display_controls:
            D.blit(controls, (x, 0))
            x -= 10
            if x<= 0:
                x = 0

            if back_rect.collidepoint(mouse_pos):
                back_color = (200, 180, 182)
                for event in events:
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        display_controls = False
                        x = 1200

            else:
                back_color = (254, 219, 183)

            pygame.draw.rect(D, back_color, (back_rect))
            pygame.draw.rect(D, (0, 0, 0), (back_rect_border), 3)
            write(25, "BACK TO MAIN MENU", (255, 255, 255), 802, 555)



        win.flip()
main()
只是一组函数调用:

def main():

        def handle_events(events):
            for event in events:
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

                if event.type ==  pygame.MOUSEBUTTONDOWN:
                    game.bullets.append(Bullet())

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                            if not  game.fireballs:
                                    game.fireballs.append(Fire_ball())

        while True:

             events = pygame.event.get()

             handle_events(events)

             D.fill((254, 219, 183))

             game_map.draw()

             player.draw()

             player.move()

             player.camera()

             player.boundries()

             player.jetpack()

             game.fall_player()

             player.draw_jet_bar()

             player.draw_health_bar()

             player.dead()

             game.shoot_bullet()

             game.shoot_fireball()

             game.destroy_bullet()

             game.spawn_enemy()

             game.move_enemy()

             check_fireball_hit()

             game.destroy_fireballs()

             check_bullet_hit()

             print_stats()

             mouse.update()

             scroll[0] += 1

             win.flip()
main()
中的
player.dead()
函数应该在播放器死亡后触发菜单(这是不工作的部分),它看起来如下所示:

 def dead(self):
        if self.hp  <= 0:
            menu()
def死(自):

如果self.hp对于这类事情,一种常见的方法是一组状态和(可能)转换。通常,您会将这些绘制为图表:

诚然,这是一组相当简单的状态。但也许你也会有游戏中的状态,比如(也许)“加载”、“1级”、“2级”等等

因此当前状态保存在一个变量中,主循环使用该值来决定要做什么。如果程序处于“菜单”状态,则会绘制菜单,箭头键会更改高亮显示的选项。而如果游戏处于“玩”状态,则使用箭头键移动玩家角色等

menu_state = 1
current_state = "menu"
while current_state != "exit":

    # paint the screen
    if ( current_state == "menu" ):
        drawMenu()
    elif ( current_state == "game" ):
        drawGame()

    # Handle Events
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            current_state = "exit"
        elif ( event.type ==  pygame.MOUSEBUTTONDOWN ):
            mouse_pos = pygame.mouse.get_pos()
            if ( current_state == "menu" ):
                current_state = handleMenuClick( mouse_pos )
            elif ( current_state == "game" ):
                game.bullets.append( Bullet() )

    # Handle continuous-key-presses, but only in menu mode
    if ( current_state == "menu" ):
        keys = pygame.key.get_pressed()
        if ( keys[pygame.K_UP] ):
            menuSelectPrevious()
        elif ( keys[pygame.K_DOWN] ):
            menuSelectNext()
        elif ( keys[pygame.K_ENTER] ):
            current_state = menuDoOption()

    print_stats()
    win.flip()

这提供了一种简单的方法,允许函数始终“知道”游戏的状态,以及允许从当前状态更改为哪些状态。这有助于理清所有可能的选项,例如处理事件。这也意味着您只需要一个事件循环,这大大简化了程序流程。

您能否添加更多有关如何不工作的详细信息?它在玩家第一次死亡时起作用,而不是在第二次死亡时起作用吗?当你期待菜单出现,或者程序停止运行或挂起时,会发生什么事情吗?我在startAhh I thinnk做了一些更改,我发现了,这是因为下次我按下开始游戏时,玩家的仍然是0或小于0,这迫使菜单执行。现在我的问题是,一旦玩家死亡,我如何重置游戏中的所有属性。有什么方法可以重新开始执行main吗?我要做的是编写一些新函数,如
player.reset()
map.reset()
,这些函数可以将角色恢复到完全健康状态,将角色移动到开始位置,重新填充地图资源等。。。从长远来看,这比试图将所有变量都保持在
main()
的范围内要容易得多(这将允许您为新游戏重新运行
main()
,但随着游戏的发展,很难管理)。
menu_state = 1
current_state = "menu"
while current_state != "exit":

    # paint the screen
    if ( current_state == "menu" ):
        drawMenu()
    elif ( current_state == "game" ):
        drawGame()

    # Handle Events
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            current_state = "exit"
        elif ( event.type ==  pygame.MOUSEBUTTONDOWN ):
            mouse_pos = pygame.mouse.get_pos()
            if ( current_state == "menu" ):
                current_state = handleMenuClick( mouse_pos )
            elif ( current_state == "game" ):
                game.bullets.append( Bullet() )

    # Handle continuous-key-presses, but only in menu mode
    if ( current_state == "menu" ):
        keys = pygame.key.get_pressed()
        if ( keys[pygame.K_UP] ):
            menuSelectPrevious()
        elif ( keys[pygame.K_DOWN] ):
            menuSelectNext()
        elif ( keys[pygame.K_ENTER] ):
            current_state = menuDoOption()

    print_stats()
    win.flip()