As the saying goes, all good things must come to an end. If the player doesn't meet the pickup threshold, the game will end, and the game over screen will be displayed. The player can choose to replay the game or exit.
Our last screen is the game over screen. By now, the process should be second nature, so let's try an abbreviated approach:
Sprite* gameOverScreen;
LoadTextures
:gameOverScreen = new Sprite(1); gameOverScreen->SetFrameSize(800.0f, 600.0f); gameOverScreen->SetNumberOfFrames(1); gameOverScreen->AddTexture("resources/gameover.png", false); gameOverScreen->IsActive(true); gameOverScreen->IsVisible(true);
GS_GameOver
case in the Update
function to look like the following code:case GameState::GS_GameOver: { gameOverScreen->Update(p_deltaTime); replayButton->IsActive(true); replayButton->Update(p_deltaTime); exitButton->IsActive(true); exitButton->Update(p_deltaTime); inputManager->Update(p_deltaTime); ProcessInput(p_deltaTime); } break;
Render
:case GameState::GS_GameOver: { gameOverScreen->Render(); replayButton->Render(); DrawStats(); } break;
As a bonus, we will also draw the game stats on the game over screen.
We need a way to reset the game to its initial state. So, let's create a function to do this:
void RestartGame() { player->SetValue(0); robot_right->SetValue(0); robot_left->SetValue(0); pickupSpawnThreshold = 5.0f; pickupSpawnTimer = 0.0f; enemySpawnThreshold = 7.0f; enemySpawnTimer = 0.0f; splashDisplayTimer = 0.0f; splashDisplayThreshold = 5.0f; levelTimer = 0.0f; pickupsReceived = 0; pickupsThreshold = 5; pickupsReceived = 0; pickup->IsVisible(false); enemy->IsVisible(false); background->SetVelocity(0.0f); robot_left->SetPosition(screen_width / 2.0f - 50.0f, screen_height - 130.0f); robot_left->IsVisible(false); robot_right->SetPosition(screen_width / 2.0f - 50.0f, screen_height - 130.0f); player = robot_right; player->IsActive(true); player->IsVisible(true); player->SetVelocity(0.0f); }
Next, we need to add a button that allows the player to replay the game. Again, as you have done this so many times, we will use a shorthand approach:
Sprite* replayButton;
LoadTextures
:replayButton = new Sprite(1); replayButton->SetFrameSize(75.0f, 38.0f); replayButton->SetNumberOfFrames(1); replayButton->SetPosition(390.0f, 400.0f); replayButton->AddTexture("resources/replayButton.png"); replayButton->IsVisible(true); replayButton->IsActive(false); inputManager->AddUiElement(replayButton);
Update
:case GameState::GS_GameOver: { gameOverScreen->Update(p_deltaTime); replayButton->IsActive(true); replayButton->Update(p_deltaTime); exitButton->IsActive(true); exitButton->Update(p_deltaTime); inputManager->Update(p_deltaTime); ProcessInput(p_deltaTime); } break;
Render
:case GameState::GS_GameOver: { gameOverScreen->Render(); replayButton->Render(); DrawStats(); } break;
ProcessInput
:if (replayButton->IsClicked()) { replayButton->IsClicked(false); replayButton->IsActive(false); exitButton->IsActive(false); RestartGame(); m_gameState = GameState::GS_Running; }
Notice how we are reusing the exit button in the Update
function. Also, if the player wants to replay the game, we call the RestartGame
function when the player clicks the replay button. This resets all of the game variables and allows the player to start all over.
3.145.76.250