How are multiple page views created in a game?

て烟熏妆下的殇ゞ 提交于 2021-01-27 14:31:39

问题


Many game examples/tutorials focus only on the rendering of the main game and don't show the rest of the game, like the first landing page, splash screen, high scores page, credits page and so on.

For something like DirectX and XNA, how are these other screens created/rendered?


回答1:


Alternatively to the Microsoft sample mentioned by Davidsbro, if you wanted to create your own basic game state management system you could do so through the use of an enum switch. For this approach you would declare an enum type and set it's default value, in this case the default value could be the main menu of a game. In XNA this would look something like the following:

enum GameScene { menu, game };
GameScene scene = GameScene.menu;

Inside of the games update method you would then implement a switch statement which would be responsible for handling both enum states:

protected override void Update(GameTime gameTime)
{
    switch (scene)
    {
            case GameScene.menu:
            {
                // Perform menu scene logic
                break;
            }
            case GameScene.game:
            {
                // Perform game scene logic
                break;
            }
    }

    base.Update(gameTime);
}

You would then need to implement another switch within the draw method similarly to the enum switch above.

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    switch (scene)
    {
        case GameScene.menu:
            {
                // Draw menu scene
                break;
            }
        case GameScene.game:
            {
                // Draw game scene
                break;
            }
    }

    base.Draw(gameTime);
}

Additionally you would need to implement some form of logic to transition between the different game states. This could be achieved through user input or some form of game timer.



来源:https://stackoverflow.com/questions/16825623/how-are-multiple-page-views-created-in-a-game

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!