1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 00:27:43 +00:00

Snake: Use a statusbar to display the current and high score

The food bitmaps would sometimes be placed underneath the score text,
which was a bit hard to see. Use a statusbar like we do in other games
like Solitaire.

Note the default height change of the Snake window is to make the inner
game widget fit exactly 20x20 cells.
This commit is contained in:
Timothy Flynn 2022-12-20 08:55:56 -05:00 committed by Andreas Kling
parent cb66c02bc4
commit 661c02b914
4 changed files with 36 additions and 33 deletions

View file

@ -74,8 +74,7 @@ Game::Game()
{
set_font(Gfx::FontDatabase::default_fixed_width_font().bold_variant());
reset();
m_high_score = Config::read_i32("Snake"sv, "Snake"sv, "HighScore"sv, 0);
m_high_score_text = DeprecatedString::formatted("Best: {}", m_high_score);
m_snake_base_color = Color::from_argb(Config::read_u32("Snake"sv, "Snake"sv, "BaseColor"sv, m_snake_base_color.value()));
}
@ -96,9 +95,12 @@ void Game::reset()
m_tail.clear_with_capacity();
m_length = 2;
m_score = 0;
m_score_text = "Score: 0";
m_is_new_high_score = false;
m_velocity_queue.clear();
if (on_score_update)
on_score_update(m_score);
pause();
start();
spawn_fruit();
@ -137,18 +139,6 @@ void Game::spawn_fruit()
m_fruit_type = get_random_uniform(m_food_bitmaps.size());
}
Gfx::IntRect Game::score_rect() const
{
int score_width = font().width(m_score_text);
return { frame_inner_rect().width() - score_width - 2, frame_inner_rect().height() - font().glyph_height() - 2, score_width, font().glyph_height() };
}
Gfx::IntRect Game::high_score_rect() const
{
int high_score_width = font().width(m_high_score_text);
return { frame_thickness() + 2, frame_inner_rect().height() - font().glyph_height() - 2, high_score_width, font().glyph_height() };
}
void Game::timer_event(Core::TimerEvent&)
{
Vector<Coordinate> dirty_cells;
@ -191,15 +181,10 @@ void Game::timer_event(Core::TimerEvent&)
if (m_head == m_fruit) {
++m_length;
++m_score;
m_score_text = DeprecatedString::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 = DeprecatedString::formatted("Best: {}", m_high_score);
update(high_score_rect());
Config::write_i32("Snake"sv, "Snake"sv, "HighScore"sv, m_high_score);
}
update(score_rect());
if (on_score_update)
m_is_new_high_score = on_score_update(m_score);
dirty_cells.append(m_fruit);
spawn_fruit();
dirty_cells.append(m_fruit);
@ -279,9 +264,6 @@ void Game::paint_event(GUI::PaintEvent& event)
}
painter.draw_scaled_bitmap(cell_rect(m_fruit), m_food_bitmaps[m_fruit_type], m_food_bitmaps[m_fruit_type].rect());
painter.draw_text(high_score_rect(), m_high_score_text, Gfx::TextAlignment::TopLeft, Color::from_rgb(0xfafae0));
painter.draw_text(score_rect(), m_score_text, Gfx::TextAlignment::TopLeft, Color::White);
}
void Game::game_over()