Snake: Don't hardcode the snake painting logic

This patch adds a m_snake_base_color variable which dictates the color
of the head of the snake and from which the rest of the snake color's
are derived from by darkening the base color.
This commit is contained in:
Baitinq 2022-12-18 21:25:46 +01:00 committed by Tim Flynn
parent b354e858c8
commit 56a75ec94e
Notes: sideshowbarker 2024-07-17 02:59:48 +09:00
2 changed files with 8 additions and 6 deletions

View File

@ -236,19 +236,19 @@ void SnakeGame::paint_event(GUI::PaintEvent& event)
painter.add_clip_rect(event.rect()); painter.add_clip_rect(event.rect());
painter.fill_rect(event.rect(), Color::Black); painter.fill_rect(event.rect(), Color::Black);
painter.fill_rect(cell_rect(m_head), Color::Yellow); painter.fill_rect(cell_rect(m_head), m_snake_base_color);
for (auto& part : m_tail) { for (auto& part : m_tail) {
auto rect = cell_rect(part); auto rect = cell_rect(part);
painter.fill_rect(rect, Color::from_rgb(0xaaaa00)); painter.fill_rect(rect, m_snake_base_color.darkened(0.77));
Gfx::IntRect left_side(rect.x(), rect.y(), 2, rect.height()); Gfx::IntRect left_side(rect.x(), rect.y(), 2, rect.height());
Gfx::IntRect top_side(rect.x(), rect.y(), rect.width(), 2); Gfx::IntRect top_side(rect.x(), rect.y(), rect.width(), 2);
Gfx::IntRect right_side(rect.right() - 1, rect.y(), 2, rect.height()); Gfx::IntRect right_side(rect.right() - 1, rect.y(), 2, rect.height());
Gfx::IntRect bottom_side(rect.x(), rect.bottom() - 1, rect.width(), 2); Gfx::IntRect bottom_side(rect.x(), rect.bottom() - 1, rect.width(), 2);
painter.fill_rect(left_side, Color::from_rgb(0xcccc00)); painter.fill_rect(left_side, m_snake_base_color.darkened(0.88));
painter.fill_rect(right_side, Color::from_rgb(0x888800)); painter.fill_rect(right_side, m_snake_base_color.darkened(0.55));
painter.fill_rect(top_side, Color::from_rgb(0xcccc00)); painter.fill_rect(top_side, m_snake_base_color.darkened(0.88));
painter.fill_rect(bottom_side, Color::from_rgb(0x888800)); painter.fill_rect(bottom_side, m_snake_base_color.darkened(0.55));
} }
painter.draw_scaled_bitmap(cell_rect(m_fruit), m_food_bitmaps[m_fruit_type], m_food_bitmaps[m_fruit_type].rect()); painter.draw_scaled_bitmap(cell_rect(m_fruit), m_food_bitmaps[m_fruit_type], m_food_bitmaps[m_fruit_type].rect());

View File

@ -75,4 +75,6 @@ private:
bool m_is_new_high_score { false }; bool m_is_new_high_score { false };
NonnullRefPtrVector<Gfx::Bitmap> m_food_bitmaps; NonnullRefPtrVector<Gfx::Bitmap> m_food_bitmaps;
Gfx::Color m_snake_base_color { Color::Yellow };
}; };