diff options
author | Martin Frederic <juni@fat.fm> | 2022-04-02 19:50:31 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-15 00:13:09 +0200 |
commit | 3e6c083754494e3eada05eb68e25bb5244c1155d (patch) | |
tree | c9f9fbe1e1e8fa59cee7b552055eeeaa022e8c77 | |
parent | 02d2a300e73411c8fdf6b5f3cf817b3ed6fb22e3 (diff) | |
download | serenity-3e6c083754494e3eada05eb68e25bb5244c1155d.zip |
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.
-rw-r--r-- | Userland/Games/Pong/Game.cpp | 7 | ||||
-rw-r--r-- | Userland/Games/Pong/Game.h | 1 |
2 files changed, 8 insertions, 0 deletions
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); |