1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:38:10 +00:00
serenity/Userland/Games/FlappyBug/Game.cpp
Mim Hufford 018344bb07 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.
2021-06-20 10:54:27 +01:00

98 lines
2.2 KiB
C++

/*
* Copyright (c) 2021, Mim Hufford <mim@hotmail.co.uk>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Game.h"
namespace FlappyBug {
Game::Game()
{
set_override_cursor(Gfx::StandardCursor::Hidden);
start_timer(16);
reset();
}
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();
}
void Game::paint_event(GUI::PaintEvent& event)
{
GUI::Painter painter(*this);
painter.add_clip_rect(event.rect());
painter.fill_rect(rect(), Color::Black);
painter.fill_rect(enclosing_int_rect(m_obstacle.top_rect()), Color::White);
painter.fill_rect(enclosing_int_rect(m_obstacle.bottom_rect()), Color::White);
painter.fill_ellipse(enclosing_int_rect(m_bug.rect()), Color::Red);
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)
{
switch (event.key()) {
case Key_Escape:
GUI::Application::the()->quit();
break;
default:
m_active = true;
m_bug.flap();
break;
}
}
void Game::tick()
{
if (m_active) {
m_difficulty += 1.0f / 16.0f;
m_bug.fall();
m_bug.apply_velocity();
m_obstacle.x -= 4 + m_difficulty / 16.0f;
if (m_bug.y > game_height || m_bug.y < 0) {
game_over();
}
if (m_bug.rect().intersects(m_obstacle.top_rect()) || m_bug.rect().intersects(m_obstacle.bottom_rect())) {
game_over();
}
if (m_obstacle.x < 0) {
m_obstacle.reset();
}
}
update();
}
}