mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:37:35 +00:00
Snake: Use a vegetable icon for the fruit.
This commit is contained in:
parent
09c087177c
commit
6ef8e2df5a
3 changed files with 10 additions and 1 deletions
BIN
Base/res/icons/snake/paprika.png
Normal file
BIN
Base/res/icons/snake/paprika.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 168 B |
|
@ -1,11 +1,13 @@
|
||||||
#include "SnakeGame.h"
|
#include "SnakeGame.h"
|
||||||
#include <LibGUI/GPainter.h>
|
#include <LibGUI/GPainter.h>
|
||||||
|
#include <SharedGraphics/GraphicsBitmap.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
SnakeGame::SnakeGame(GWidget* parent)
|
SnakeGame::SnakeGame(GWidget* parent)
|
||||||
: GWidget(parent)
|
: GWidget(parent)
|
||||||
{
|
{
|
||||||
|
m_fruit_bitmap = GraphicsBitmap::load_from_file("/res/icons/snake/paprika.png");
|
||||||
srand(time(nullptr));
|
srand(time(nullptr));
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
@ -19,6 +21,7 @@ void SnakeGame::reset()
|
||||||
m_head = { m_rows / 2, m_columns / 2 };
|
m_head = { m_rows / 2, m_columns / 2 };
|
||||||
m_tail.clear_with_capacity();
|
m_tail.clear_with_capacity();
|
||||||
m_length = 2;
|
m_length = 2;
|
||||||
|
m_score = 0;
|
||||||
m_velocity_queue.clear();
|
m_velocity_queue.clear();
|
||||||
stop_timer();
|
stop_timer();
|
||||||
start_timer(120);
|
start_timer(120);
|
||||||
|
@ -83,6 +86,7 @@ void SnakeGame::timer_event(CTimerEvent&)
|
||||||
|
|
||||||
if (m_head == m_fruit) {
|
if (m_head == m_fruit) {
|
||||||
++m_length;
|
++m_length;
|
||||||
|
++m_score;
|
||||||
spawn_fruit();
|
spawn_fruit();
|
||||||
}
|
}
|
||||||
update();
|
update();
|
||||||
|
@ -141,7 +145,9 @@ void SnakeGame::paint_event(GPaintEvent& event)
|
||||||
for (auto& coord : m_tail)
|
for (auto& coord : m_tail)
|
||||||
painter.fill_rect(cell_rect(coord), Color::from_rgb(0xaaaa00));
|
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()
|
void SnakeGame::game_over()
|
||||||
|
|
|
@ -50,4 +50,7 @@ private:
|
||||||
Coordinate m_fruit;
|
Coordinate m_fruit;
|
||||||
|
|
||||||
int m_length { 0 };
|
int m_length { 0 };
|
||||||
|
unsigned m_score { 0 };
|
||||||
|
|
||||||
|
RetainPtr<GraphicsBitmap> m_fruit_bitmap;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue