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

FlappyBug: Add an input cooldown after game over

This makes sure the player doesn't accidentally start a new game after
they bump into an obstacle.
This commit is contained in:
Mim Hufford 2021-06-02 10:37:00 +01:00 committed by Linus Groh
parent 018344bb07
commit f50003bdd2
2 changed files with 26 additions and 3 deletions

View file

@ -20,6 +20,7 @@ void Game::reset()
m_active = false;
m_last_score = m_difficulty;
m_difficulty = 1;
m_restart_cooldown = 3;
m_bug.reset();
m_obstacle.reset();
}
@ -32,6 +33,19 @@ void Game::game_over()
reset();
}
bool Game::ready_to_start() const
{
if (!m_highscore.has_value()) {
return true;
}
if (m_restart_cooldown <= 0) {
return true;
}
return false;
}
void Game::timer_event(Core::TimerEvent&)
{
tick();
@ -51,7 +65,8 @@ void Game::paint_event(GUI::PaintEvent& event)
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);
auto message = String::formatted("Your score: {:.0}\nHighscore: {:.0}\n\n{}", m_last_score, m_highscore.value(), m_restart_cooldown < 0 ? "Press any key to play again" : " ");
painter.draw_text(rect(), message, Gfx::TextAlignment::Center, Color::Green);
} else {
painter.draw_text(rect(), "Press any key to start", Gfx::TextAlignment::Center, Color::Green);
}
@ -64,8 +79,12 @@ void Game::keydown_event(GUI::KeyEvent& event)
GUI::Application::the()->quit();
break;
default:
m_active = true;
m_bug.flap();
if (ready_to_start()) {
m_active = true;
}
if (m_active) {
m_bug.flap();
}
break;
}
}
@ -92,6 +111,8 @@ void Game::tick()
}
}
m_restart_cooldown -= 1.0f / 16.0f;
update();
}

View file

@ -34,6 +34,7 @@ private:
void tick();
void reset();
void game_over();
bool ready_to_start() const;
struct Bug {
const float x { 50 };
@ -99,6 +100,7 @@ private:
Optional<float> m_highscore;
float m_last_score;
float m_difficulty;
float m_restart_cooldown;
};
}