From 0abdeb474fc97b1fe46282e2cad80642a0817f46 Mon Sep 17 00:00:00 2001 From: Martin Frederic Date: Sat, 2 Apr 2022 17:28:31 +0200 Subject: [PATCH] Pong: Extend Game::reset() Previously, the method reset the ball and the paddles. Now, it will also reset and redraw the scores and update the ball's rect. --- Userland/Games/Pong/Game.cpp | 14 ++++++++++++++ Userland/Games/Pong/Game.h | 1 + 2 files changed, 15 insertions(+) diff --git a/Userland/Games/Pong/Game.cpp b/Userland/Games/Pong/Game.cpp index d020a16c37c..e421f53ff9a 100644 --- a/Userland/Games/Pong/Game.cpp +++ b/Userland/Games/Pong/Game.cpp @@ -36,6 +36,10 @@ void Game::reset_paddles() void Game::reset() { + // Make sure the current ball disappears. + update(enclosing_int_rect(m_ball.rect())); + + reset_scores(); reset_ball(1); reset_paddles(); } @@ -130,6 +134,16 @@ void Game::track_mouse_move(Gfx::IntPoint const& point) update(cursor_paddle_target_rect()); } +void Game::reset_scores() +{ + // Clearing the scores first would lead to overly narrow rects for multi-digit scores. + update(player_1_score_rect()); + update(player_2_score_rect()); + + m_player_1_score = 0; + m_player_2_score = 0; +} + void Game::reset_ball(int serve_to_player) { int position_y_min = (game_width / 2) - 50; diff --git a/Userland/Games/Pong/Game.h b/Userland/Games/Pong/Game.h index ebde7607244..801b7f5665a 100644 --- a/Userland/Games/Pong/Game.h +++ b/Userland/Games/Pong/Game.h @@ -39,6 +39,7 @@ private: virtual void timer_event(Core::TimerEvent&) override; virtual void track_mouse_move(Gfx::IntPoint const&) override; + void reset_scores(); void reset_ball(int serve_to_player); void reset_paddles(); void tick();