-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.cpp
241 lines (215 loc) · 8.68 KB
/
snake.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <QPainter>
#include "food.h"
#include "snake.h"
#include "constant.h"
#include <QKeyEvent>
#include "gamecontroller.h"
#include <QMessageBox>
Snake::Snake(int &mark,QLCDNumber &monitor,Gamecontroller &ctrl,int x,int y,QColor color,int le):ctrl(ctrl),
growing(le),LengthOfSnake(le),ColorOfSnake(color),score(mark),monitor(monitor){
setFlag(QGraphicsItem::ItemIsFocusable);
setFlag(QGraphicsItem::ItemIsMovable);
//setFocus();
LengthOfSnake=le;
immortal=false;
alive=true;
PosOfSnake=QPoint(x,y);
moveDirection=NoMove;
effect=NOEFFECT;
setData(GD_Type,SNAKE);
}
void Snake::reshape(){
LengthOfSnake=DEFAULT_LENGTH;
growing=DEFAULT_LENGTH;
body.clear();
ColorOfSnake=DEFAULT_COLOR;
//while(body.size()!=1) body.pop_back();
setPos(PosOfSnake=QPoint(100,0));
setMoveDirection(Snake::NoMove);
}
QRectF Snake::boundingRect() const{
int minX = PosOfSnake.x();
int minY = PosOfSnake.y();
int maxX = PosOfSnake.x();
int maxY = PosOfSnake.y();
foreach (QPoint p, body) {
maxX = p.x() > maxX ? p.x() : maxX;
maxY = p.y() > maxY ? p.y() : maxY;
minX = p.x() < minX ? p.x() : minX;
minY = p.y() < minY ? p.y() : minY;
}
QPointF tl = mapFromScene(QPointF(minX, minY));
QPointF br = mapFromScene(QPointF(maxX, maxY));
QRectF bound = QRectF(tl.x(), // x
tl.y(), // y
br.x() - tl.x() + SNAKE_SIZE, // width
br.y() - tl.y() + SNAKE_SIZE //height
);
return bound;
}
QPainterPath Snake::shape() const{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
QPointF j=mapFromScene(PosOfSnake);
path.addEllipse(j.x(),j.y(),SNAKE_SIZE,SNAKE_SIZE);
foreach(QPointF p,body){
QPointF i=mapFromScene(p);
path.addRect(i.x(),i.y(),SNAKE_SIZE,SNAKE_SIZE);
}
return path;
}
void Snake::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->setRenderHint(QPainter::Antialiasing);
painter->save();
painter->fillPath(shape(), ColorOfSnake);
painter->restore();
}
void Snake::setMoveDirection(Direction direction){
if (moveDirection == MoveLeft && direction == MoveRight)
return;
if (moveDirection == MoveRight && direction == MoveLeft)
return;
if (moveDirection == MoveUp && direction == MoveDown)
return;
if (moveDirection == MoveDown && direction == MoveUp)
return;
moveDirection = direction;
}
void Snake::advance(int phase){
if(!phase) return;
if(moveDirection==NoMove) return;
if(growing>0){
QPoint tailPoint = PosOfSnake;
body << tailPoint;
growing -= 1;
LengthOfSnake++;
}else {
if(!body.isEmpty()) body.removeFirst();
body << PosOfSnake;
}
if(ctrl.mode!=AUTO)
switch (moveDirection) {
case MoveLeft:
PosOfSnake.rx() -= SNAKE_SIZE;//move one step
if(PosOfSnake.rx()<X_MIN) PosOfSnake.rx()=X_MAX;//deal with overflow
break;
case MoveRight:
PosOfSnake.rx() += SNAKE_SIZE;
if(PosOfSnake.rx()>X_MAX) PosOfSnake.rx()=X_MIN;
break;
case MoveUp:
PosOfSnake.ry() -= SNAKE_SIZE;
if(PosOfSnake.ry()<Y_MIN) PosOfSnake.ry()=Y_MAX;
break;
case MoveDown:
PosOfSnake.ry() += SNAKE_SIZE;
if(PosOfSnake.ry()>Y_MAX) PosOfSnake.ry()=Y_MIN;
break;
}
else{
switch (moveDirection) {
case MoveLeft:
if(body.contains(QPoint(PosOfSnake.x()-SNAKE_SIZE*2,PosOfSnake.y())))
if(!(body.contains(QPoint(PosOfSnake.x(),PosOfSnake.y()-SNAKE_SIZE))))
PosOfSnake.ry()-=SNAKE_SIZE;
else PosOfSnake.ry()+=SNAKE_SIZE;
else PosOfSnake.rx() -= SNAKE_SIZE;//if eating itself,change a direction
if(PosOfSnake.rx()<X_MIN) PosOfSnake.rx()=X_MAX;
if(ctrl.food->px==PosOfSnake.x())//head for food and avoid eating itself
if(ctrl.food->py<PosOfSnake.y())
if((body.contains(QPoint(PosOfSnake.x(),PosOfSnake.y()-SNAKE_SIZE))))
setMoveDirection(Snake::MoveDown);
else setMoveDirection(Snake::MoveUp);
else
if((body.contains(QPoint(PosOfSnake.x(),PosOfSnake.y()+SNAKE_SIZE))))
setMoveDirection(Snake::MoveUp);
else setMoveDirection(Snake::MoveDown);
break;
case MoveRight:
if(body.contains(QPoint(PosOfSnake.x()+SNAKE_SIZE*2,PosOfSnake.y())))
if(!(body.contains(QPoint(PosOfSnake.x(),PosOfSnake.y()-SNAKE_SIZE))))
setMoveDirection(Snake::MoveUp);
else PosOfSnake.rx() -= SNAKE_SIZE;
else PosOfSnake.rx() += SNAKE_SIZE;
if(PosOfSnake.rx()>X_MAX) PosOfSnake.rx()=X_MIN;
if(ctrl.food->px==PosOfSnake.x())
if(ctrl.food->py<PosOfSnake.y())
if((body.contains(QPoint(PosOfSnake.x(),PosOfSnake.y()-SNAKE_SIZE))))
setMoveDirection(Snake::MoveDown);
else setMoveDirection(Snake::MoveUp);
else
if((body.contains(QPoint(PosOfSnake.x(),PosOfSnake.y()+SNAKE_SIZE))))
setMoveDirection(Snake::MoveUp);
else setMoveDirection(Snake::MoveDown);
break;
case MoveUp:
if(body.contains(QPoint(PosOfSnake.x(),PosOfSnake.y()-SNAKE_SIZE*2)))
if(!(body.contains(QPoint(PosOfSnake.x()-SNAKE_SIZE,PosOfSnake.y()))))
PosOfSnake.rx()-=SNAKE_SIZE;
else PosOfSnake.rx()+=SNAKE_SIZE;
else PosOfSnake.ry() -= SNAKE_SIZE;
if(PosOfSnake.ry()<Y_MIN) PosOfSnake.ry()=Y_MAX;
if(ctrl.food->py==PosOfSnake.y())
if(ctrl.food->px<PosOfSnake.x())
if((body.contains(QPoint(PosOfSnake.x()-SNAKE_SIZE,PosOfSnake.y()))))
setMoveDirection(Snake::MoveRight);
else setMoveDirection(Snake::MoveLeft);
else
if((body.contains(QPoint(PosOfSnake.x()+SNAKE_SIZE,PosOfSnake.y()))))
setMoveDirection(Snake::MoveLeft);
else setMoveDirection(Snake::MoveRight);
break;
case MoveDown:
if(body.contains(QPoint(PosOfSnake.x(),PosOfSnake.y()+SNAKE_SIZE*2)))
if(!(body.contains(QPoint(PosOfSnake.x()-SNAKE_SIZE,PosOfSnake.y()))))
PosOfSnake.rx()-=SNAKE_SIZE;
else PosOfSnake.rx()+=SNAKE_SIZE;
else
PosOfSnake.ry() += SNAKE_SIZE;
if(PosOfSnake.ry()>Y_MAX) PosOfSnake.ry()=Y_MIN;
if(ctrl.food->py==PosOfSnake.y())
if(ctrl.food->px<PosOfSnake.x())
if((body.contains(QPoint(PosOfSnake.x()-SNAKE_SIZE,PosOfSnake.y()))))
setMoveDirection(Snake::MoveRight);
else setMoveDirection(Snake::MoveLeft);
else
if((body.contains(QPoint(PosOfSnake.x()+SNAKE_SIZE,PosOfSnake.y()))))
setMoveDirection(Snake::MoveLeft);
else setMoveDirection(Snake::MoveRight);
break;
}
}
setPos(PosOfSnake);
handleCollision();
}
void Snake::handleCollision(){
QList<QGraphicsItem*> l=collidingItems();
foreach(QGraphicsItem *item,l){//examine every colliding item
if(item->data(GD_Type)==FOOD||item->data(GD_Type)==REWARD)
ctrl.SnakeAteFood(this,(Food *)item);//eat food
else if(ColorOfSnake!=Qt::gray&&item->data(GD_Type)==WALL)//collide with the wall
ctrl.gameover(this);
}
if(moveDirection!=NoMove&&body.contains(PosOfSnake)&&ColorOfSnake!=Qt::gray)
ctrl.gameover(this);//eat itself
}
void Snake::moveBackward(){
body.push_back(PosOfSnake);
QPoint p=body.first();
body.pop_front();
setPos(PosOfSnake=p);
int _size=body.size();
QPoint *pptr=new QPoint[_size];
for(int i=0;i<_size;i++) {pptr[i]=body.last();body.pop_back();}
for(int i=0;i<_size;i++){body.push_back(pptr[i]);}
switch(moveDirection){
case MoveUp:moveDirection=MoveDown;break;
case MoveDown:moveDirection=MoveUp;break;
case MoveLeft:moveDirection=MoveRight;break;
case MoveRight:moveDirection=MoveLeft;break;
}
delete [] pptr;
}
void Snake::randommove(QPoint newPos){
setPos(PosOfSnake=newPos);
}