summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Frederic <juni@fat.fm>2022-04-02 19:16:02 +0200
committerAndreas Kling <kling@serenityos.org>2022-04-15 00:13:09 +0200
commit02d2a300e73411c8fdf6b5f3cf817b3ed6fb22e3 (patch)
tree813bcdb8a21a2d35c4d54fb312561406796a0457
parent0abdeb474fc97b1fe46282e2cad80642a0817f46 (diff)
downloadserenity-02d2a300e73411c8fdf6b5f3cf817b3ed6fb22e3.zip
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().
-rw-r--r--Userland/Games/Pong/Game.cpp10
-rw-r--r--Userland/Games/Pong/Game.h4
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<int> m_cursor_paddle_target_y;
bool m_up_key_held = false;
bool m_down_key_held = false;
+
+ bool m_game_over = false;
};
}