C++控制台应用程序之贪吃蛇(改进版)

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<time.h>
  5 #include<conio.h>
  6 #include<windows.h>
  7 using namespace std;
  8 
  9 typedef struct{ int x, y; }Point;
 10 
 11 char map[22][22];                        //定义一个22*22的地图(含边界)
 12 Point snake[400], food, Next;            //定义蛇、食物、下一步蛇头的位置
 13 int head, tail;                            //用于储存蛇头和蛇尾的下标
 14 int grade, length, autotime;            //游戏等级、蛇长、自动前进所需时间
 15 char direction;                            //前进方向
 16 
 17 //定位光标
 18 void gotoxy(int x, int y)
 19 {
 20     HANDLE hConsoleOutput;
 21     COORD dwCursorPosition;
 22     dwCursorPosition.X = x;
 23     dwCursorPosition.Y = y;
 24     hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
 25     SetConsoleCursorPosition(hConsoleOutput, dwCursorPosition);
 26 }
 27 
 28 //用inline定义内联函数节省程序运行时的调用开销
 29 //刷新地图
 30 inline void Update(char map[][22], int grade, int length, int autotime)
 31 {
 32     //system("cls");                        //清屏
 33     gotoxy(0, 0);
 34     int i, j;
 35     printf("\n");
 36     for (i = 0; i < 22; i++)
 37     {
 38         printf("\t");
 39         for (j = 0; j < 22; j++)
 40             printf("%c ", map[i][j]);
 41         if (i == 0)
 42             printf("\t等级为:%d", grade);
 43         if (i == 2)
 44             printf("\t长度为:%d", length);
 45         if (i == 6)
 46             printf("\t自动前进时间");
 47         if (i == 8)
 48             printf("\t间隔为:%d ms", autotime);
 49         printf("\n");
 50     }
 51 }
 52 
 53 //欢迎界面
 54 inline void hello()
 55 {
 56     puts("\n\n\n\t\t\t贪吃蛇游戏即将开始!");                        //准备开始
 57     double start;
 58     for (int i = 3; i >= 0; i--)
 59     {
 60         start = (double)clock() / CLOCKS_PER_SEC;                //得到程序目前为止运行的时间
 61         while ((double)clock() / CLOCKS_PER_SEC - start <= 1);    //经过1秒之后
 62         if (i > 0)
 63         {
 64             system("cls");                                        //清屏
 65             printf("\n\n\n\t\t\t进入倒计时:%d\n", i);                //倒计时
 66         }
 67         else
 68             Update(map, grade, length, autotime);                //刷新地图
 69     }
 70 }
 71 
 72 //随机生成食物位置
 73 inline void f()
 74 {
 75     srand(int(time(0)));                                        //调用种子函数
 76     do{
 77         food.x = rand() % 20 + 1;
 78         food.y = rand() % 20 + 1;
 79     } while (map[food.x][food.y] != ' ');
 80     map[food.x][food.y] = '!';                                    //食物为“!”
 81 }
 82 
 83 //初始化
 84 inline void init()
 85 {
 86     int i, j;
 87     for (i = 1; i <= 20; i++)
 88         for (j = 1; j <= 20; j++)
 89             map[i][j] = ' ';
 90     for (i = 0; i <= 21; i++)
 91         map[0][i] = map[21][i] = map[i][0] = map[i][21] = '*';    //边界
 92     map[1][1] = map[1][2] = 'O';                                //蛇身(含蛇尾)
 93     map[1][3] = '@';                                            //蛇头
 94     head = 2; tail = 0;                                            //开始时头和尾的下标
 95     snake[head].x = 1; snake[head].y = 3;                        //开始时蛇头在地图上的下标
 96     snake[tail].x = 1; snake[tail].y = 1;                        //开始时蛇尾在地图上的下标
 97     snake[1].x = 1; snake[1].y = 2;                                //开始时蛇身在地图上的下标
 98     f();                                                        //随机生成食物位置
 99     grade = 1; length = 3; autotime = 500;                        //开始的等级、长度、自动前进时间
100     direction = 77;                                                //初始的运动方向向右
101 }
102 
103 //预前进
104 inline int GO()
105 {
106     bool timeover = true;
107     double start = (double)clock() / CLOCKS_PER_SEC;            //得到程序目前为止运行的时间
108 
109 L:
110     //自动经过1秒或者等待1秒内的键盘输入
111     while ((timeover = ((double)clock() / CLOCKS_PER_SEC - start <= autotime / 1000.0)) && !_kbhit());
112     //键盘输入
113     if (timeover)
114     {
115         //_getch();
116         char d = _getch();                                    //获取方向
117         if (d != 72 && d != 80 && d != 75 && d != 77 || direction == 72 && d == 80 || direction == 80 && d == 72 || direction == 75 && d == 77 || direction == 77 && d == 75)
118             goto L;
119         else
120             direction = d;
121     }
122     switch (direction)
123     {
124     case 72:
125         Next.x = snake[head].x - 1; Next.y = snake[head].y;        //向上
126         break;
127     case 80:
128         Next.x = snake[head].x + 1; Next.y = snake[head].y;        //向下
129         break;
130     case 75:
131         Next.x = snake[head].x; Next.y = snake[head].y - 1;        //向左
132         break;
133     case 77:
134         Next.x = snake[head].x; Next.y = snake[head].y + 1;        //向右
135         break;
136     default:
137         puts("\tGame over!");                                    //按下非方向键游戏失败
138         return 0;
139     }
140     if (Next.x == 0 || Next.x == 21 || Next.y == 0 || Next.y == 21)            //撞到边界
141     {
142         puts("\tGame over!");
143         return 0;
144     }
145     if (map[Next.x][Next.y] != ' '&&!(Next.x == food.x&&Next.y == food.y))    //吃到自己
146     {
147         puts("\tGame over!");
148         return 0;
149     }
150     if (length == 400)                                                        //最长蛇长
151     {
152         puts("\tGood game!");
153         return 0;
154     }
155     return 1;
156 }
157 
158 //吃到食物
159 inline void EAT()
160 {
161     length++;                                        //长度增加1
162     int _grade = length / 10 + 1;                    //计算等级
163     if (_grade != grade)
164     {
165         grade = _grade;
166         if (autotime >= 100)
167             autotime = 550 - grade * 50;            //增加一级自动时间减短50毫秒
168     }
169     map[Next.x][Next.y] = '@';                        //蛇头位置变化
170     map[snake[head].x][snake[head].y] = 'O';        //原蛇头位置变化为蛇身
171     head = (head + 1) % 400;                        //蛇头下标加1
172     snake[head].x = Next.x; snake[head].y = Next.y;    //蛇头下标变化
173     f();                                            //随机生成食物位置
174     Update(map, grade, length, autotime);            //刷新地图
175 }
176 
177 //没吃到食物
178 inline void FAILURE()
179 {
180     map[snake[tail].x][snake[tail].y] = ' ';        //蛇尾原来的位置变成“ ”
181     tail = (tail + 1) % 400;                        //蛇尾下标加1
182     map[Next.x][Next.y] = '@';                        //蛇头位置变化
183     map[snake[head].x][snake[head].y] = 'O';        //原蛇头位置变化为蛇身
184     head = (head + 1) % 400;                        //蛇头下标加1
185     snake[head].x = Next.x;                            //蛇头下标变化
186     snake[head].y = Next.y;
187     Update(map, grade, length, autotime);            //刷新地图
188 }
189 
190 //main函数
191 int main()
192 {
193     system("color F0");
194     init();                    //初始化
195     hello();                //欢迎界面
196     while (1)
197     {
198         if (GO())            //预前进
199         {
200             if (Next.x == food.x&&Next.y == food.y)
201                 EAT();        //吃到食物
202             else
203                 FAILURE();    //没吃到食物
204         }
205         else
206             return 0;        //失败或者胜利,游戏结束
207     }
208     return 0;
209 }
210 //

 

运行结果截图:

posted @ 2015-09-01 00:36  叶建成  阅读(4756)  评论(2编辑  收藏  举报