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

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.
This commit is contained in:
Mim Hufford 2021-06-01 17:59:01 +01:00 committed by Linus Groh
parent 3f603ab082
commit 018344bb07
2 changed files with 23 additions and 5 deletions

View file

@ -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) {

View file

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