diff options
author | Mim Hufford <mim@hotmail.co.uk> | 2021-06-01 17:59:01 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-20 10:54:27 +0100 |
commit | 018344bb0790f5cdd9582662690cbc4c0209156a (patch) | |
tree | c4282495fdec6b042d41c485b56abb1486a86ecf /Userland/Games | |
parent | 3f603ab0828a8e80990c38cf0845e67a2d443943 (diff) | |
download | serenity-018344bb0790f5cdd9582662690cbc4c0209156a.zip |
FlappyBug: Keep track of score and highscore
Better information is now shown to the player. Instructions are shown
when first loading the program, and any available scores are shown on
the game over screen.
Diffstat (limited to 'Userland/Games')
-rw-r--r-- | Userland/Games/FlappyBug/Game.cpp | 25 | ||||
-rw-r--r-- | Userland/Games/FlappyBug/Game.h | 3 |
2 files changed, 23 insertions, 5 deletions
diff --git a/Userland/Games/FlappyBug/Game.cpp b/Userland/Games/FlappyBug/Game.cpp index db44f7ba3b..2c7e62b511 100644 --- a/Userland/Games/FlappyBug/Game.cpp +++ b/Userland/Games/FlappyBug/Game.cpp @@ -18,11 +18,20 @@ Game::Game() void Game::reset() { m_active = false; + m_last_score = m_difficulty; m_difficulty = 1; m_bug.reset(); m_obstacle.reset(); } +void Game::game_over() +{ + if (m_highscore.value_or(0) < m_difficulty) { + m_highscore = m_difficulty; + } + reset(); +} + void Game::timer_event(Core::TimerEvent&) { tick(); @@ -39,7 +48,13 @@ void Game::paint_event(GUI::PaintEvent& event) painter.fill_rect(enclosing_int_rect(m_obstacle.bottom_rect()), Color::White); painter.fill_ellipse(enclosing_int_rect(m_bug.rect()), Color::Red); - painter.draw_text({ 10, 10, 100, 100 }, String::formatted("{}", m_difficulty), Gfx::TextAlignment::TopLeft, Color::Green); + if (m_active) { + painter.draw_text({ 10, 10, 100, 100 }, String::formatted("{:.0}", m_difficulty), Gfx::TextAlignment::TopLeft, Color::Green); + } else if (m_highscore.has_value()) { + painter.draw_text(rect(), String::formatted("Your score: {:.0}\nHighscore: {:.0}\nPress any key to start", m_last_score, m_highscore.value()), Gfx::TextAlignment::Center, Color::Green); + } else { + painter.draw_text(rect(), "Press any key to start", Gfx::TextAlignment::Center, Color::Green); + } } void Game::keydown_event(GUI::KeyEvent& event) @@ -58,18 +73,18 @@ void Game::keydown_event(GUI::KeyEvent& event) void Game::tick() { if (m_active) { - m_difficulty += 0.0001f; + m_difficulty += 1.0f / 16.0f; m_bug.fall(); m_bug.apply_velocity(); - m_obstacle.x -= 4 + m_difficulty; + m_obstacle.x -= 4 + m_difficulty / 16.0f; if (m_bug.y > game_height || m_bug.y < 0) { - reset(); + game_over(); } if (m_bug.rect().intersects(m_obstacle.top_rect()) || m_bug.rect().intersects(m_obstacle.bottom_rect())) { - reset(); + game_over(); } if (m_obstacle.x < 0) { diff --git a/Userland/Games/FlappyBug/Game.h b/Userland/Games/FlappyBug/Game.h index ad0d20fa39..8d03ec87db 100644 --- a/Userland/Games/FlappyBug/Game.h +++ b/Userland/Games/FlappyBug/Game.h @@ -33,6 +33,7 @@ private: void tick(); void reset(); + void game_over(); struct Bug { const float x { 50 }; @@ -95,6 +96,8 @@ private: Bug m_bug; Obstacle m_obstacle; bool m_active; + Optional<float> m_highscore; + float m_last_score; float m_difficulty; }; |