From 6ef8e2df5a6c5689d6993cfb5395e7d334a2bc90 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 20 Apr 2019 04:00:32 +0200 Subject: [PATCH] Snake: Use a vegetable icon for the fruit. --- Base/res/icons/snake/paprika.png | Bin 0 -> 168 bytes Games/Snake/SnakeGame.cpp | 8 +++++++- Games/Snake/SnakeGame.h | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Base/res/icons/snake/paprika.png diff --git a/Base/res/icons/snake/paprika.png b/Base/res/icons/snake/paprika.png new file mode 100644 index 0000000000000000000000000000000000000000..03ac589786b590aeafdd263b97a19b430f26d46b GIT binary patch literal 168 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s7@;zM~Lo7}wryOA2=Jhu{ zAtB*H)#4)sy8k$o^-_Iqu>3bR_~ZUeJ}vE^|0G5p9v+_md*>Tw>1}b3=9|;J{OsK V+z>pzhk=2C!PC{xWt~$(69B>EIywLV literal 0 HcmV?d00001 diff --git a/Games/Snake/SnakeGame.cpp b/Games/Snake/SnakeGame.cpp index 5f5541ef08..4aad4e553a 100644 --- a/Games/Snake/SnakeGame.cpp +++ b/Games/Snake/SnakeGame.cpp @@ -1,11 +1,13 @@ #include "SnakeGame.h" #include +#include #include #include SnakeGame::SnakeGame(GWidget* parent) : GWidget(parent) { + m_fruit_bitmap = GraphicsBitmap::load_from_file("/res/icons/snake/paprika.png"); srand(time(nullptr)); reset(); } @@ -19,6 +21,7 @@ void SnakeGame::reset() m_head = { m_rows / 2, m_columns / 2 }; m_tail.clear_with_capacity(); m_length = 2; + m_score = 0; m_velocity_queue.clear(); stop_timer(); start_timer(120); @@ -83,6 +86,7 @@ void SnakeGame::timer_event(CTimerEvent&) if (m_head == m_fruit) { ++m_length; + ++m_score; spawn_fruit(); } update(); @@ -141,7 +145,9 @@ void SnakeGame::paint_event(GPaintEvent& event) for (auto& coord : m_tail) painter.fill_rect(cell_rect(coord), Color::from_rgb(0xaaaa00)); - painter.fill_rect(cell_rect(m_fruit), Color::Red); + 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); } void SnakeGame::game_over() diff --git a/Games/Snake/SnakeGame.h b/Games/Snake/SnakeGame.h index 20c926ff70..3102e029e9 100644 --- a/Games/Snake/SnakeGame.h +++ b/Games/Snake/SnakeGame.h @@ -50,4 +50,7 @@ private: Coordinate m_fruit; int m_length { 0 }; + unsigned m_score { 0 }; + + RetainPtr m_fruit_bitmap; };