-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.cpp
232 lines (204 loc) · 5.28 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
#include "snake.h"
#include <cmath>
using namespace std;
void Snake::paint(XInfo &xinfo) {
list<SnakeBlock *>::const_iterator begin = this->body->begin();
list<SnakeBlock *>::const_iterator end = this->body->end();
while( begin != end ) {
SnakeBlock *b = *begin;
b->paint(xinfo);
begin++;
}
/*
for (int k = 0; k < length; ++k) {
XFillRectangle(xinfo.display, xinfo.pixmap, xinfo.gc[0], x - (k * blockSize), y, blockSize, blockSize);
}*/
}
void Snake::unPaint(XInfo &xinfo) {
list<SnakeBlock *>::const_iterator begin = this->body->begin();
list<SnakeBlock *>::const_iterator end = this->body->end();
while( begin != end ) {
SnakeBlock *b = *begin;
b->unPaint(xinfo);
begin++;
}
/*
for (int k = 0; k < length; ++k) {
XFillRectangle(xinfo.display, xinfo.pixmap, xinfo.gc[1], x - (k * blockSize), y, blockSize, blockSize);
}*/
}
// true while still alive
bool Snake::move(XInfo &xinfo) {
unPaint(xinfo);
int deltaX = 0;
int deltaY = 0;
switch ( direction ) {
case Up:
deltaY = -moveFactor;
break;
case Down:
deltaY = moveFactor;
break;
case Left:
deltaX = -moveFactor;
break;
case Right:
deltaX = moveFactor;
break;
}
list<SnakeBlock *>::const_iterator begin = this->body->begin();
list<SnakeBlock *>::const_iterator end = this->body->end();
int lastX = (*begin)->getX();
int lastY = (*begin)->getY();
(*begin)->setX(lastX + deltaX);
(*begin)->setY(lastY + deltaY);
++begin;
while( begin != end ) {
SnakeBlock *b = *begin;
int curX = b->getX();
int curY = b->getY();
b->setX(lastX);
b->setY(lastY);
lastX = curX;
lastY = curY;
begin++;
}
didEatFruit(xinfo);
return !this->didHitObstacle();
}
int Snake::getX() {
return this->body->front()->getX();
}
int Snake::getY() {
return this->body->front()->getY();
}
/*
* ** ADD YOUR LOGIC **
* Use these placeholder methods as guidance for implementing the snake behaviour.
* You do not have to use these methods, feel free to implement your own.
*/
void Snake::didEatFruit(XInfo &xInfo) {
if ((abs(this->getX() - this->fruit->x) <= blockSize) && (abs(this->getY() - this->fruit->y) <= blockSize)) {
this->fruit->regenerate(xInfo);
int newX = this->body->back()->getX();
int newY = this->body->back()->getY();
//cout << newX << endl;
//cout << newY << endl;
switch ( direction ) {
case Up:
newY += blockSize;
break;
case Down:
newY -= blockSize;
break;
case Left:
newX += blockSize;
break;
case Right:
newX -= blockSize;
break;
}
//cout << newX << endl;
//cout << newY << endl;
body->push_back(new SnakeBlock(newX, newY));
//++this->length;
this->updateScore(blockSize, xInfo);
}
}
void Snake::updateScore(int scoreToAdd, XInfo &xInfo) {
this->score->unPaint(xInfo);
this->score->addScore(scoreToAdd);
this->score->paint(xInfo);
}
bool Snake::didHitObstacle() {
// screen bound
if ((this->getX() <= 0) || (this->getY() <= 0) || (this->getX() >= width) || (this->getY() >= height)) {
return true;
}
// East self
list<SnakeBlock *>::const_iterator begin = this->body->begin();
list<SnakeBlock *>::const_iterator end = this->body->end();
int headX = (*begin)->getX();
int headY = (*begin)->getY();
++begin;
//++begin;
while( begin != end ) {
SnakeBlock *b = *begin;
int curX = b->getX();
int curY = b->getY();
switch (direction) {
case Up:
if ((curY < headY) &&
((curY - headY) <= 0) &&
((curY - headY) >= -moveFactor) &&
(abs(curX - headX) <= moveFactor))
return true;
break;
case Down:
if ((curY > headY) &&
((headY - curY) <= 0) &&
((headY - curY) >= -moveFactor) &&
(abs(curX - headX) <= moveFactor))
return true;
break;
case Left:
if ((curX < headX) &&
((curX - headX) <= 0) &&
((curX - headX) >= -moveFactor) &&
(abs(curY - headY) <= moveFactor))
return true;
break;
case Right:
if ((curX > headX) &&
((headX - curX) <= 0) &&
((headX - curX) >= -moveFactor) &&
(abs(curY - headY) <= moveFactor))
return true;
break;
}
/*cout << "headX: " << headX << endl;
cout << "headY: " << headY << endl;
cout << "curX: " << curX << endl;
cout << "curY: " << curY << endl;*/
begin++;
}
return false;
}
void Snake::turnLeft() {
if (direction == Up) direction = Left;
else --direction;
}
void Snake::turnRight() {
if (direction == Left) direction = Up;
else ++direction;
}
Snake::Snake(int x, int y, Fruit *fruit, Score *score): fruit(fruit) {
direction = Right;
speed = 5;
blockSize = 15;
moveFactor = blockSize * (((float)speed / (float)10) + 1);
body = new list<SnakeBlock *>();
for (int k = 0; k < 5; ++k) {
body->push_back(new SnakeBlock(x - (k * blockSize), y));
// cout << "curX: " << this->body->back()->getX() << endl;
//cout << "curY: " << this->body->back()->getY() << endl;
}
this->score = score;
}
void Snake::setSpeed(int speed) {
this->speed = speed;
this->moveFactor = this->blockSize * (((float)this->speed / (float)10) + 1);
}
int Snake::getSpeed() {
return this->speed;
}
Snake::~Snake() {
list<SnakeBlock *>::const_iterator begin = this->body->begin();
list<SnakeBlock *>::const_iterator end = this->body->end();
while( begin != end ) {
SnakeBlock *b = *begin;
delete(b);
begin++;
}
delete(this->body);
}