summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatcool <26722564+matcool@users.noreply.github.com>2022-10-01 15:52:53 -0300
committerAndreas Kling <kling@serenityos.org>2022-10-02 21:28:37 +0200
commitcb9b004ff8e185970b37bff4a52311dcea6ffddf (patch)
tree6c046b0707d57c9b9e4fa581682be71b540edbf0
parent67300af248167e2f0cef425a67fa21dee2f04a3f (diff)
downloadserenity-cb9b004ff8e185970b37bff4a52311dcea6ffddf.zip
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.
-rw-r--r--Userland/Games/Snake/SnakeGame.cpp15
-rw-r--r--Userland/Games/Snake/SnakeGame.h1
2 files changed, 16 insertions, 0 deletions
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 <AK/Random.h>
#include <LibConfig/Client.h>
+#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Font/Font.h>
@@ -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<Gfx::Bitmap> m_fruit_bitmaps;
};