Game State Management
The game state is managed using a combination of classes and methods, primarily within the Game.cs
file. The game state includes information such as player positions, game progress, and levels.
State Representation
The core of the game state is represented by the GameState
class. This class holds information about the game’s current state:
- Game Level: The current level the player is on.
- Player: Stores the player’s current position and status.
- Score: The player’s accumulated score.
- Lives: The number of lives the player has remaining.
- Ghosts: Information about each ghost’s position and state (e.g., chasing, scared).
- Game Over: A flag indicating if the game has ended.
- Game Won: A flag indicating if the player has won the game.
Data Structures
GameState
Class: This class, defined inGame.cs
, contains the central data structure for representing the game state.
// Game.cs
public class GameState
{
public int Level { get; set; }
public Player Player { get; set; }
public int Score { get; set; }
public int Lives { get; set; }
public List<Ghost> Ghosts { get; set; }
public bool GameOver { get; set; }
public bool GameWon { get; set; }
}
State Updates
Game state updates occur in various parts of the code, mainly triggered by user input and game logic:
- Player Movement: The
Player
class handles player movement, updating thePlayer.Position
property within theGameState
instance. - Ghost Movement: The
Ghost
class handles ghost movement, updating their positions within theGameState.Ghosts
list. - Pellet Consumption: When the player consumes a pellet, the
Score
property is incremented in theGameState
instance. - Power Pellet Consumption: When the player consumes a power pellet, the
Ghosts
in theGameState
instance are set to a “scared” state. - Death: If the player collides with a ghost, a life is deducted from the
Lives
property in theGameState
instance. - Level Completion: When all pellets are consumed, the
Level
property is incremented in theGameState
instance.
State Synchronization
- Blazor Component Updates: The
Game.cs
class includes methods likeUpdateState()
that are used to notify the Blazor components about state changes. These methods trigger re-rendering of components to reflect the updated game state.
// Game.cs
public void UpdateState()
{
// Notify Blazor components of state changes
StateHasChanged?.Invoke();
}
- State Persistence: The current implementation of the game does not persist game state between sessions. If you want to add this feature, consider using browser storage or server-side data persistence mechanisms.