Skip to content

Commit 468de2c

Browse files
committed
增加了代码注释
1 parent ca93147 commit 468de2c

File tree

1 file changed

+59
-57
lines changed

1 file changed

+59
-57
lines changed

AISnake/SnakeAI/GameMain.cpp

+59-57
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
#include <Windows.h>
88

99
#include "FindPathBFS.h"
10-
1110
using namespace std;
1211

13-
//
1412
int dir[4][2] = { { 0,1 },{ 0,-1 },{ 1,0 },{ -1,0 } };
1513

1614
#define UP 1
@@ -20,28 +18,30 @@ int dir[4][2] = { { 0,1 },{ 0,-1 },{ 1,0 },{ -1,0 } };
2018
#define HEAD 0
2119

2220
int speed = 0;
23-
21+
//将光标移动到x,y位置
2422
void gotoxy(int x, int y)
2523
{
2624
COORD c;
2725
c.X = x; c.Y = y;
2826
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
2927
}
30-
28+
//设置颜色 用到两个Windows API 不做详细介绍
3129
void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)
3230
{
3331
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//获取当前窗口句柄
3432
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);//设置颜色
3533
}
36-
34+
//游戏设置相关模块,把函数都放到一个类里面了。函数定义为static静态成员,不生成实体也可以直接调用
3735
class GameSetting
3836
{
3937
public:
38+
//游戏窗口的长宽
4039
static const int window_height = 40;
4140
static const int window_width = 80;
4241
public:
4342
static void GameInit()
4443
{
44+
//设置游戏窗口大小
4545
char buffer[32];
4646
sprintf_s(buffer, "mode con cols=%d lines=%d",window_width, window_height);
4747
system(buffer);
@@ -52,15 +52,15 @@ class GameSetting
5252
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
5353
CursorInfo.bVisible = false; //隐藏控制台光标
5454
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
55-
55+
//初始化随机数种子
5656
srand((unsigned int)time(0));
5757
}
5858
};
59-
59+
//打印信息类,打印相关信息:欢迎,分数,说明,结束等等
6060
class PrintInfo
6161
{
6262
public:
63-
63+
//选择模式:手动? AI?
6464
static void DrawChoiceInfo()
6565
{
6666
gotoxy(GameSetting::window_width / 2 - 10, GameSetting::window_height / 2 - 5);
@@ -72,7 +72,7 @@ class PrintInfo
7272
gotoxy(GameSetting::window_width / 2 - 10, GameSetting::window_height / 2 + 1);
7373
cout << "请输入您的选择-> ";
7474
}
75-
75+
//画地图边界
7676
static void DrawMap()
7777
{
7878
system("cls");
@@ -104,6 +104,7 @@ class PrintInfo
104104
cout << "#";
105105

106106
}
107+
//游戏结束
107108
static void GameOver(int score)
108109
{
109110
setColor(12, 0);
@@ -112,7 +113,7 @@ class PrintInfo
112113
gotoxy(GameSetting::window_width / 2 - 20, GameSetting::window_height / 2 - 3);
113114
cout << "您的得分为:" << score << endl;
114115
}
115-
116+
//画分数
116117
static void DrawScore(int score)
117118
{
118119
gotoxy(GameSetting::window_width - 22+14, 6);
@@ -126,7 +127,7 @@ class PrintInfo
126127
cout << "当前游戏速度: " << 10 - speed / 25 << endl;
127128

128129
}
129-
130+
//画游戏操作说明等
130131
static void DrawGameInfo(bool model)
131132
{
132133
gotoxy(GameSetting::window_width - 22, 8);
@@ -162,12 +163,12 @@ class PrintInfo
162163
}
163164

164165
};
165-
166+
//食物类,定义食物的生成等相关操作
166167
class Food
167168
{
168169
private:
170+
//食物坐标
169171
COORDINATE m_coordinate;
170-
171172
public:
172173
//坐标范围:
173174
//x: 1 to GameSetting::window_width - 30 闭区间
@@ -177,8 +178,10 @@ class Food
177178
m_coordinate.x = rand() % (GameSetting::window_width - 30) + 1;
178179
m_coordinate.y = rand() % (GameSetting::window_height - 2) + 1;
179180
unsigned int i;
181+
//原则上不允许食物出现在蛇的位置上,如果有,重新生成
180182
for (i = 0; i < coord.size(); i++)
181183
{
184+
//食物出现在蛇身的位置上。重新生成
182185
if (coord[i].x == m_coordinate.x && coord[i].y == m_coordinate.y)
183186
{
184187
m_coordinate.x = rand() % (GameSetting::window_width - 30) + 1;
@@ -187,49 +190,49 @@ class Food
187190
}
188191
}
189192
}
190-
193+
//默认构造函数
191194
Food() {}
192-
195+
//构造函数,传入参数为蛇身坐标
193196
Food(vector<COORDINATE> & coord)
194197
{
195198
RandomXY(coord);
196199
}
197-
200+
//画出食物的位置
198201
void DrawFood()
199202
{
200203
setColor(12, 0);
201204
gotoxy(m_coordinate.x, m_coordinate.y);
202205
cout << "@";
203206
setColor(7, 0);
204207
}
205-
208+
//接口,获取食物位置
206209
COORDINATE GetFoodCoordinate()
207210
{
208211
return m_coordinate;
209212
}
210213

211214
};
212-
213-
215+
//贪吃蛇类,定义贪吃蛇的移动,打印,吃食物等等
214216
//地图范围width:2 to width-2 height: 2 to height-2
215217
class Snake
216218
{
217219
private:
218220
bool m_model; //true人机 false AI
219221
int m_direction;
220222
bool m_is_alive;
223+
private: //AI功能相关
221224
bool m_chess[GameSetting::window_width - 29 + 1][GameSetting::window_height]; //AI功能用
222225
FindPathBFS m_AISnake;
223226
COORDINATE map_size;
224-
public:
227+
public://蛇身坐标
225228
vector<COORDINATE> m_coordinate;
226229

227-
public:
230+
public://默认构造函数
228231
Snake(bool model = false) : m_model(model) //默认人机模式
229232
{
230233
map_size.x = GameSetting::window_width - 29 + 1;
231234
map_size.y = GameSetting::window_height;
232-
235+
//移动方向向上
233236
m_direction = 1;
234237
m_is_alive = true;
235238
COORDINATE snake_head;
@@ -256,9 +259,9 @@ class Snake
256259
}
257260

258261
}
259-
262+
//设置游戏模式
260263
void set_model(bool m) { m_model = m; }
261-
264+
//监听键盘
262265
void listen_key_borad()
263266
{
264267
char ch;
@@ -307,7 +310,7 @@ class Snake
307310
}
308311
}
309312
}
310-
313+
//AI功能
311314
void AI_speed()
312315
{
313316
char ch;
@@ -332,7 +335,7 @@ class Snake
332335
}
333336
}
334337
}
335-
338+
//AI功能
336339
void AI_find_path(Food &f)
337340
{
338341
static int not_found = 1;
@@ -363,7 +366,7 @@ class Snake
363366
}
364367
return false;
365368
}
366-
369+
//AI功能
367370
void AI_move_snake()
368371
{
369372
static int cot = 0;
@@ -403,14 +406,13 @@ class Snake
403406

404407
m_coordinate.insert(m_coordinate.begin(), head);
405408
}
406-
409+
//移动贪吃蛇
407410
void move_snake()
408411
{
409-
412+
//监听键盘
410413
listen_key_borad();
411-
414+
//蛇头
412415
COORDINATE head = m_coordinate[0];
413-
414416
//direction方向:1 上 2 下 3 左 4 右
415417
switch (this->m_direction)
416418
{
@@ -427,26 +429,28 @@ class Snake
427429
head.x++;
428430
break;
429431
}
430-
432+
//插入移动后新的蛇头
431433
m_coordinate.insert(m_coordinate.begin(), head);
432434
}
433-
435+
//判断是否吃到食物
434436
bool is_eat_food(Food & f)
435437
{
436-
//TODO
438+
//获取食物坐标
437439
COORDINATE food_coordinate = f.GetFoodCoordinate();
440+
//吃到食物,食物重新生成,不删除蛇尾
438441
if (m_coordinate[HEAD].x == food_coordinate.x && m_coordinate[HEAD].y == food_coordinate.y)
439442
{
440443
f.RandomXY(m_coordinate);
441444
return true;
442445
}
443446
else
444447
{
448+
//没有吃到食物,删除蛇尾
445449
m_coordinate.erase(m_coordinate.end() - 1);
446450
return false;
447451
}
448452
}
449-
453+
//判断贪吃蛇死了没
450454
bool snake_is_alive()
451455
{
452456
if (m_coordinate[HEAD].x <= 0 ||
@@ -471,18 +475,20 @@ class Snake
471475

472476
return m_is_alive;
473477
}
474-
478+
//画出贪吃蛇
475479
void draw_snake()
476480
{
481+
//设置颜色为浅绿色
477482
setColor(10, 0);
478483
for (unsigned int i = 0; i < this->m_coordinate.size(); i++)
479484
{
480485
gotoxy(m_coordinate[i].x, m_coordinate[i].y);
481486
cout << "*";
482487
}
488+
//恢复原来的颜色
483489
setColor(7, 0);
484490
}
485-
491+
//清除屏幕上的贪吃蛇
486492
void ClearSnake()
487493
{
488494
for (unsigned int i = 0; i < m_coordinate.size(); i++)
@@ -493,28 +499,27 @@ class Snake
493499
cout << " ";
494500

495501
}
496-
502+
//获取贪吃蛇的长度
497503
int GetSnakeSize()
498504
{
499505
return m_coordinate.size();
500506
}
501-
507+
//获取当前游戏模式
502508
bool GetModel()
503509
{
504510
return m_model;
505511
}
506-
507-
508512
};
509513

510-
514+
//主函数,组合各种类和资源,进行游戏。
511515
int main()
512516
{
513517
GameSetting setting;
514518
PrintInfo print_info;
515519
Snake snake;
516-
520+
//初始化游戏
517521
setting.GameInit();
522+
//游戏模式选择
518523
print_info.DrawChoiceInfo();
519524

520525
char ch = _getch();
@@ -536,22 +541,23 @@ int main()
536541
}
537542
gotoxy(GameSetting::window_width / 2 - 10, GameSetting::window_height / 2 + 3);
538543
system("pause");
539-
544+
//画地图
540545
print_info.DrawMap();
541546
print_info.DrawGameInfo(snake.GetModel());
542-
547+
//生成食物
543548
Food food(snake.m_coordinate);
544-
549+
//游戏死循环
545550
while (true)
546551
{
552+
//打印成绩
547553
print_info.DrawScore(snake.GetSnakeSize());
548-
554+
//画出食物
549555
food.DrawFood();
550-
556+
//清理蛇尾,每次画蛇前必做
551557
snake.ClearSnake();
552-
558+
//判断是否吃到食物
553559
snake.is_eat_food(food);
554-
560+
//根据用户模式选择不同的调度方式
555561
if (snake.GetModel() == true)
556562
{
557563
snake.move_snake();
@@ -561,19 +567,15 @@ int main()
561567
snake.AI_find_path(food);
562568
snake.AI_move_snake();
563569
}
564-
565-
566-
570+
//画蛇
567571
snake.draw_snake();
568-
569-
572+
//判断蛇是否还活着
570573
if (!snake.snake_is_alive())
571574
{
572575
print_info.GameOver(snake.GetSnakeSize());
573576
break;
574577
}
575-
576-
578+
//控制游戏速度
577579
Sleep(speed);
578580
}
579581

0 commit comments

Comments
 (0)