python - 在用 pygame 制作的游戏中实现菜单

标签 python pygame

我一直在尝试为我的游戏制作菜单。它在我第一次打开游戏时起作用,当玩家死亡时,它会进入 menu()正如预期的那样。但是当我在玩家死一次后按“开始游戏”时,它会执行 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()

现在在我的if __name__ == "__main__":我运行了菜单和主要功能,如下所示

if __name__ == "__main__":
    pygame.init()
    menu()
    main()

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()

player.dead()函数位于 main()应该在玩家死亡后触发菜单(这是不起作用的部分),它看起来像这样:

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

这是我第一次为游戏创建菜单,所以我不确定最好的方法是什么。一旦玩家死亡,它就不起作用,这意味着问题出在 player.dead() 中。触发菜单的函数。任何帮助,将不胜感激。谢谢

编辑: 我认为这是因为下次我按下开始游戏时玩家的值仍然是 0 或小于 0,这会强制执行菜单。现在我的问题是,一旦玩家死亡,我如何重置游戏中的所有统计数据。一旦玩家死亡,有什么方法可以重新开始执行 main 函数吗?

最佳答案

对于这类事情,常见的方法是一组状态和(可能)转换。您经常会看到这些绘制成图表:

state-transition-diagram

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

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

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()

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

关于python - 在用 pygame 制作的游戏中实现菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60045900/

相关文章:

pygame - 对我的 Sprite 不等的 +/- 速度感到困惑

Python Setuptools 和 PBR - 如何使用 git 标签作为版本创建包版本?

python - 错误类型 :can't pickle _thread. _local 对象

python - Pygame 由于重复循环而滞后,但不知道任何替代解决方案

python - 如何在 mac OSX 10.9.4 上卸载 pygame

python - pygame.time.wait() 使窗口卡住

python - python中的屏幕共享

python - 如何在 python 3 中每隔一行写一次?

Python3 : module 'tabula' has no attribute 'read_pdf'

python - Celery Task 是按每个工作进程初始化还是每个应用程序初始化一次?