1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:17:35 +00:00

Snake: Flesh out a basic snake game :^)

This commit is contained in:
Andreas Kling 2019-04-20 03:24:50 +02:00
parent eca9494adf
commit e24e486714
6 changed files with 196 additions and 9 deletions

View file

@ -4,8 +4,43 @@
class SnakeGame : public GWidget {
public:
explicit SnakeGame(GWidget* parent);
explicit SnakeGame(GWidget* parent = nullptr);
virtual ~SnakeGame() override;
void reset();
private:
virtual void paint_event(GPaintEvent&) override;
virtual void keydown_event(GKeyEvent&) override;
virtual void timer_event(CTimerEvent&) override;
struct Coordinate {
int row { 0 };
int column { 0 };
bool operator==(const Coordinate& other) const
{
return row == other.row && column == other.column;
}
};
void game_over();
void spawn_fruit();
bool is_available(const Coordinate&);
int m_rows { 20 };
int m_columns { 20 };
int m_horizontal_velocity { 1 };
int m_vertical_velocity { 0 };
int m_last_horizontal_velocity { 1 };
int m_last_vertical_velocity { 0 };
Coordinate m_head;
Vector<Coordinate> m_tail;
Coordinate m_fruit;
int m_length { 0 };
};