mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 13:57:34 +00:00
Snake: Try to only repaint the parts that actually changed between ticks.
This commit is contained in:
parent
2d736d5591
commit
79573ce7e6
2 changed files with 43 additions and 15 deletions
|
@ -7,6 +7,7 @@
|
||||||
SnakeGame::SnakeGame(GWidget* parent)
|
SnakeGame::SnakeGame(GWidget* parent)
|
||||||
: GWidget(parent)
|
: GWidget(parent)
|
||||||
{
|
{
|
||||||
|
set_font(Font::default_bold_font());
|
||||||
m_fruit_bitmap = GraphicsBitmap::load_from_file("/res/icons/snake/paprika.png");
|
m_fruit_bitmap = GraphicsBitmap::load_from_file("/res/icons/snake/paprika.png");
|
||||||
srand(time(nullptr));
|
srand(time(nullptr));
|
||||||
reset();
|
reset();
|
||||||
|
@ -22,10 +23,12 @@ void SnakeGame::reset()
|
||||||
m_tail.clear_with_capacity();
|
m_tail.clear_with_capacity();
|
||||||
m_length = 2;
|
m_length = 2;
|
||||||
m_score = 0;
|
m_score = 0;
|
||||||
|
m_score_text = "Score: 0";
|
||||||
m_velocity_queue.clear();
|
m_velocity_queue.clear();
|
||||||
stop_timer();
|
stop_timer();
|
||||||
start_timer(120);
|
start_timer(120);
|
||||||
spawn_fruit();
|
spawn_fruit();
|
||||||
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SnakeGame::is_available(const Coordinate& coord)
|
bool SnakeGame::is_available(const Coordinate& coord)
|
||||||
|
@ -53,16 +56,28 @@ void SnakeGame::spawn_fruit()
|
||||||
m_fruit = coord;
|
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&)
|
void SnakeGame::timer_event(CTimerEvent&)
|
||||||
{
|
{
|
||||||
|
Vector<Coordinate> dirty_cells;
|
||||||
|
|
||||||
m_tail.prepend(m_head);
|
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();
|
m_tail.take_last();
|
||||||
|
}
|
||||||
|
|
||||||
if (!m_velocity_queue.is_empty())
|
if (!m_velocity_queue.is_empty())
|
||||||
m_velocity = m_velocity_queue.dequeue();
|
m_velocity = m_velocity_queue.dequeue();
|
||||||
|
|
||||||
|
dirty_cells.append(m_head);
|
||||||
|
|
||||||
m_head.row += m_velocity.vertical;
|
m_head.row += m_velocity.vertical;
|
||||||
m_head.column += m_velocity.horizontal;
|
m_head.column += m_velocity.horizontal;
|
||||||
|
|
||||||
|
@ -77,6 +92,8 @@ void SnakeGame::timer_event(CTimerEvent&)
|
||||||
if (m_head.column < 0)
|
if (m_head.column < 0)
|
||||||
m_head.column = m_columns - 1;
|
m_head.column = m_columns - 1;
|
||||||
|
|
||||||
|
dirty_cells.append(m_head);
|
||||||
|
|
||||||
for (int i = 0; i < m_tail.size(); ++i) {
|
for (int i = 0; i < m_tail.size(); ++i) {
|
||||||
if (m_head == m_tail[i]) {
|
if (m_head == m_tail[i]) {
|
||||||
game_over();
|
game_over();
|
||||||
|
@ -87,9 +104,16 @@ void SnakeGame::timer_event(CTimerEvent&)
|
||||||
if (m_head == m_fruit) {
|
if (m_head == m_fruit) {
|
||||||
++m_length;
|
++m_length;
|
||||||
++m_score;
|
++m_score;
|
||||||
|
m_score_text = String::format("Score: %u", m_score);
|
||||||
|
update(score_rect());
|
||||||
|
dirty_cells.append(m_fruit);
|
||||||
spawn_fruit();
|
spawn_fruit();
|
||||||
|
dirty_cells.append(m_fruit);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& coord : dirty_cells) {
|
||||||
|
update(cell_rect(coord));
|
||||||
}
|
}
|
||||||
update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::keydown_event(GKeyEvent& event)
|
void SnakeGame::keydown_event(GKeyEvent& event)
|
||||||
|
@ -124,22 +148,23 @@ void SnakeGame::keydown_event(GKeyEvent& event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::paint_event(GPaintEvent& event)
|
Rect SnakeGame::cell_rect(const Coordinate& coord) const
|
||||||
{
|
{
|
||||||
GPainter painter(*this);
|
|
||||||
painter.fill_rect(event.rect(), Color::Black);
|
|
||||||
|
|
||||||
auto game_rect = rect();
|
auto game_rect = rect();
|
||||||
auto cell_size = Size(game_rect.width() / m_columns, game_rect.height() / m_rows);
|
auto cell_size = Size(game_rect.width() / m_columns, game_rect.height() / m_rows);
|
||||||
|
|
||||||
auto cell_rect = [&] (const Coordinate& coord) -> Rect {
|
|
||||||
return {
|
return {
|
||||||
coord.column * cell_size.width(),
|
coord.column * cell_size.width(),
|
||||||
coord.row * cell_size.height(),
|
coord.row * cell_size.height(),
|
||||||
cell_size.width(),
|
cell_size.width(),
|
||||||
cell_size.height()
|
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);
|
||||||
|
|
||||||
painter.fill_rect(cell_rect(m_head), Color::Yellow);
|
painter.fill_rect(cell_rect(m_head), Color::Yellow);
|
||||||
for (auto& coord : m_tail)
|
for (auto& coord : m_tail)
|
||||||
|
@ -147,7 +172,7 @@ void SnakeGame::paint_event(GPaintEvent& event)
|
||||||
|
|
||||||
painter.draw_scaled_bitmap(cell_rect(m_fruit), *m_fruit_bitmap, m_fruit_bitmap->rect());
|
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()
|
void SnakeGame::game_over()
|
||||||
|
|
|
@ -35,6 +35,8 @@ private:
|
||||||
bool is_available(const Coordinate&);
|
bool is_available(const Coordinate&);
|
||||||
void queue_velocity(int v, int h);
|
void queue_velocity(int v, int h);
|
||||||
const Velocity& last_velocity() const;
|
const Velocity& last_velocity() const;
|
||||||
|
Rect cell_rect(const Coordinate&) const;
|
||||||
|
Rect score_rect() const;
|
||||||
|
|
||||||
int m_rows { 20 };
|
int m_rows { 20 };
|
||||||
int m_columns { 20 };
|
int m_columns { 20 };
|
||||||
|
@ -51,6 +53,7 @@ private:
|
||||||
|
|
||||||
int m_length { 0 };
|
int m_length { 0 };
|
||||||
unsigned m_score { 0 };
|
unsigned m_score { 0 };
|
||||||
|
String m_score_text;
|
||||||
|
|
||||||
RetainPtr<GraphicsBitmap> m_fruit_bitmap;
|
RetainPtr<GraphicsBitmap> m_fruit_bitmap;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue