From 3e6c083754494e3eada05eb68e25bb5244c1155d Mon Sep 17 00:00:00 2001 From: Martin Frederic Date: Sat, 2 Apr 2022 19:50:31 +0200 Subject: [PATCH] Pong: Explicitly clear held keys in Game::reset() The paddle's movement is determined by the currently held keys. A key is no longer considered held when a matching keyup_event() fires. However, the event does not fire when the timer has stopped (e.g. due to a game over condition), which can result in the paddle keeping its former direction and moving on its own -- even after the player started a new game. Therefore, any held keys will be cleared explicitly. --- Userland/Games/Pong/Game.cpp | 7 +++++++ Userland/Games/Pong/Game.h | 1 + 2 files changed, 8 insertions(+) diff --git a/Userland/Games/Pong/Game.cpp b/Userland/Games/Pong/Game.cpp index d0a5ae8cb0..a5b52fd60b 100644 --- a/Userland/Games/Pong/Game.cpp +++ b/Userland/Games/Pong/Game.cpp @@ -15,6 +15,12 @@ Game::Game() reset(); } +void Game::reset_keys() +{ + m_up_key_held = false; + m_down_key_held = false; +} + void Game::reset_paddles() { if (m_cursor_paddle_target_y.has_value()) @@ -46,6 +52,7 @@ void Game::reset() reset_scores(); reset_ball(1); + reset_keys(); reset_paddles(); } diff --git a/Userland/Games/Pong/Game.h b/Userland/Games/Pong/Game.h index b8ebf7aa85..ab7973e63a 100644 --- a/Userland/Games/Pong/Game.h +++ b/Userland/Games/Pong/Game.h @@ -41,6 +41,7 @@ private: void reset_scores(); void reset_ball(int serve_to_player); + void reset_keys(); void reset_paddles(); void tick(); void round_over(int player);