Skip to content

Commit ddc855f

Browse files
mimhuffordlinusg
authored andcommitted
FlappyBug: Add cloud and sky graphics
We now have a nice sunset sky and some random cloud graphics. The obstacles will get graphics at some point too :)
1 parent 28e08f0 commit ddc855f

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed
650 Bytes
Loading
23.5 KB
Loading
13.9 KB
Loading
19.4 KB
Loading

Userland/Games/FlappyBug/Game.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ void Game::paint_event(GUI::PaintEvent& event)
5656
GUI::Painter painter(*this);
5757
painter.add_clip_rect(event.rect());
5858

59-
painter.fill_rect(rect(), m_sky_color);
59+
painter.draw_tiled_bitmap(rect(), *m_background_bitmap);
60+
61+
painter.draw_scaled_bitmap(m_cloud.rect(), *m_cloud.bitmap(), m_cloud.bitmap()->rect(), 0.2f);
6062

6163
painter.fill_rect(enclosing_int_rect(m_obstacle.top_rect()), m_obstacle.color);
6264
painter.fill_rect(enclosing_int_rect(m_obstacle.bottom_rect()), m_obstacle.color);
@@ -98,6 +100,7 @@ void Game::tick()
98100
m_bug.fall();
99101
m_bug.apply_velocity();
100102
m_obstacle.x -= 4 + m_difficulty / 16.0f;
103+
m_cloud.x -= m_difficulty / 16.0f;
101104

102105
if (m_bug.y > game_height || m_bug.y < 0) {
103106
game_over();
@@ -110,6 +113,10 @@ void Game::tick()
110113
if (m_obstacle.x < 0) {
111114
m_obstacle.reset();
112115
}
116+
117+
if (m_cloud.x < 0) {
118+
m_cloud.reset();
119+
}
113120
}
114121

115122
m_restart_cooldown -= 1.0f / 16.0f;

Userland/Games/FlappyBug/Game.h

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <AK/Optional.h>
1010
#include <AK/Random.h>
11+
#include <AK/Vector.h>
1112
#include <LibGUI/Application.h>
1213
#include <LibGUI/Painter.h>
1314
#include <LibGUI/Widget.h>
@@ -81,7 +82,7 @@ class Game final : public GUI::Widget {
8182
struct Obstacle {
8283
const float width { 20 };
8384
Color color { Color::DarkGray };
84-
float x;
85+
float x {};
8586
float gap_top_y { 200 };
8687
float gap_height { 175 };
8788

@@ -102,14 +103,49 @@ class Game final : public GUI::Widget {
102103
}
103104
};
104105

106+
struct Cloud {
107+
const Vector<RefPtr<Gfx::Bitmap>> cloud_bitmaps {
108+
Gfx::Bitmap::load_from_file("/res/icons/flappybug/cloud_0.png"),
109+
Gfx::Bitmap::load_from_file("/res/icons/flappybug/cloud_1.png"),
110+
Gfx::Bitmap::load_from_file("/res/icons/flappybug/cloud_2.png"),
111+
};
112+
float x {};
113+
float y {};
114+
int bitmap_id {};
115+
116+
Cloud()
117+
{
118+
reset();
119+
x = get_random_uniform(game_width);
120+
}
121+
122+
void reset()
123+
{
124+
bitmap_id = get_random_uniform(cloud_bitmaps.size());
125+
x = game_width + bitmap()->width();
126+
y = get_random_uniform(game_height / 2) + bitmap()->height();
127+
}
128+
129+
RefPtr<Gfx::Bitmap> bitmap() const
130+
{
131+
return cloud_bitmaps[bitmap_id];
132+
}
133+
134+
Gfx::IntRect rect() const
135+
{
136+
return { (int)x - bitmap()->width(), (int)y - bitmap()->height(), bitmap()->width(), bitmap()->height() };
137+
}
138+
};
139+
105140
Bug m_bug;
106141
Obstacle m_obstacle;
142+
Cloud m_cloud;
107143
bool m_active;
108-
Optional<float> m_highscore;
109-
float m_last_score;
110-
float m_difficulty;
111-
float m_restart_cooldown;
112-
Color m_sky_color { 100, 100, 200 };
144+
Optional<float> m_highscore {};
145+
float m_last_score {};
146+
float m_difficulty {};
147+
float m_restart_cooldown {};
148+
const RefPtr<Gfx::Bitmap> m_background_bitmap { Gfx::Bitmap::load_from_file("/res/icons/flappybug/background.png") };
113149
};
114150

115151
}

0 commit comments

Comments
 (0)