1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:37:35 +00:00

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.
This commit is contained in:
matcool 2022-10-01 15:52:53 -03:00 committed by Andreas Kling
parent 67300af248
commit cb9b004ff8
2 changed files with 16 additions and 0 deletions

View file

@ -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();
}

View file

@ -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;
};