From cb9b004ff8e185970b37bff4a52311dcea6ffddf Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Sat, 1 Oct 2022 15:52:53 -0300 Subject: [PATCH] Snake: Show message box on game over Previously the game would immediately reset on game over, but now it'll pause the game and show a message box with your score. --- Userland/Games/Snake/SnakeGame.cpp | 15 +++++++++++++++ Userland/Games/Snake/SnakeGame.h | 1 + 2 files changed, 16 insertions(+) diff --git a/Userland/Games/Snake/SnakeGame.cpp b/Userland/Games/Snake/SnakeGame.cpp index 63f187938c..48efd50beb 100644 --- a/Userland/Games/Snake/SnakeGame.cpp +++ b/Userland/Games/Snake/SnakeGame.cpp @@ -9,6 +9,7 @@ #include "SnakeGame.h" #include #include +#include #include #include #include @@ -34,6 +35,7 @@ void SnakeGame::reset() m_length = 2; m_score = 0; m_score_text = "Score: 0"; + m_is_new_high_score = false; m_velocity_queue.clear(); stop_timer(); start_timer(100); @@ -123,6 +125,7 @@ void SnakeGame::timer_event(Core::TimerEvent&) ++m_score; m_score_text = String::formatted("Score: {}", m_score); if (m_score > m_high_score) { + m_is_new_high_score = true; m_high_score = m_score; m_high_score_text = String::formatted("Best: {}", m_high_score); update(high_score_rect()); @@ -214,6 +217,18 @@ void SnakeGame::paint_event(GUI::PaintEvent& event) void SnakeGame::game_over() { + stop_timer(); + + StringBuilder text; + text.appendff("Your score was {}", m_score); + if (m_is_new_high_score) { + text.append("\nThat's a new high score!"sv); + } + GUI::MessageBox::show(window(), + text.to_string(), + "Game Over"sv, + GUI::MessageBox::Type::Information); + reset(); } diff --git a/Userland/Games/Snake/SnakeGame.h b/Userland/Games/Snake/SnakeGame.h index 063b6c8474..e29249346c 100644 --- a/Userland/Games/Snake/SnakeGame.h +++ b/Userland/Games/Snake/SnakeGame.h @@ -68,6 +68,7 @@ private: String m_score_text; unsigned m_high_score { 0 }; String m_high_score_text; + bool m_is_new_high_score { false }; NonnullRefPtrVector m_fruit_bitmaps; };