Snake: Show the highest score achieved so far (in this session.)

This commit is contained in:
Andreas Kling 2019-04-20 21:24:38 +02:00
parent 0dcb7a234e
commit a56e1afb64
2 changed files with 20 additions and 1 deletions

View file

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

View file

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