使用 PyGame 切换到菜单的好方法 [英] A good way to switch to a menu using PyGame

查看:177
本文介绍了使用 PyGame 切换到菜单的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyGame 来创建一个游戏,并且无法找到一种在场景和菜单之间切换的干净方法.我不知道我是否需要为每个事物创建一个具有自己的更新和渲染循环的场景类,或者只使用 while 语句和函数(我不太喜欢它,因为它对我来说有点凌乱)它不会与我的代码无关,只是在菜单和主游戏之间切换的方式.

I am using PyGame to create a game, and can't figure a clean way to switch between scenes and menus. I don't know if I need to create a scene class for each thing with its own update and render loop or just use while statements and functions (which I don't like that much as it is a little messy for me) It doesn't matter about my code, just the way to switch between a menu and the main game.

推荐答案

有不同的策略,这取决于您想要什么功能.一种简单的方法是将每个场景放在一个函数中,该函数返回下一个场景应该是什么.然后创建一个简单的 if 语句,在主游戏循环中在它们之间切换.

There are different strategies and it depends on what functionality you want. One simple way is to have each scene in a function that returns what the next scene should be. Then create a simple if-statement that switches between them in the main game loop.

import pygame
pygame.init()

SCENE_MENU = 0
SCENE_GAME = 1
SCENE_SHOP = 2


def menu(screen):
    # Initialize game variables as the player, enemies and such.
    fps_cap = 30
    options = ['continue', 'save', 'quit']
    clock = pygame.time.Clock()

    # Game loop.
    while True:

        # Time management.
        clock.tick(fps_cap)

        # Handle events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:    # Go to game if you press A.
                    return SCENE_GAME
                elif event.key == pygame.K_b:  # Go to shop if you press B.
                    return SCENE_SHOP

        # Update the menu (like buttons, settings, ...).
        print('Updating buttons:', *options)

        # Draw the shop.
        screen.fill((0, 0, 255))  # A green menu.
        pygame.display.update()


def game(screen):
    # Initialize game variables as the player, enemies and such.
    fps_cap = 60
    player  = 'Ted'
    clock   = pygame.time.Clock()

    # Game loop.
    while True:

        # Time management.
        clock.tick(fps_cap)

        # Handle events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:    # Go to menu if you press A.
                    return SCENE_MENU
                elif event.key == pygame.K_b:  # Go to shop if you press B.
                    return SCENE_SHOP

        # Update the game.
        print(f'Player {player} is playing!')

        # Draw your game.
        screen.fill((0, 255, 0))  # A blue game.
        pygame.display.update()


def shop(screen):
    # Initialize game variables as the player, enemies and such.
    fps_cap = 30
    items = ['sword', 'armor', 'potion']
    clock = pygame.time.Clock()

    # Game loop.
    while True:

        # Time management.
        clock.tick(fps_cap)

        # Handle events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:    # Go to game if you press A.
                    return SCENE_GAME
                elif event.key == pygame.K_b:  # Go to shop if you press B.
                    return SCENE_SHOP

        # Update the shop (like buttons, money transfers, ...).
        print('Looking at items:', *items)

        # Draw the shop.
        screen.fill((255, 0, 0))  # A red shop.
        pygame.display.update()


def main():
    screen = pygame.display.set_mode((100, 100))
    scene = SCENE_MENU
    while True:
        if scene == SCENE_MENU:
            scene = menu(screen)
        elif scene == SCENE_SHOP:
            scene = shop(screen)
        elif scene == SCENE_GAME:
            scene = game(screen)

main()

这只是一个简单的例子,但应该说明原理.

This is just a simple example, but should show the principle.

这篇关于使用 PyGame 切换到菜单的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆