diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-20 21:24:38 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-20 21:24:38 +0200 |
commit | a56e1afb6474a3dab2774b83766e2e372a7479c0 (patch) | |
tree | ecdd3da930c5a653c90fba615f393355c6dce757 /Games/Snake | |
parent | 0dcb7a234ee71c91d4d469eb7fce43f7fc68c37b (diff) | |
download | serenity-a56e1afb6474a3dab2774b83766e2e372a7479c0.zip |
Snake: Show the highest score achieved so far (in this session.)
Diffstat (limited to 'Games/Snake')
-rw-r--r-- | Games/Snake/SnakeGame.cpp | 18 | ||||
-rw-r--r-- | Games/Snake/SnakeGame.h | 3 |
2 files changed, 20 insertions, 1 deletions
diff --git a/Games/Snake/SnakeGame.cpp b/Games/Snake/SnakeGame.cpp index cfa3511c17..9c8439c423 100644 --- a/Games/Snake/SnakeGame.cpp +++ b/Games/Snake/SnakeGame.cpp @@ -1,5 +1,6 @@ #include "SnakeGame.h" #include <LibGUI/GPainter.h> +#include <LibGUI/GFontDatabase.h> #include <SharedGraphics/GraphicsBitmap.h> #include <stdlib.h> #include <time.h> @@ -7,13 +8,16 @@ SnakeGame::SnakeGame(GWidget* parent) : GWidget(parent) { - set_font(Font::default_bold_font()); + set_font(GFontDatabase::the().get_by_name("Liza Regular")); m_fruit_bitmaps.append(*GraphicsBitmap::load_from_file("/res/icons/snake/paprika.png")); m_fruit_bitmaps.append(*GraphicsBitmap::load_from_file("/res/icons/snake/eggplant.png")); m_fruit_bitmaps.append(*GraphicsBitmap::load_from_file("/res/icons/snake/cauliflower.png")); m_fruit_bitmaps.append(*GraphicsBitmap::load_from_file("/res/icons/snake/tomato.png")); srand(time(nullptr)); reset(); + + m_high_score = 0; + m_high_score_text = "Best: 0"; } SnakeGame::~SnakeGame() @@ -66,6 +70,12 @@ Rect SnakeGame::score_rect() const return { width() - score_width - 2, height() - font().glyph_height() - 2, score_width, font().glyph_height() }; } +Rect SnakeGame::high_score_rect() const +{ + int high_score_width = font().width(m_high_score_text); + return { 2, height() - font().glyph_height() - 2, high_score_width, font().glyph_height() }; +} + void SnakeGame::timer_event(CTimerEvent&) { Vector<Coordinate> dirty_cells; @@ -109,6 +119,11 @@ void SnakeGame::timer_event(CTimerEvent&) ++m_length; ++m_score; m_score_text = String::format("Score: %u", m_score); + if (m_score > m_high_score) { + m_high_score = m_score; + m_high_score_text = String::format("Best: %u", m_high_score); + update(high_score_rect()); + } update(score_rect()); dirty_cells.append(m_fruit); spawn_fruit(); @@ -188,6 +203,7 @@ void SnakeGame::paint_event(GPaintEvent& event) painter.draw_scaled_bitmap(cell_rect(m_fruit), *m_fruit_bitmaps[m_fruit_type], m_fruit_bitmaps[m_fruit_type]->rect()); + painter.draw_text(high_score_rect(), m_high_score_text, TextAlignment::TopLeft, Color::from_rgb(0xfafae0)); painter.draw_text(score_rect(), m_score_text, TextAlignment::TopLeft, Color::White); } diff --git a/Games/Snake/SnakeGame.h b/Games/Snake/SnakeGame.h index ec04227335..8f8a67c9a9 100644 --- a/Games/Snake/SnakeGame.h +++ b/Games/Snake/SnakeGame.h @@ -37,6 +37,7 @@ private: const Velocity& last_velocity() const; Rect cell_rect(const Coordinate&) const; Rect score_rect() const; + Rect high_score_rect() const; int m_rows { 20 }; int m_columns { 20 }; @@ -55,6 +56,8 @@ private: int m_length { 0 }; unsigned m_score { 0 }; String m_score_text; + unsigned m_high_score { 0 }; + String m_high_score_text; Vector<Retained<GraphicsBitmap>> m_fruit_bitmaps; }; |