This repository has been archived by the owner on Nov 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SnakeGame.cpp
326 lines (299 loc) · 8.62 KB
/
SnakeGame.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <fstream>
#include <iostream>
#include <windows.h>
#include <conio.h>
#include "Classes.h" //included <vector>
#include "GraphicAPI.h"
#include "Sound.h"
#include "Screens.h"
using namespace std;
SnakeGame::SnakeGame(){
//initGraphic();
srand(GetTickCount());
this->setSecureKey(rand()%10 + 1);
}
SnakeGame::~SnakeGame(){
gameOverScreen();
//closegraph();
}
inline void SnakeGame::setZone(int _x, int _y, int _val){
this->zone[_y][_x] = _val;
if ( !this->gameOver )
this->drawPoint(_x, _y);
}
inline void SnakeGame::setZone(point _pos, int _val){
this->zone[_pos.y][_pos.x] = _val;
if ( !this->gameOver )
this->drawPoint(_pos.x, _pos.y);
}
inline int SnakeGame::getZone(int _x, int _y){
return this->zone[_y][_x];
}
inline int SnakeGame::getZone(point _pos){
return this->zone[_pos.y][_pos.x];
}
int SnakeGame::getScore(){
return this->score;
}
void SnakeGame::setScore(int _score){
this->score = _score;
this->drawScoreValue();
}
void SnakeGame::setSecureKey(int _val){
this->secureKey = _val;
}
int SnakeGame::setGameSpeed(int _gameSpeed){
if (_gameSpeed>=20 && _gameSpeed<=150)
this->gameSpeed = _gameSpeed;
}
int SnakeGame::getGameSpeed(){
return this->gameSpeed;
}
void SnakeGame::getKey2ChangeDirection(){
SnakeDirection _snakeDirection = this->snake->getDirection();
if (kbhit()) //Keyboard Hit
{
int kbKey = getch();
switch (kbKey)
{
case 'a':
case 'A':
_snakeDirection = LEFT;
break;
case 's':
case 'S':
_snakeDirection = DOWN;
break;
case 'd':
case 'D':
_snakeDirection = RIGHT;
break;
case 'w':
case 'W':
_snakeDirection = UP;
break;
case ',':
this->setGameSpeed(this->getGameSpeed()+10);
break;
case '.':
this->setGameSpeed(this->getGameSpeed()-10);
break;
}
if (kbKey == 0 || kbKey == 224)
{
char tempKB = getch();
switch (tempKB)
{
case KEY_LEFT:
_snakeDirection = LEFT;
break;
case KEY_DOWN:
_snakeDirection = DOWN;
break;
case KEY_RIGHT:
_snakeDirection = RIGHT;
break;
case KEY_UP:
_snakeDirection = UP;
break;
}
}
this->snake->setDirection(_snakeDirection);
}
}
void SnakeGame::snakeMove(){
point currentPos = snake->getPosition();
switch (snake->getDirection())
{
case UP:
snake->setPosition(currentPos.x, currentPos.y - 1);
break;
case DOWN:
snake->setPosition(currentPos.x, currentPos.y + 1);
break;
case LEFT:
snake->setPosition(currentPos.x - 1, currentPos.y);
break;
case RIGHT:
snake->setPosition(currentPos.x + 1, currentPos.y);
break;
}
}
void SnakeGame::drawScreen(){
for (int i=1; i<playZoneH; i++)
for (int j=1; j<playZoneW; j++)
this->drawPoint(j,i);
}
void SnakeGame::drawPoint(int x, int y){
switch (this->getZone(x, y)) // zone[i][j])
{
case ZONE_VOID: // None
drawBlock(scrX + x -1, scrY + y-1, SOLID_FILL, BLACK);
break;
case ZONE_WALL: // Danger block
drawBlock(scrX + x-1, scrY + y-1, SOLID_FILL, RED);
break;
case ZONE_HEAD: // Head
drawBlock(scrX + x-1, scrY + y-1, SOLID_FILL, RGB(0,155,155));
break;
case ZONE_TAIL: // Tail
drawBlock(scrX + x-1, scrY + y-1, SOLID_FILL, GREEN);
break;
case ZONE_FOOD: // Food
drawBlock(scrX + x-1, scrY + y-1, SOLID_FILL, YELLOW);
break;
}
}
void SnakeGame::drawBorder(){
for (int i=0; i<5; i++)
rectangle(scrX * unitLength - i,
scrY * unitLength - i,
(scrX + playZoneW -1 ) * unitLength + i,
(scrY + playZoneH -1 ) * unitLength + i);
}
void SnakeGame::drawScoreBoard(){
for (int i=0; i<5; i++)
rectangle( (scrX + playZoneW + 1) * unitLength - i,
(scrY + 0) * unitLength - i,
(scrX + playZoneW + 7 ) * unitLength + i,
(scrY + 2) * unitLength + i);
}
void SnakeGame::drawScoreValue(){
char Score_Str[7];
sprintf(Score_Str,"%d", this->score);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(BOLD_FONT, HORIZ_DIR, 4);
outtextxy((scrX + playZoneW + 3.6) * unitLength + 10,
(scrY + 1.2) * unitLength,
Score_Str);
}
void SnakeGame::loadMap(char *_dir){
ifstream inFile;
inFile.open(_dir);
char ch;
for (int r=0; r<playZoneH+1; r++)
{
for (int c=0; c<playZoneW+1; c++)
{
inFile >> ch;
if (ch == '#' && ch != '\n')
this->setZone(c, r, 1);
}
}
inFile.close();
#ifdef DEBUG
for (int r=0; r<playZoneH+1; r++)
{
for (int c=0; c<playZoneW+1; c++)
{
cout << getZone(c,r);
}
cout << endl;
}
#endif // DEBUG
}
void SnakeGame::beginGame(){
cleardevice();
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(BOLD_FONT, HORIZ_DIR, 6);
getch();
outtextxy(W/2, H/2 - 170,"INSTRUCTION");
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 4);
outtextxy(W/2, H/2 - 50, "THIS IS A VERY SIMPLE GAME");
outtextxy(W/2, H/2 - 0, "EAT EVERYTHING");
outtextxy(W/2, H/2 + 50, "EXCEPT ITS TAIL");
outtextxy(W/2, H/2 + 100,"AND THE WALL");
getch();
cleardevice();
outtextxy(W/2, H/2 - 100,"USE");
outtextxy(W/2, H/2 - 50, "W, A, S, D");
outtextxy(W/2, H/2 - 0, "OR");
outtextxy(W/2, H/2 + 50, "UP, DOWN, LEFT, RIGHT");
outtextxy(W/2, H/2 + 100,"TO CONTROL THE HUNGRY SNAKE");
outtextxy(W/2, H/2 + 150,"PRESS < OR > TO CHANGE GAME SPEED");
getch();
cleardevice();
outtextxy(W/2, H/2 - 0, "THE GAME BEGIN");
Sleep(1000);
stopSound(); //stop all sound
playSound("soundtrack\\Despacito.wav",1);
cleardevice(); //remove screen
this->drawBorder();
this->drawScreen();
this->drawScoreBoard();
this->drawScoreValue();
this->gameOver = false;
this->foodSpawn(); // first food spawn
this->gameThread();
//game over
delete this;
}
void SnakeGame::gameThread(){
while (!gameOver)
{
this->getKey2ChangeDirection();
point oldPos = this->snake->getPosition();
this->snake->move(); // move head to other position
this->logic();
this->saveTail(oldPos);
this->setZone(this->snake->getPosition(), ZONE_HEAD); // make new head in Zone
this->makeTail();
delay(this->getGameSpeed()); // delay each of game frame
}
}
void SnakeGame::logic(){
point _pos = this->snake->getPosition();
int zoneStatus = this->getZone(_pos);
if (zoneStatus == ZONE_FOOD) //EAT FOOD
{
this->setScore(this->getScore() + 10);
this->foodSpawn();
this->snake->setTailLen(this->snake->getTailLen()+1);
}
else
if (zoneStatus == ZONE_WALL || zoneStatus == ZONE_TAIL)
{
this->gameOver = true;
playSound("soundtrack\\hit_sound2.wav");
//Dead effect
cleardevice();
Sleep(100);
this -> drawScreen();
this -> drawBorder();
Sleep(300);
cleardevice();
Sleep(100);
this -> drawScreen();
this -> drawBorder();
Sleep(300);
cleardevice();
//
Sleep(1000);
}
}
void SnakeGame::foodSpawn(){
point _snakePos = this->snake->getPosition();
point _foodPos;
do
{
srand(_snakePos.x + _snakePos.y + _foodPos.x + _foodPos.y + GetTickCount()); //random seed {updated by HTML 31/7/17}
_foodPos.x = 1 + rand() % (playZoneW-1);
_foodPos.y = 1 + rand() % (playZoneH-1);
}
while ( this->getZone(_foodPos) != 0); // || (foodX==snakeX && foodY==snakeY));
this->setZone(_foodPos, ZONE_FOOD);
}
void SnakeGame::saveTail(point newTail){
this->snake->tails.insert(this->snake->tails.begin(), newTail);
//deleteTail
while (this->snake->tails.size() > this->snake->getTailLen())
{
vector<point> snakeTails = this->snake->getTails();
this->setZone(snakeTails[snakeTails.size()-1], ZONE_VOID);
this->snake->popTails();
}
}
void SnakeGame::makeTail(){
for (int i=0; i < this->snake->tails.size(); i++)
this->setZone(this->snake->tails[i], ZONE_TAIL);
}