Difference between revisions of "Game states"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "A simple way to get started with different states in the game is to use a simple state machine. Declare an enum with the different game states. <syntaxhighlight lang=csharp>...")
 
Line 25: Line 25:
 
     {
 
     {
 
     case GameState.MainMenu:
 
     case GameState.MainMenu:
         UpdateMainMenu(deltaTime);
+
         UpdateMainMenu(gameTime);
 
         break;
 
         break;
 
     case GameState.Gameplay:
 
     case GameState.Gameplay:
         UpdateGameplay(deltaTime);
+
         UpdateGameplay(gameTime);
 
         break;
 
         break;
 
     case GameState.EndOfGame:
 
     case GameState.EndOfGame:
         UpdateEndOfGame(deltaTime);
+
         UpdateEndOfGame(gameTime);
 
         break;
 
         break;
 
     }
 
     }
Line 40: Line 40:
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
void UpdateMainMenu(GameTime deltaTime)
+
void UpdateMainMenu(GameTime gameTime)
 
{
 
{
 
     // Respond to user input for menu selections, etc
 
     // Respond to user input for menu selections, etc
 
     if (pushedStartGameButton)
 
     if (pushedStartGameButton)
         _state = GameState.GamePlay;
+
         state = GameState.GamePlay;
 
}
 
}
  
void UpdateGameplay(GameTime deltaTime)
+
void UpdateGameplay(GameTime gameTime)
 
{
 
{
 
     // Respond to user actions in the game.
 
     // Respond to user actions in the game.
Line 53: Line 53:
 
     // Handle collisions
 
     // Handle collisions
 
     if (playerDied)
 
     if (playerDied)
         _state = GameState.EndOfGame;
+
         state = GameState.EndOfGame;
 
}
 
}
  
void UpdateEndOfGame(GameTime deltaTime)
+
void UpdateEndOfGame(GameTime gameTime)
 
{
 
{
 
     // Update scores
 
     // Update scores
Line 62: Line 62:
 
     // Respond to user input to restart level, or go back to main menu
 
     // Respond to user input to restart level, or go back to main menu
 
     if (pushedMainMenuButton)
 
     if (pushedMainMenuButton)
         _state = GameState.MainMenu;
+
         state = GameState.MainMenu;
 
     else if (pushedRestartLevelButton)
 
     else if (pushedRestartLevelButton)
 
     {
 
     {
 
         ResetLevel();
 
         ResetLevel();
         _state = GameState.Gameplay;
+
         state = GameState.Gameplay;
 
     }
 
     }
 
}
 
}
Line 74: Line 74:
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
void Draw(GameTime deltaTime)
+
void Draw(GameTime gameTime)
 
{
 
{
     base.Draw(deltaTime);
+
     base.Draw(gameTime);
     switch (_state)
+
     switch (state)
 
     {
 
     {
 
     case GameState.MainMenu:
 
     case GameState.MainMenu:
         DrawMainMenu(deltaTime);
+
         DrawMainMenu(gameTime);
 
         break;
 
         break;
 
     case GameState.Gameplay:
 
     case GameState.Gameplay:
         DrawGameplay(deltaTime);
+
         DrawGameplay(gameTime);
 
         break;
 
         break;
 
     case GameState.EndOfGame:
 
     case GameState.EndOfGame:
         DrawEndOfGame(deltaTime);
+
         DrawEndOfGame(gameTime);
 
         break;
 
         break;
 
     }
 
     }
Line 95: Line 95:
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
void DrawMainMenu(GameTime deltaTime)
+
void DrawMainMenu(GameTime gameTime)
 
{
 
{
 
     // Draw the main menu, any active selections, etc
 
     // Draw the main menu, any active selections, etc
 
}
 
}
  
void DrawGameplay(GameTime deltaTime)
+
void DrawGameplay(GameTime gameTime)
 
{
 
{
 
     // Draw the background the level
 
     // Draw the background the level
 
     // Draw enemies
 
     // Draw enemies
 
     // Draw the player
 
     // Draw the player
    // Draw particle effects, etc
 
 
}
 
}
  
void DrawEndOfGame(GameTime deltaTime)
+
void DrawEndOfGame(GameTime gameTime)
 
{
 
{
 
     // Draw text and scores
 
     // Draw text and scores

Revision as of 20:43, 29 September 2017

A simple way to get started with different states in the game is to use a simple state machine. Declare an enum with the different game states.

enum GameState
{
    MainMenu,
    Gameplay,
    EndOfGame,
}

In your Game class, declare a member of the GameState type.

GameState state;

In your Update() method, use the _state to determine which update to run.

void Update(GameTime gameTime)
{
    base.Update(gameTime);
    switch (state)
    {
    case GameState.MainMenu:
        UpdateMainMenu(gameTime);
        break;
    case GameState.Gameplay:
        UpdateGameplay(gameTime);
        break;
    case GameState.EndOfGame:
        UpdateEndOfGame(gameTime);
        break;
    }
}

Now define the UpdateMainmenu(), UpdateGameplay() and UpdateEndOfGame().

void UpdateMainMenu(GameTime gameTime)
{
    // Respond to user input for menu selections, etc
    if (pushedStartGameButton)
        state = GameState.GamePlay;
}

void UpdateGameplay(GameTime gameTime)
{
    // Respond to user actions in the game.
    // Update enemies
    // Handle collisions
    if (playerDied)
        state = GameState.EndOfGame;
}

void UpdateEndOfGame(GameTime gameTime)
{
    // Update scores
    // Do any animations, effects, etc for getting a high score
    // Respond to user input to restart level, or go back to main menu
    if (pushedMainMenuButton)
        state = GameState.MainMenu;
    else if (pushedRestartLevelButton)
    {
        ResetLevel();
        state = GameState.Gameplay;
    }
}

In the Game.Draw() method, again handle the different game states.

void Draw(GameTime gameTime)
{
    base.Draw(gameTime);
    switch (state)
    {
    case GameState.MainMenu:
        DrawMainMenu(gameTime);
        break;
    case GameState.Gameplay:
        DrawGameplay(gameTime);
        break;
    case GameState.EndOfGame:
        DrawEndOfGame(gameTime);
        break;
    }
}

Now define the different Draw methods.

void DrawMainMenu(GameTime gameTime)
{
    // Draw the main menu, any active selections, etc
}

void DrawGameplay(GameTime gameTime)
{
    // Draw the background the level
    // Draw enemies
    // Draw the player
}

void DrawEndOfGame(GameTime gameTime)
{
    // Draw text and scores
    // Draw menu for restarting level or going back to main menu
}