From 02d2a300e73411c8fdf6b5f3cf817b3ed6fb22e3 Mon Sep 17 00:00:00 2001 From: Martin Frederic Date: Sat, 2 Apr 2022 19:16:02 +0200 Subject: 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(). --- Userland/Games/Pong/Game.cpp | 10 ++++++++-- Userland/Games/Pong/Game.h | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Userland/Games/Pong/Game.cpp b/Userland/Games/Pong/Game.cpp index e421f53ff9..d0a5ae8cb0 100644 --- a/Userland/Games/Pong/Game.cpp +++ b/Userland/Games/Pong/Game.cpp @@ -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; } diff --git a/Userland/Games/Pong/Game.h b/Userland/Games/Pong/Game.h index 801b7f5665..b8ebf7aa85 100644 --- a/Userland/Games/Pong/Game.h +++ b/Userland/Games/Pong/Game.h @@ -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 m_cursor_paddle_target_y; bool m_up_key_held = false; bool m_down_key_held = false; + + bool m_game_over = false; }; } -- cgit v1.2.3