-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.h
72 lines (52 loc) · 1.25 KB
/
snake.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef SNAKE_H
#define SNAKE_H
#include <QJsonArray>
#include <QLabel>
#include <QList>
#include <QMouseEvent>
#include <QTimer>
#include <QWidget>
#include <utility>
#include "bug.h"
#ifndef POS
#define POS std::pair<int, int>
#endif
enum DIR { UP, DOWN, RIGHT, LEFT };
class Snake : public QWidget {
Q_OBJECT
public:
explicit Snake(QWidget* parent);
~Snake();
QList<POS> getBody() const { return body_; }
QList<POS> getObstacles() const { return obstacles_; }
DIR getDir() const { return dir_; }
int getTime() const { return time_; }
POS getBug() const { return bug->getPs(); }
void restart(); // Code of shit. Is there a better approach?
bool validMove(const POS& next);
void load(QJsonArray body, QJsonArray obstacles, QJsonArray bug, DIR dir,
int time);
QTimer* timer;
bool timeToToggleObstacles;
protected:
void paintEvent(QPaintEvent* event);
void mousePressEvent(QMouseEvent* event);
void toggleObstacle(int i, int j);
private:
QList<POS> body_;
QList<POS> obstacles_;
DIR dir_;
int time_;
Bug* bug;
int afterEating_;
public slots:
void oneMove();
void dirUP();
void dirDOWN();
void dirRIGHT();
void dirLEFT();
signals:
void gameOver();
void timeFlies(int);
};
#endif // SNAKE_H