diff options
Diffstat (limited to 'Games/Snake/SnakeGame.cpp')
-rw-r--r-- | Games/Snake/SnakeGame.cpp | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/Games/Snake/SnakeGame.cpp b/Games/Snake/SnakeGame.cpp index 4aad4e553a..24a539c83a 100644 --- a/Games/Snake/SnakeGame.cpp +++ b/Games/Snake/SnakeGame.cpp @@ -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,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 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() - }; + 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); painter.fill_rect(cell_rect(m_head), Color::Yellow); 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_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() |