From c9b86be1ccadef17b103a3ee6ba557dd37b8235f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 20 Apr 2019 18:50:41 +0200 Subject: [PATCH] Snake: Add some more fruit types. --- Base/res/icons/snake/cauliflower.png | Bin 0 -> 282 bytes Base/res/icons/snake/eggplant.png | Bin 0 -> 313 bytes Base/res/icons/snake/tomato.png | Bin 0 -> 235 bytes Games/Snake/SnakeGame.cpp | 24 ++++++++++++++++++++---- Games/Snake/SnakeGame.h | 3 ++- Games/Snake/main.cpp | 2 +- 6 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 Base/res/icons/snake/cauliflower.png create mode 100644 Base/res/icons/snake/eggplant.png create mode 100644 Base/res/icons/snake/tomato.png diff --git a/Base/res/icons/snake/cauliflower.png b/Base/res/icons/snake/cauliflower.png new file mode 100644 index 0000000000000000000000000000000000000000..6bcda88e51ffa2f2f17aff3b2dce7821c4ed6fd2 GIT binary patch literal 282 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s79(uYshFF|lI>}b3$v~hr zUZUDfZHvgXDpqTC^9|d#evlDcxwB#J^bMQ0vBMX=Ntc?#(%()3Doum8)j@>s?Ic%Sst}XC-MSKR?sx#`WgW&GeXUcNtD>-tzdx zHm-B*4F@LF7_C%##m=yyJ|=ReNZ57JkcA7m{xUyl|7{=?njsT!zI-bP0l+XkKW9W9z literal 0 HcmV?d00001 diff --git a/Base/res/icons/snake/eggplant.png b/Base/res/icons/snake/eggplant.png new file mode 100644 index 0000000000000000000000000000000000000000..e017e2d2a444a0ec6f431470d751fc5d226f585f GIT binary patch literal 313 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s789ZGaLo80Oon-6BY$)J1 zfA6#)wJ5EoEt&!6Cd`vlENtlDW>J}<_EJ&v(o?qT)&k~p4pZ|~t@Z?+^62wr)P8Gq zPhN4^rgWS9XTMW^tlv>1J!}7$&egGx))=Ta{CI7}^rNCeJeHBm!Af-MQa558M>zWT;LYtbKfF3w)LD~^BC8uIU~kMq#u Ts;FULU|{fc^>bP0l+XkK3S5H^ literal 0 HcmV?d00001 diff --git a/Base/res/icons/snake/tomato.png b/Base/res/icons/snake/tomato.png new file mode 100644 index 0000000000000000000000000000000000000000..f341ed1cb45e51cb1e516c04f4d5477e607f0ef1 GIT binary patch literal 235 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s7HhH=@hFF|VPFNsdAQYsq z)Af~o&)+-GC6yhS|5+RUaTlMgH7QA;X7!N+2M!!4(EZ2JB_~$WE1z2WaMGWCePaUy zg9E1Vu@x1)w0ks(nKF0qapJp)yDU#Xx^lyI>!%l{y3`sf1 zvKd_U6pnsN-e0L%ey~1;dBWqV5w|5T#yWHp{bbI}%-nqT;rX5lV_}_z i37bz!t(vLDz#y1muG{)zUmODi1B0ilpUXO@geCy6k64=k literal 0 HcmV?d00001 diff --git a/Games/Snake/SnakeGame.cpp b/Games/Snake/SnakeGame.cpp index 0b49fe4d10..cfa3511c17 100644 --- a/Games/Snake/SnakeGame.cpp +++ b/Games/Snake/SnakeGame.cpp @@ -8,7 +8,10 @@ SnakeGame::SnakeGame(GWidget* parent) : GWidget(parent) { set_font(Font::default_bold_font()); - m_fruit_bitmap = GraphicsBitmap::load_from_file("/res/icons/snake/paprika.png"); + m_fruit_bitmaps.append(*GraphicsBitmap::load_from_file("/res/icons/snake/paprika.png")); + m_fruit_bitmaps.append(*GraphicsBitmap::load_from_file("/res/icons/snake/eggplant.png")); + m_fruit_bitmaps.append(*GraphicsBitmap::load_from_file("/res/icons/snake/cauliflower.png")); + m_fruit_bitmaps.append(*GraphicsBitmap::load_from_file("/res/icons/snake/tomato.png")); srand(time(nullptr)); reset(); } @@ -54,6 +57,7 @@ void SnakeGame::spawn_fruit() break; } m_fruit = coord; + m_fruit_type = rand() % m_fruit_bitmaps.size(); } Rect SnakeGame::score_rect() const @@ -167,10 +171,22 @@ void SnakeGame::paint_event(GPaintEvent& event) painter.fill_rect(event.rect(), Color::Black); painter.fill_rect(cell_rect(m_head), Color::Yellow); - for (auto& coord : m_tail) - painter.fill_rect(cell_rect(coord), Color::from_rgb(0xaaaa00)); + for (auto& part : m_tail) { + auto rect = cell_rect(part); + painter.fill_rect(rect, Color::from_rgb(0xaaaa00)); - painter.draw_scaled_bitmap(cell_rect(m_fruit), *m_fruit_bitmap, m_fruit_bitmap->rect()); + Rect left_side(rect.x(), rect.y(), 2, rect.height()); + Rect top_side(rect.x(), rect.y(), rect.width(), 2); + Rect right_side(rect.right() - 1, rect.y(), 2, rect.height()); + Rect bottom_side(rect.x(), rect.bottom() - 1, rect.width(), 2); + painter.fill_rect(left_side, Color::from_rgb(0xcccc00)); + painter.fill_rect(right_side, Color::from_rgb(0x888800)); + painter.fill_rect(top_side, Color::from_rgb(0xcccc00)); + painter.fill_rect(bottom_side, Color::from_rgb(0x888800)); + + } + + painter.draw_scaled_bitmap(cell_rect(m_fruit), *m_fruit_bitmaps[m_fruit_type], m_fruit_bitmaps[m_fruit_type]->rect()); painter.draw_text(score_rect(), m_score_text, TextAlignment::TopLeft, Color::White); } diff --git a/Games/Snake/SnakeGame.h b/Games/Snake/SnakeGame.h index 192b548521..ec04227335 100644 --- a/Games/Snake/SnakeGame.h +++ b/Games/Snake/SnakeGame.h @@ -50,10 +50,11 @@ private: Vector m_tail; Coordinate m_fruit; + int m_fruit_type { 0 }; int m_length { 0 }; unsigned m_score { 0 }; String m_score_text; - RetainPtr m_fruit_bitmap; + Vector> m_fruit_bitmaps; }; diff --git a/Games/Snake/main.cpp b/Games/Snake/main.cpp index 7cf72b64ff..1ae78b6bfa 100644 --- a/Games/Snake/main.cpp +++ b/Games/Snake/main.cpp @@ -13,7 +13,7 @@ int main(int argc, char** argv) auto* window = new GWindow; window->set_title("Snake"); - window->set_rect(100, 100, 300, 300); + window->set_rect(100, 100, 320, 320); auto* game = new SnakeGame; window->set_main_widget(game);