Difference between revisions of "Game states"
(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>...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
<syntaxhighlight lang=csharp> | <syntaxhighlight lang=csharp> | ||
− | GameState state; | + | GameState state = GameState.MainMenu; |
</syntaxhighlight> | </syntaxhighlight> | ||
Line 25: | Line 25: | ||
{ | { | ||
case GameState.MainMenu: | case GameState.MainMenu: | ||
− | UpdateMainMenu( | + | UpdateMainMenu(gameTime); |
break; | break; | ||
case GameState.Gameplay: | case GameState.Gameplay: | ||
− | UpdateGameplay( | + | UpdateGameplay(gameTime); |
break; | break; | ||
case GameState.EndOfGame: | case GameState.EndOfGame: | ||
− | UpdateEndOfGame( | + | UpdateEndOfGame(gameTime); |
break; | break; | ||
} | } | ||
Line 40: | Line 40: | ||
<syntaxhighlight lang=csharp> | <syntaxhighlight lang=csharp> | ||
− | void UpdateMainMenu(GameTime | + | 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; | |
} | } | ||
− | void UpdateGameplay(GameTime | + | 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; | |
} | } | ||
− | void UpdateEndOfGame(GameTime | + | 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; | |
else if (pushedRestartLevelButton) | else if (pushedRestartLevelButton) | ||
{ | { | ||
ResetLevel(); | ResetLevel(); | ||
− | + | state = GameState.Gameplay; | |
} | } | ||
} | } | ||
Line 74: | Line 74: | ||
<syntaxhighlight lang=csharp> | <syntaxhighlight lang=csharp> | ||
− | void Draw(GameTime | + | void Draw(GameTime gameTime) |
{ | { | ||
− | base.Draw( | + | base.Draw(gameTime); |
− | switch ( | + | switch (state) |
{ | { | ||
case GameState.MainMenu: | case GameState.MainMenu: | ||
− | DrawMainMenu( | + | DrawMainMenu(gameTime); |
break; | break; | ||
case GameState.Gameplay: | case GameState.Gameplay: | ||
− | DrawGameplay( | + | DrawGameplay(gameTime); |
break; | break; | ||
case GameState.EndOfGame: | case GameState.EndOfGame: | ||
− | DrawEndOfGame( | + | DrawEndOfGame(gameTime); |
break; | break; | ||
} | } | ||
Line 95: | Line 95: | ||
<syntaxhighlight lang=csharp> | <syntaxhighlight lang=csharp> | ||
− | void DrawMainMenu(GameTime | + | void DrawMainMenu(GameTime gameTime) |
{ | { | ||
// Draw the main menu, any active selections, etc | // Draw the main menu, any active selections, etc | ||
} | } | ||
− | void DrawGameplay(GameTime | + | void DrawGameplay(GameTime gameTime) |
{ | { | ||
// Draw the background the level | // Draw the background the level | ||
// Draw enemies | // Draw enemies | ||
// Draw the player | // Draw the player | ||
− | |||
} | } | ||
− | void DrawEndOfGame(GameTime | + | void DrawEndOfGame(GameTime gameTime) |
{ | { | ||
// Draw text and scores | // Draw text and scores | ||
// Draw menu for restarting level or going back to main menu | // Draw menu for restarting level or going back to main menu | ||
} | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==OnChange event on enum== | ||
+ | You could use a private variable, but have a public property. Within the set of the property you could run a method which will be run every time use change the enum value with the property: | ||
+ | |||
+ | <syntaxhighlight lang=csharp> | ||
+ | public enum PersonName | ||
+ | { | ||
+ | Eric, | ||
+ | George, | ||
+ | David, | ||
+ | Frank | ||
+ | } | ||
+ | |||
+ | private PersonName myPersonName | ||
+ | |||
+ | public PersonName MyPersonName | ||
+ | { | ||
+ | get { return myPersonName; } | ||
+ | set | ||
+ | { | ||
+ | myPersonName = value; | ||
+ | //simply call what you want done | ||
+ | PersonNamePropertyChanged(); | ||
+ | } | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 07:41, 12 October 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 = GameState.MainMenu;
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
}
OnChange event on enum
You could use a private variable, but have a public property. Within the set of the property you could run a method which will be run every time use change the enum value with the property:
public enum PersonName
{
Eric,
George,
David,
Frank
}
private PersonName myPersonName
public PersonName MyPersonName
{
get { return myPersonName; }
set
{
myPersonName = value;
//simply call what you want done
PersonNamePropertyChanged();
}
}