mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 17:06:31 +00:00
FlappyBug: Persist high score to disk
Previously, the high score was only in-memory, so only persisted for as long as the FlappyBug window was open.
This commit is contained in:
parent
25c53e35e7
commit
1a2053781f
|
@ -27,9 +27,9 @@ void Game::reset()
|
|||
|
||||
void Game::game_over()
|
||||
{
|
||||
if (m_highscore.value_or(0) < m_difficulty) {
|
||||
m_highscore = m_difficulty;
|
||||
}
|
||||
if (on_game_end)
|
||||
m_highscore = on_game_end(static_cast<u32>(m_difficulty));
|
||||
|
||||
reset();
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,8 @@ public:
|
|||
static const int game_width = 560;
|
||||
static const int game_height = 480;
|
||||
|
||||
Function<u32(u32)> on_game_end;
|
||||
|
||||
private:
|
||||
Game();
|
||||
|
||||
|
|
|
@ -5,10 +5,12 @@
|
|||
*/
|
||||
|
||||
#include "Game.h"
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/Menubar.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -20,6 +22,7 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto app = GUI::Application::construct(argc, argv);
|
||||
auto config = Core::ConfigFile::get_for_app("FlappyBug");
|
||||
|
||||
if (pledge("stdio rpath wpath cpath recvfd sendfd", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
|
@ -31,11 +34,18 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (unveil(config->filename().characters(), "crw") < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (unveil(nullptr, nullptr) < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
u32 high_score = static_cast<u32>(config->read_num_entry("Game", "HighScore", 0));
|
||||
|
||||
auto window = GUI::Window::construct();
|
||||
window->resize(FlappyBug::Game::game_width, FlappyBug::Game::game_height);
|
||||
auto app_icon = GUI::Icon::default_icon("app-flappybug");
|
||||
|
@ -43,8 +53,20 @@ int main(int argc, char** argv)
|
|||
window->set_title("Flappy Bug");
|
||||
window->set_double_buffering_enabled(false);
|
||||
window->set_resizable(false);
|
||||
window->set_main_widget<FlappyBug::Game>();
|
||||
window->show();
|
||||
auto& widget = window->set_main_widget<FlappyBug::Game>();
|
||||
|
||||
widget.on_game_end = [&](u32 score) {
|
||||
if (score <= high_score)
|
||||
return high_score;
|
||||
|
||||
config->write_num_entry("Game", "HighScore", static_cast<int>(score));
|
||||
high_score = score;
|
||||
|
||||
if (!config->sync())
|
||||
GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error);
|
||||
|
||||
return high_score;
|
||||
};
|
||||
|
||||
auto menubar = GUI::Menubar::construct();
|
||||
|
||||
|
@ -57,6 +79,7 @@ int main(int argc, char** argv)
|
|||
help_menu.add_action(GUI::CommonActions::make_about_action("Flappy Bug", app_icon, window));
|
||||
|
||||
window->set_menubar(move(menubar));
|
||||
window->show();
|
||||
|
||||
return app->exec();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue