Pong: Restart timer if necessary

When the player runs into a game over condition, Game's timer is
stopped. In order for reset() to work properly, the timer has to be
started again. The condition is tracked via a new member variable,
`m_game_over`. To prevent confusion, game_over() has been renamed to
show_game_over_message().
This commit is contained in:
Martin Frederic 2022-04-02 19:16:02 +02:00 committed by Andreas Kling
parent 0abdeb474f
commit 02d2a300e7
Notes: sideshowbarker 2024-07-19 17:06:20 +09:00
2 changed files with 11 additions and 3 deletions

View file

@ -36,6 +36,11 @@ void Game::reset_paddles()
void Game::reset()
{
if (m_game_over) {
m_game_over = false;
start_timer(16);
}
// Make sure the current ball disappears.
update(enclosing_int_rect(m_ball.rect()));
@ -162,7 +167,7 @@ void Game::reset_ball(int serve_to_player)
m_ball.velocity = { velocity_x, velocity_y };
}
void Game::game_over(int winner)
void Game::show_game_over_message(int winner)
{
GUI::MessageBox::show(window(), String::formatted("Player {} wins!", winner), "Pong", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OK);
}
@ -183,7 +188,8 @@ void Game::round_over(int winner)
}
if (m_player_1_score == m_score_to_win || m_player_2_score == m_score_to_win) {
game_over(winner);
m_game_over = true;
show_game_over_message(winner);
return;
}

View file

@ -44,7 +44,7 @@ private:
void reset_paddles();
void tick();
void round_over(int player);
void game_over(int player);
void show_game_over_message(int player);
void calculate_move();
struct Ball {
@ -113,6 +113,8 @@ private:
Optional<int> m_cursor_paddle_target_y;
bool m_up_key_held = false;
bool m_down_key_held = false;
bool m_game_over = false;
};
}