From 88cc019275bed9ed4dabd302fb516380a1e333ad Mon Sep 17 00:00:00 2001 From: 0GreenClover0 Date: Wed, 1 Nov 2023 03:09:21 +0100 Subject: [PATCH] FlappyBug: Unify the way of getting the final score Previously we would display the score rounded to the nearest integer, but save the high score by using a static_cast, which would always round the score down. This could lead to the final score being higher than the new high score, when they should be equal. Now we always round the score to the nearest integer. --- Userland/Games/FlappyBug/Game.cpp | 9 +++++++-- Userland/Games/FlappyBug/Game.h | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Userland/Games/FlappyBug/Game.cpp b/Userland/Games/FlappyBug/Game.cpp index c911c32767..7029d06877 100644 --- a/Userland/Games/FlappyBug/Game.cpp +++ b/Userland/Games/FlappyBug/Game.cpp @@ -30,7 +30,7 @@ void Game::reset() void Game::game_over() { if (on_game_end) - m_high_score = on_game_end(static_cast(m_difficulty)); + m_high_score = on_game_end(get_final_score(m_difficulty)); reset(); } @@ -73,7 +73,7 @@ void Game::paint_event(GUI::PaintEvent& event) if (m_active) { painter.draw_text(m_score_rect, String::formatted("{:.0}", m_difficulty).release_value_but_fixme_should_propagate_errors(), Gfx::TextAlignment::TopLeft, Color::White); } else if (m_high_score.has_value()) { - auto message = String::formatted("Your score: {:.0}\nHigh score: {:.0}\n\n{}", m_last_score, m_high_score.value(), m_restart_cooldown < 0 ? "Press any key to play again" : " ").release_value_but_fixme_should_propagate_errors(); + auto message = String::formatted("Your score: {}\nHigh score: {}\n\n{}", get_final_score(m_last_score), m_high_score.value(), m_restart_cooldown < 0 ? "Press any key to play again" : " ").release_value_but_fixme_should_propagate_errors(); painter.draw_text(m_text_rect, message, Gfx::TextAlignment::Center, Color::White); } else { painter.draw_text(m_text_rect, "Press any key to start"sv, Gfx::TextAlignment::Center, Color::White); @@ -150,4 +150,9 @@ void Game::tick() queue_update(); } +u32 Game::get_final_score(float score) +{ + return static_cast(roundf(score)); +} + } diff --git a/Userland/Games/FlappyBug/Game.h b/Userland/Games/FlappyBug/Game.h index 897cd23efb..88e3ac64b6 100644 --- a/Userland/Games/FlappyBug/Game.h +++ b/Userland/Games/FlappyBug/Game.h @@ -39,6 +39,8 @@ private: bool ready_to_start() const; void player_input(); + static u32 get_final_score(float score); + public: struct Bug { float const x { 50 }; @@ -169,7 +171,7 @@ private: Obstacle m_obstacle; Cloud m_cloud; bool m_active; - Optional m_high_score {}; + Optional m_high_score {}; float m_last_score {}; float m_difficulty {}; float m_restart_cooldown {};