1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:27:35 +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

@ -18,6 +18,7 @@
#include <LibGUI/Icon.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/Statusbar.h>
#include <LibGUI/Window.h>
#include <LibMain/Main.h>
#include <stdio.h>
@ -45,7 +46,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->set_double_buffering_enabled(false);
window->set_title("Snake");
window->resize(324, 344);
window->resize(324, 345);
auto widget = TRY(window->try_set_main_widget<GUI::Widget>());
widget->load_from_gml(snake_gml);
@ -53,6 +54,24 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto& game = *widget->find_descendant_of_type_named<Snake::Game>("game");
game.set_focus(true);
auto high_score = Config::read_u32("Snake"sv, "Snake"sv, "HighScore"sv, 0);
auto& statusbar = *widget->find_descendant_of_type_named<GUI::Statusbar>("statusbar"sv);
statusbar.set_text(0, "Score: 0"sv);
statusbar.set_text(1, DeprecatedString::formatted("High Score: {}", high_score));
game.on_score_update = [&](auto score) {
statusbar.set_text(0, DeprecatedString::formatted("Score: {}", score));
if (score <= high_score)
return false;
statusbar.set_text(1, DeprecatedString::formatted("High Score: {}", score));
Config::write_u32("Snake"sv, "Snake"sv, "HighScore"sv, score);
high_score = score;
return true;
};
auto game_menu = TRY(window->try_add_menu("&Game"));
TRY(game_menu->try_add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/reload.png"sv)), [&](auto&) {