mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-21 18:37:58 +03:00
Snake: Try to only repaint the parts that actually changed between ticks.
This commit is contained in:
parent
2d736d5591
commit
79573ce7e6
Notes:
sideshowbarker
2024-07-19 14:38:32 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/79573ce7e66
@ -7,6 +7,7 @@
|
||||
SnakeGame::SnakeGame(GWidget* parent)
|
||||
: GWidget(parent)
|
||||
{
|
||||
set_font(Font::default_bold_font());
|
||||
m_fruit_bitmap = GraphicsBitmap::load_from_file("/res/icons/snake/paprika.png");
|
||||
srand(time(nullptr));
|
||||
reset();
|
||||
@ -22,10 +23,12 @@ void SnakeGame::reset()
|
||||
m_tail.clear_with_capacity();
|
||||
m_length = 2;
|
||||
m_score = 0;
|
||||
m_score_text = "Score: 0";
|
||||
m_velocity_queue.clear();
|
||||
stop_timer();
|
||||
start_timer(120);
|
||||
spawn_fruit();
|
||||
update();
|
||||
}
|
||||
|
||||
bool SnakeGame::is_available(const Coordinate& coord)
|
||||
@ -53,16 +56,28 @@ void SnakeGame::spawn_fruit()
|
||||
m_fruit = coord;
|
||||
}
|
||||
|
||||
Rect SnakeGame::score_rect() const
|
||||
{
|
||||
int score_width = font().width(m_score_text);
|
||||
return { width() - score_width - 2, height() - font().glyph_height() - 2, score_width, font().glyph_height() };
|
||||
}
|
||||
|
||||
void SnakeGame::timer_event(CTimerEvent&)
|
||||
{
|
||||
Vector<Coordinate> dirty_cells;
|
||||
|
||||
m_tail.prepend(m_head);
|
||||
|
||||
if (m_tail.size() > m_length)
|
||||
if (m_tail.size() > m_length) {
|
||||
dirty_cells.append(m_tail.last());
|
||||
m_tail.take_last();
|
||||
}
|
||||
|
||||
if (!m_velocity_queue.is_empty())
|
||||
m_velocity = m_velocity_queue.dequeue();
|
||||
|
||||
dirty_cells.append(m_head);
|
||||
|
||||
m_head.row += m_velocity.vertical;
|
||||
m_head.column += m_velocity.horizontal;
|
||||
|
||||
@ -77,6 +92,8 @@ void SnakeGame::timer_event(CTimerEvent&)
|
||||
if (m_head.column < 0)
|
||||
m_head.column = m_columns - 1;
|
||||
|
||||
dirty_cells.append(m_head);
|
||||
|
||||
for (int i = 0; i < m_tail.size(); ++i) {
|
||||
if (m_head == m_tail[i]) {
|
||||
game_over();
|
||||
@ -87,9 +104,16 @@ void SnakeGame::timer_event(CTimerEvent&)
|
||||
if (m_head == m_fruit) {
|
||||
++m_length;
|
||||
++m_score;
|
||||
m_score_text = String::format("Score: %u", m_score);
|
||||
update(score_rect());
|
||||
dirty_cells.append(m_fruit);
|
||||
spawn_fruit();
|
||||
dirty_cells.append(m_fruit);
|
||||
}
|
||||
|
||||
for (auto& coord : dirty_cells) {
|
||||
update(cell_rect(coord));
|
||||
}
|
||||
update();
|
||||
}
|
||||
|
||||
void SnakeGame::keydown_event(GKeyEvent& event)
|
||||
@ -124,30 +148,31 @@ void SnakeGame::keydown_event(GKeyEvent& event)
|
||||
}
|
||||
}
|
||||
|
||||
Rect SnakeGame::cell_rect(const Coordinate& coord) const
|
||||
{
|
||||
auto game_rect = rect();
|
||||
auto cell_size = Size(game_rect.width() / m_columns, game_rect.height() / m_rows);
|
||||
return {
|
||||
coord.column * cell_size.width(),
|
||||
coord.row * cell_size.height(),
|
||||
cell_size.width(),
|
||||
cell_size.height()
|
||||
};
|
||||
}
|
||||
|
||||
void SnakeGame::paint_event(GPaintEvent& event)
|
||||
{
|
||||
GPainter painter(*this);
|
||||
painter.add_clip_rect(event.rect());
|
||||
painter.fill_rect(event.rect(), Color::Black);
|
||||
|
||||
auto game_rect = rect();
|
||||
auto cell_size = Size(game_rect.width() / m_columns, game_rect.height() / m_rows);
|
||||
|
||||
auto cell_rect = [&] (const Coordinate& coord) -> Rect {
|
||||
return {
|
||||
coord.column * cell_size.width(),
|
||||
coord.row * cell_size.height(),
|
||||
cell_size.width(),
|
||||
cell_size.height()
|
||||
};
|
||||
};
|
||||
|
||||
painter.fill_rect(cell_rect(m_head), Color::Yellow);
|
||||
for (auto& coord : m_tail)
|
||||
painter.fill_rect(cell_rect(coord), Color::from_rgb(0xaaaa00));
|
||||
|
||||
painter.draw_scaled_bitmap(cell_rect(m_fruit), *m_fruit_bitmap, m_fruit_bitmap->rect());
|
||||
|
||||
painter.draw_text(rect(), String::format("Score: %u", m_score), TextAlignment::TopLeft, Color::White);
|
||||
painter.draw_text(score_rect(), m_score_text, TextAlignment::TopLeft, Color::White);
|
||||
}
|
||||
|
||||
void SnakeGame::game_over()
|
||||
|
@ -35,6 +35,8 @@ private:
|
||||
bool is_available(const Coordinate&);
|
||||
void queue_velocity(int v, int h);
|
||||
const Velocity& last_velocity() const;
|
||||
Rect cell_rect(const Coordinate&) const;
|
||||
Rect score_rect() const;
|
||||
|
||||
int m_rows { 20 };
|
||||
int m_columns { 20 };
|
||||
@ -51,6 +53,7 @@ private:
|
||||
|
||||
int m_length { 0 };
|
||||
unsigned m_score { 0 };
|
||||
String m_score_text;
|
||||
|
||||
RetainPtr<GraphicsBitmap> m_fruit_bitmap;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user