-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGo.cpp
More file actions
485 lines (412 loc) · 15.5 KB
/
Copy pathSnakeGo.cpp
File metadata and controls
485 lines (412 loc) · 15.5 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "./include/SnakeGo.h"
#include "./include/filled_thing/SnakeNode.h"
#include "./include/filled_thing/FilledThing.h"
#include "./include/filled_thing/NormalFood.h"
#include "./include/mapgo/MapGo.h"
#include "./include/mapgo/NormalMap.h"
#include "./include/Coordinate.h"
#include "./include/GameExit.h"
#include "./include/GameReadLoad.h"
#include "./include/GameLevel.h"
#include "./include/GameScoreSort.h"
#include "./include/GameMusicEffect.h"
extern vector<StageProperty> stagePropertyVector;
extern vector<FilledThing*> filledThingVector;
extern GameMusicEffect gameMusicEffectObj;
SnakeGo::SnakeGo(){
}
SnakeGo::~SnakeGo(){
snakeNodePointerVector.clear();
}
//隐藏光标防止闪烁
void hideCursor() {
CONSOLE_CURSOR_INFO cursor_info;
cursor_info.dwSize = 1;
cursor_info.bVisible = false;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void writeChar(short x, short y, const char * pStr, WORD color = 1 / 8){
COORD pos = {x, y};
HANDLE hOutStd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutStd, pos);
SetConsoleTextAttribute(hOutStd, color);
printf("%s", pStr);
hideCursor();
//ShowCursor(false);//控制光标闪烁
}
/*
* #define CONSOLE_HIGH 50
#define CONSOLE_WEIGHT 135
*/
bool touchSideOrSelf(SnakeNode& snakeNode) {
if (snakeNode.nextX == CONSOLE_WEIGHT-1 || snakeNode.nextX < 1
|| snakeNode.nextY == CONSOLE_HIGH-1 || snakeNode.nextY < 1) {
return true;
}
Coordinate coordinate = Coordinate{ snakeNode.nextX, snakeNode.nextY };
for (int index = 0; index < wholeMapVector.size(); index++) {
//在正常地图模式中如果坐标存在地图中说明触碰到了自己的身体
if (wholeMapVector[index]->mapSerialNumber == NORMAL_MAP) {
bool isInLocalArea = wholeMapVector[index]->inLocalArea(coordinate);
if (isInLocalArea) {
return true;
}
}
if (wholeMapVector[index]->mapSerialNumber == CUSTOMS_MAP) {
//如果是障碍物地图,蛇的节点碰到了障碍物,则游戏结束 不在障碍物区域里面则游戏继续进行
bool isInObstacleThing = wholeMapVector[index]->inLocalArea(coordinate);
if (isInObstacleThing) {
return true;
}
}
}
return false;
}
void putSnakeNodeToLocaleMap(Coordinate& coordinate, int flag){
for(int index = 0; index < wholeMapVector.size(); index++){
bool isInMapScope = wholeMapVector[index]->checkCoordinateScopeInLocale(coordinate);
if (isInMapScope) {
bool isInLocalArea = wholeMapVector[index]->inLocalArea(coordinate);
if (!isInLocalArea) {
wholeMapVector[index]->putFilledThingInLocalArea(&coordinate, 1);
break;
}
}
}
}
//随机生成蛇的身体不能碰到障碍物和地图边界以及自己 初始长度3
void SnakeGo::init(){
this->snakeNodePointerVector = vector<SnakeNode *>();
FilledThing filledThing = FilledThing();
Coordinate* coordinate = filledThing.generateRandomGoordinate(CONSOLE_WEIGHT, CONSOLE_HIGH);
for(int index = 0; index < INIT_SNAKE_LENGTH; index++){
SnakeNode* snakeNode = new SnakeNode();
snakeNode->currentX = coordinate->getX() + index;
snakeNode->currentY = coordinate->getY();
snakeNode->nextX = coordinate->getX()+index;
snakeNode->nextY = coordinate->getY();
if (index==0) {
SnakeNode* snakeNodeTail = new SnakeNode();
snakeNodeTail->nextX = coordinate->getX() + INIT_SNAKE_LENGTH - 1;
snakeNodeTail->nextY = coordinate->getY();
while (touchSideOrSelf(*snakeNode) || touchSideOrSelf(*snakeNodeTail)) {
Coordinate* coordinateTemp = filledThing.generateRandomGoordinate(CONSOLE_WEIGHT, CONSOLE_HIGH);
snakeNode->nextX = coordinateTemp->getX() + index;
snakeNode->nextY = coordinateTemp->getY();
snakeNodeTail->nextX = coordinateTemp->getX() + INIT_SNAKE_LENGTH - 1;
snakeNodeTail->nextY = coordinateTemp->getY();
delete coordinateTemp;
}
}
this->snakeNodePointerVector.push_back(snakeNode);
Coordinate coor = Coordinate{snakeNode->currentX, snakeNode->currentY};
putSnakeNodeToLocaleMap(coor, 1);
}
delete coordinate;
}
void SnakeGo::show(){
for(int index = 0; index < this->snakeNodePointerVector.size(); index++){
writeChar(this->snakeNodePointerVector[index]->currentX
,this->snakeNodePointerVector[index]->currentY
, "A", FOREGROUND_GREEN);
}
//自身的分数区域:
for (int index = 0; index < CONSOLE_WEIGHT; index++) {
writeChar(index, 0, "X", FOREGROUND_BLUE);
writeChar(index, CONSOLE_HIGH - 1 + 2, "X", FOREGROUND_BLUE);
}
for (int index = CONSOLE_HIGH; index < CONSOLE_HIGH + 2; index++) {
writeChar(0, index, "X", FOREGROUND_BLUE);
writeChar(CONSOLE_WEIGHT - 1, index, "X", FOREGROUND_BLUE);
}
}
void SnakeGo::showGameScore() {
//在分数区域显示 先擦除原来坐标位置上的分数 写入新的分数:
//先擦除原来的分数:
writeChar(CONSOLE_WEIGHT / 2 - 2
, CONSOLE_HIGH
, " ");
//在重新定位输出分数内容
writeChar(CONSOLE_WEIGHT / 2 - 2
, CONSOLE_HIGH
, "", FOREGROUND_GREEN);
std::string scoreStr = "SCORE: ";
std::cout << scoreStr << this->getGameScore();
hideCursor();
}
int waitKey(){
if(_kbhit()){
return _getch();
}else{
return 0;
}
}
int reverseDirection(int hitKey) {
switch (hitKey) {
case UP:
return DOWN;
break;
case DOWN:
return UP;
break;
case LEFT:
return RIGHT;
break;
case RIGHT:
return LEFT;
break;
default:
break;
}
}
void matchDirection(int hitKey, SnakeNode * current){
switch(hitKey){
case UP:
current->nextY = current->currentY -1;
current->nextX = current->currentX;
break;
case DOWN:
current->nextY = current->currentY + 1;
current->nextX = current->currentX;
break;
case LEFT:
current->nextY = current->currentY;
current->nextX = current->currentX - 1;
break;
case RIGHT:
current->nextY = current->currentY;
current->nextX = current->currentX + 1;
break;
default:
break;
}
}
bool handleCoordinate(int hitKey, SnakeNode * current, SnakeNode * next, int gameLevel){
int validKeyHit[3] = {0};
int currentDirection = 0;
if(current->currentX == next->currentX){
if(current->currentY > next->currentY){
validKeyHit[0] = DOWN;
validKeyHit[1] = LEFT;
validKeyHit[2] = RIGHT;
currentDirection = DOWN;
}else{
validKeyHit[0] = UP;
validKeyHit[1] = LEFT;
validKeyHit[2] = RIGHT;
currentDirection = UP;
}
}else{
if(current->currentX > next->currentX){
validKeyHit[0] = RIGHT;
validKeyHit[1] = UP;
validKeyHit[2] = DOWN;
currentDirection = RIGHT;
}else{
validKeyHit[0] = LEFT;
validKeyHit[1] = UP;
validKeyHit[2] = DOWN;
currentDirection = LEFT;
}
}
if (HARD_LEVEL_MODE == gameLevel) {
hitKey = reverseDirection(hitKey);
}
bool hitInValidKey = false;
for(int index = 0; index < 3; index++){
if(hitKey == validKeyHit[index]){
hitInValidKey = true;
}
}
if(!hitInValidKey){
matchDirection(currentDirection, current);
return false;
}else{
matchDirection(hitKey, current);
return true;
}
}
void SnakeGo::saveSnakeNodeVectorToFile() {
FILE* filePointer = nullptr;
fopen_s(&filePointer ,SNAKE_NODE_VECTOR_SAVE_PATH, WRITE_AND_READ_BINARY);
int snakeNodeCount = this->snakeNodePointerVector.size();
fwrite(&snakeNodeCount, sizeof(int), 1, filePointer);
for (int index = 0; index < snakeNodeCount; index++) {
fwrite(snakeNodePointerVector[index],sizeof(SnakeNode), 1, filePointer);
}
fclose(filePointer);
}
bool SnakeGo::readSnakeNodeVectorFromFile() {
FILE* filePointer = nullptr;
fopen_s(&filePointer, SNAKE_NODE_VECTOR_SAVE_PATH, READ_BINARY);
if (filePointer == nullptr) {
return false;
}
int snakeNodeCount = 0;
fread(&snakeNodeCount, sizeof(int), 1, filePointer);
for (int index = 0; index < snakeNodeCount; index++) {
SnakeNode * snakeNode = new SnakeNode();
fread(snakeNode, sizeof(SnakeNode), 1, filePointer);
this->snakeNodePointerVector.push_back(snakeNode);
}
fclose(filePointer);
return true;
}
bool SnakeGo::scanDirectionFromKeyBoard(vector<SnakeNode*> snakeNodePointerVector){
int hitKey = waitKey();
if (hitKey == 'p' || hitKey == 'P') {
std::wstring title = TEXT("SNAKE GO!");
MessageBoxW(nullptr, L"GAME PAUSE ENTER TO CONTINUE", title.c_str(), MB_SETFOREGROUND);
//MessageBoxW(nullptr, TEXT("游戏暂停,按下回车键继续"), TEXT("贪吃蛇游戏"), MB_SETFOREGROUND);
}
if (hitKey == 27) {
//用户存档
int isSave = MessageBoxW(nullptr, L"Is to save?", L"SNAKE GO!", MB_OKCANCEL | MB_SETFOREGROUND);
if (isSave == SAVE) {
GameReadLoad gameReadLoad = GameReadLoad();
gameReadLoad.gameSave(this);
}
}
return handleCoordinate(hitKey, snakeNodePointerVector[0], snakeNodePointerVector[1], this->getGameLevel());
}
void randomSetFilledThing(){
if(filledThingVector.size()==0){
NormalFood * normalFood = new NormalFood();
normalFood->init();
SnakeNode sn = SnakeNode();
sn.nextX= normalFood->getCurrentX();
sn.nextY = normalFood->getCurrentY();
bool isTouchSideOrSnake = touchSideOrSelf(sn);
while (isTouchSideOrSnake) {
normalFood->init();
sn.nextX = normalFood->getCurrentX();
sn.nextY = normalFood->getCurrentY();
isTouchSideOrSnake = touchSideOrSelf(sn);
}
filledThingVector.push_back(normalFood);
writeChar(normalFood->getCurrentX()
, normalFood->getCurrentY()
, "$", BACKGROUND_RED);
}
}
void eraseFilledThing(Coordinate coordinate){
for(int index = 0; index < wholeMapVector.size(); index++){
bool isInLocalArea = wholeMapVector[index]->inLocalArea(coordinate);
if(isInLocalArea){
wholeMapVector[index]->eraseFilledThingFromLocaleArea(coordinate);
break;
}
}
}
bool touchStageProperty(SnakeNode & snakeNode){
if(filledThingVector.size()==0){
return false;
}
if(filledThingVector[0]->getCurrentX() == snakeNode.nextX
&& filledThingVector[0]->getCurrentY() == snakeNode.nextY){
return true;
}
return false;
}
int SnakeGo::getGameLevel() {
if (this->gameLevel == -1) {
GameLevel gameLevel = GameLevel();
this->gameLevel = gameLevel.getLevelMode();
}
return this->gameLevel;
}
void SnakeGo::setGameScore(int gameScore) {
this->gameScore = gameScore;
}
int SnakeGo::getGameScore() {
if (this->gameScore==-1) {
return 0;
}
return this->gameScore;
}
bool SnakeGo::loadGameScore(std::string fileReadMode, std::string path) {
if (this->gameScore != -1) {
return false;
}
FILE* filePointer = nullptr;
fopen_s(&filePointer, path.c_str(), fileReadMode.c_str());
if (filePointer == nullptr) {
return false;
}
int score = 0;
fread(&score, sizeof(int), 1, filePointer);
this->gameScore = score;
fclose(filePointer);
return true;
}
int SnakeGo::snakeSpeedUp(int gameScore) {
if (gameScore == 0) {
return this->snakeSpeed;
}
return this->snakeSpeed - (gameScore / (SCORE_FOR_ONE_FOOD * 1)) * 15;
}
void SnakeGo::setSnakeSpeed(int speed) {
this->snakeSpeed = speed;
}
int SnakeGo::getSnakeSpeed() {
return this->snakeSpeed;
}
void SnakeGo::goGoGo() {
while(true){
int snakeSpeedMillisecond = snakeSpeedUp(this->getGameScore());
Sleep(snakeSpeedMillisecond); //默认150毫秒 每5个食物减少15毫秒
writeChar(this->snakeNodePointerVector.at(this->snakeNodePointerVector.size() - 1)->currentX
, this->snakeNodePointerVector.at(this->snakeNodePointerVector.size() - 1)->currentY, " ");
//输出填充障碍物:
randomSetFilledThing();
//这里使用键盘控制是给头节点的x还是y坐标+1
//scanDirectionFromKeyBoard(snakeNodePointerVector[0], snakeNodePointerVector[1]);
scanDirectionFromKeyBoard(snakeNodePointerVector);
//判断头节点是否触碰到边界(1.地图的边界 2.自己的身体 3.障碍物 如果触碰到则游戏结束)
bool isTouchSideOrSelf = touchSideOrSelf(*(this->snakeNodePointerVector[0]));
if(isTouchSideOrSelf){
//输出游戏结束:
gameMusicEffectObj.playBackGroundMusic(MUSIC_MODE_GAME_OVER);
writeChar(CONSOLE_WEIGHT/2-2, CONSOLE_HIGH/2-2, "GAME OVER!", FOREGROUND_GREEN);
Sleep(2000);//这里画面停一下 等待 音效完成
break;
}
//判断头节点是否触碰到障碍物(1.触碰到食物 把当前位置加到自身的头部 更新坐标)
bool isTouchStageProperty = touchStageProperty(*(snakeNodePointerVector[0]));
if(isTouchStageProperty){
filledThingVector[0]->trigger(*this);
filledThingVector.clear();
}
for(int index = 0; index < this->snakeNodePointerVector.size(); index++){
writeChar(snakeNodePointerVector[index]->nextX, snakeNodePointerVector[index]->nextY, "A", FOREGROUND_GREEN);
if(index+1 != this->snakeNodePointerVector.size()){
snakeNodePointerVector[index+1]->nextY = snakeNodePointerVector[index]->currentY;
snakeNodePointerVector[index+1]->nextX = snakeNodePointerVector[index]->currentX;
}
//将尾节点从地图中删除
if(index == this->snakeNodePointerVector.size()-1){
eraseFilledThing(Coordinate(snakeNodePointerVector[index]->currentX,snakeNodePointerVector[index]->currentY));
}
snakeNodePointerVector[index]->currentX = snakeNodePointerVector[index]->nextX;
snakeNodePointerVector[index]->currentY = snakeNodePointerVector[index]->nextY;
//将节点添加到地图中
Coordinate coordinateSnakeNode = Coordinate{snakeNodePointerVector[index]->currentX, snakeNodePointerVector[index]->currentY};
putSnakeNodeToLocaleMap(coordinateSnakeNode, 1);
}
}
if (this->gameScore>0) {
this->saveGameScore(WRITE_ADD_TO_BINARY, GAME_SCORE_FILE_PATH);
GameScoreSort gameScoreSort = GameScoreSort();
gameScoreSort.handleGameScoreFileSortAndShow(false);
}
}
void SnakeGo::saveGameScore(std::string readWriteMode, std::string path) {
FILE* filePointer = nullptr;
fopen_s(&filePointer, path.c_str(), readWriteMode.c_str());
if (filePointer == nullptr) {
return;
}
int score = this->gameScore;
fwrite(&score, sizeof(int), 1, filePointer);
fclose(filePointer);
}