mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 19:58:11 +00:00
FlappyBug: Propagate errors in Bug class
Move-construct Bug into the Game class to improve error handling.
This commit is contained in:
parent
a67afa735c
commit
ff359c27e3
3 changed files with 21 additions and 5 deletions
|
@ -8,7 +8,8 @@
|
|||
|
||||
namespace FlappyBug {
|
||||
|
||||
Game::Game()
|
||||
Game::Game(Bug bug)
|
||||
: m_bug(move(bug))
|
||||
{
|
||||
set_override_cursor(Gfx::StandardCursor::Hidden);
|
||||
start_timer(16);
|
||||
|
|
|
@ -44,11 +44,26 @@ public:
|
|||
const float x { 50 };
|
||||
const float radius { 16 };
|
||||
const float starting_y { 200 };
|
||||
NonnullRefPtr<Gfx::Bitmap> falling_bitmap { Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/falling.png").release_value_but_fixme_should_propagate_errors() };
|
||||
NonnullRefPtr<Gfx::Bitmap> flapping_bitmap { Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/flapping.png").release_value_but_fixme_should_propagate_errors() };
|
||||
NonnullRefPtr<Gfx::Bitmap> falling_bitmap;
|
||||
NonnullRefPtr<Gfx::Bitmap> flapping_bitmap;
|
||||
float y {};
|
||||
float velocity {};
|
||||
|
||||
private:
|
||||
Bug(NonnullRefPtr<Gfx::Bitmap> falling_bitmap_value, NonnullRefPtr<Gfx::Bitmap> flapping_bitmap_value)
|
||||
: falling_bitmap(move(falling_bitmap_value))
|
||||
, flapping_bitmap(move(flapping_bitmap_value))
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
static ErrorOr<Bug> construct()
|
||||
{
|
||||
NonnullRefPtr<Gfx::Bitmap> falling_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/falling.png"));
|
||||
NonnullRefPtr<Gfx::Bitmap> flapping_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/flapping.png"));
|
||||
return Bug(move(falling_bitmap), move(flapping_bitmap));
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
y = starting_y;
|
||||
|
@ -153,7 +168,7 @@ private:
|
|||
const Gfx::IntRect m_score_rect { 10, 10, 20, 20 };
|
||||
const Gfx::IntRect m_text_rect { game_width / 2 - 80, game_height / 2 - 40, 160, 80 };
|
||||
|
||||
Game();
|
||||
Game(Bug);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->set_title("Flappy Bug");
|
||||
window->set_double_buffering_enabled(false);
|
||||
window->set_resizable(false);
|
||||
auto widget = TRY(window->try_set_main_widget<FlappyBug::Game>());
|
||||
auto widget = TRY(window->try_set_main_widget<FlappyBug::Game>(TRY(FlappyBug::Game::Bug::construct())));
|
||||
|
||||
widget->on_game_end = [&](u32 score) {
|
||||
if (score <= high_score)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue