2019-04-20 01:54:10 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-20 03:44:01 +02:00
|
|
|
#include <AK/CircularQueue.h>
|
2019-06-27 13:46:31 +02:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <LibGUI/GWidget.h>
|
2019-04-20 01:54:10 +02:00
|
|
|
|
|
|
|
class SnakeGame : public GWidget {
|
|
|
|
public:
|
2019-04-20 03:24:50 +02:00
|
|
|
explicit SnakeGame(GWidget* parent = nullptr);
|
|
|
|
virtual ~SnakeGame() override;
|
|
|
|
|
|
|
|
void reset();
|
2019-04-20 01:54:10 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void paint_event(GPaintEvent&) override;
|
2019-04-20 03:24:50 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-20 03:44:01 +02:00
|
|
|
struct Velocity {
|
|
|
|
int vertical { 0 };
|
|
|
|
int horizontal { 0 };
|
|
|
|
};
|
|
|
|
|
2019-04-20 03:24:50 +02:00
|
|
|
void game_over();
|
|
|
|
void spawn_fruit();
|
|
|
|
bool is_available(const Coordinate&);
|
2019-04-20 03:44:01 +02:00
|
|
|
void queue_velocity(int v, int h);
|
|
|
|
const Velocity& last_velocity() const;
|
2019-04-20 15:04:02 +02:00
|
|
|
Rect cell_rect(const Coordinate&) const;
|
|
|
|
Rect score_rect() const;
|
2019-04-20 21:24:38 +02:00
|
|
|
Rect high_score_rect() const;
|
2019-04-20 03:24:50 +02:00
|
|
|
|
|
|
|
int m_rows { 20 };
|
|
|
|
int m_columns { 20 };
|
|
|
|
|
2019-04-20 03:44:01 +02:00
|
|
|
Velocity m_velocity { 0, 1 };
|
|
|
|
Velocity m_last_velocity { 0, 1 };
|
2019-04-20 03:24:50 +02:00
|
|
|
|
2019-04-20 03:44:01 +02:00
|
|
|
CircularQueue<Velocity, 10> m_velocity_queue;
|
2019-04-20 03:24:50 +02:00
|
|
|
|
|
|
|
Coordinate m_head;
|
|
|
|
Vector<Coordinate> m_tail;
|
|
|
|
|
|
|
|
Coordinate m_fruit;
|
2019-04-20 18:50:41 +02:00
|
|
|
int m_fruit_type { 0 };
|
2019-04-20 03:24:50 +02:00
|
|
|
|
|
|
|
int m_length { 0 };
|
2019-04-20 04:00:32 +02:00
|
|
|
unsigned m_score { 0 };
|
2019-04-20 15:04:02 +02:00
|
|
|
String m_score_text;
|
2019-04-20 21:24:38 +02:00
|
|
|
unsigned m_high_score { 0 };
|
|
|
|
String m_high_score_text;
|
2019-04-20 04:00:32 +02:00
|
|
|
|
2019-06-27 13:46:31 +02:00
|
|
|
NonnullRefPtrVector<GraphicsBitmap> m_fruit_bitmaps;
|
2019-04-20 01:54:10 +02:00
|
|
|
};
|