-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.h
170 lines (148 loc) · 4.53 KB
/
Model.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef MODEL_H
#define MODEL_H
#include <QtWidgets>
#include <time.h>
class Model : public QObject {
Q_OBJECT
public:
Model(size_t cell, size_t w, size_t h);
~Model();
enum GameStatus {
STOPPED,
STARTED,
PAUSED
};
enum ObjectType {
SNAKE_PART,
FOOD,
BONUS,
WALL
};
/* class GameField {
public:
bool isOccupied(size_t i, size_t j) {
return gameField.at(i).at(j);
}
private:
QVector<QVector<bool> > gameField;
void setGameObject(GameObject obj) {
gameField.at(obj.getPosition().y()).at(obj.getPosition().x()) = true;
}
};*/
private:
class GameObject {
public:
GameObject(ObjectType objectType): gameType(objectType), cell(0, 0) { }
ObjectType gameType;
QPoint getCell() { return cell; }
protected:
QPoint cell;
bool isCollapsed(GameObject *obj1, GameObject *obj2) {
if (obj1->cell == obj2->cell) return true;
return false;
}
};
class Bonus;
class SnakePart;
class Wall;
/*class Collapsed {
public:
Collapsed(GameObject *obj);
GameObject *collapsedWith(QVector<GameObject *> listObj);
private:
GameObject *obj;
bool isCollapsed(GameObject *obj1, GameObject *obj2);
};*/
class Food : public GameObject {
public:
Food(Model *model) : GameObject(FOOD), addScore(100), model(model) { }
size_t addScore;
bool isCollides(Bonus *bonus, QVector<SnakePart *> *snake, QVector<Wall *> *walls);
void setCell(QPoint const& cell) {
this->cell = cell;
emit model->updateFood(cell);
}
private:
Model *model;
};
class Bonus : public GameObject {
public:
Bonus(Model *model) : GameObject(BONUS), addScore(0), data("0"), model(model) {
setCell(QPoint(-1, -1));
}
size_t addScore;
QString data;
bool isCollides(Food *food, QVector<SnakePart *> *snake, QVector<Wall *> *walls);
void setCell(QPoint cell) {
this->cell = cell;
emit model->updateBonus(cell);
}
private:
Model * model ;//с подчеркивания все прайваты
};
class SnakePart : public GameObject {
public:
SnakePart(Model *model) : GameObject(SNAKE_PART), number(0), firstTimeUpdate(true), model(model) { }
size_t number;
ObjectType collidesWith(Food *food, Bonus *bonus, QVector<SnakePart *> *snake, QVector<Wall *> *walls);
void setCell(QPoint cell) {
this->cell = cell;
firstTimeUpdate = false;
emit model->updateSnake(firstTimeUpdate, cell);
}
private:
bool firstTimeUpdate;
Model *model;
};
class Wall : public GameObject {
public:
Wall(int x, int y, Model *model) : GameObject(WALL), model(model) {
cell = QPoint(x, y);
emit model->updateWall(cell);
}
private:
Model *model;
};
void addGameElement(ObjectType gameElement);
void removeGameElement(ObjectType gameElement);
void initWalls();
void initSnake();
const size_t NORMAL_SNAKE_SPEED = 750;
const size_t FAST_SNAKE_SPEED = 250;
const size_t SLOW_SNAKE_SPEED = 1250;
size_t score, ate, bonusAte, life;
size_t bonusTiks, bonusVisibleTimeLeft, snakeSpeed;
int idGameTimer;
size_t cellSize, cellsW, cellsH;
bool isBonusVisible, haveMooved;
QPoint zeroSnakePos;
GameStatus gameStatus;
int previousVector, presentVector;
QVector<SnakePart*> snake;
QVector<Wall*> walls;
Food food;
Bonus bonus;
QBasicTimer gameTimer;
public slots:
void keyPressed(QKeyEvent *keyEvent);
void startGame();
void pauseGame();
void resumeGame();
void finishGame();
protected:
virtual void timerEvent(QTimerEvent *timer);
signals:
void updateScore(size_t);
void updateAte(size_t);
void updateBonusAte(size_t);
void updateLife(size_t);
void changeGameAreaSize(size_t, size_t);
void updateBonusTimeLeft(size_t);
void showBonusTimeLeft(QString);
void finishGame(size_t);
void updateFood(QPoint);
void updateBonus(QPoint);
void updateSnake(size_t, QPoint);
void updateWall(QPoint);
};
#endif // MODEL_H