贪吃蛇

  1 /************************贪吃蛇***********************/
  2 /**********************2012-11-20*********************/
  3 
  4 #include <iostream>
  5 #include <cstdio>
  6 #include <cstdlib>
  7 #include <ctime>
  8 #include <conio.h>
  9 #include <cmath>
 10 #include <windows.h>
 11 using namespace std;
 12 
 13 /*** 光标定位 ***/
 14 HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
 15 COORD coord;
 16 
 17 void locate(int x,int y)
 18 {
 19     coord.X=y;
 20     coord.Y=x;
 21     SetConsoleCursorPosition(hout,coord);
 22 };
 23 
 24 /*** 隐藏光标 ***/
 25 void hide()
 26 {
 27     CONSOLE_CURSOR_INFO cursor_info={1,0};
 28     SetConsoleCursorInfo(hout, &cursor_info);
 29 }
 30 
 31 /*** 生成随机数 ***/
 32 double random(double start, double end)
 33 {
 34     return start+(end-start)*rand()/(RAND_MAX + 1.0);
 35 }
 36 
 37 /*** 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 ***/
 38 int m,n;
 39 
 40 struct node
 41 {
 42     int x,y;
 43 }snake[1000];
 44 
 45 int snake_length,dir;
 46 node food;
 47 int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
 48 
 49 /*** 输出墙 ***/
 50 void print_wall()
 51 {
 52     cout << " ";
 53     for (int i=1;i<=n;i++)
 54         cout << "-";
 55     cout << endl;
 56     for (int j=0;j<=m-1;j++)
 57     {
 58         cout << "|";
 59         for (int i=1;i<=n;i++) cout << " ";
 60         cout << "|" << endl;
 61     }
 62     cout << " ";
 63     for (int i=1;i<=n;i++)
 64         cout << "-";
 65 }
 66 
 67 /*** 首次输出蛇,其中snake[0]代表头 ***/
 68 void print_snake()
 69 {
 70     locate(snake[0].x,snake[0].y);
 71     cout << "@";
 72     for (int i=1;i<=snake_length-1;i++)
 73     {
 74         locate(snake[i].x,snake[i].y);
 75         cout << "*";
 76     }
 77 }
 78 
 79 /*** 判断是否撞墙或者自撞 ***/
 80 bool is_correct()
 81 {
 82     if (snake[0].x==0 || snake[0].y==0 || snake[0].x==m+1 || snake[0].y==n+1) return false;
 83     for (int i=1;i<=snake_length-1;i++)
 84     {
 85         if (snake[0].x==snake[i].x && snake[0].y==snake[i].y) return false;
 86     }
 87     return true;
 88 }
 89 
 90 /*** 随机生成并输出食物位置 ***/
 91 bool print_food()
 92 {
 93     srand((unsigned)time(0));
 94     bool e;
 95     while (1)
 96     {
 97         e=true;
 98         int i=(int) random(0,m)+1,j=(int) random(0,n)+1;
 99         food.x=i;food.y=j;
100         for (int k=0;k<=snake_length-1;k++)
101         {
102             if (snake[k].x==food.x && snake[k].y==food.y)
103             {
104                 e=false;break;
105             }
106         }
107         if (e) break;
108     }
109     locate(food.x,food.y);
110     cout << "$";
111     return true;
112 }
113 
114 /*** 蛇的前进 ***/
115 bool go_ahead()
116 {
117     node temp;
118     bool e=false;
119     temp=snake[snake_length-1];
120     for (int i=snake_length-1;i>=1;i--)
121         snake[i]=snake[i-1];
122     snake[0].x+=direct[dir][0];
123     snake[0].y+=direct[dir][1];
124     locate(snake[1].x,snake[1].y);
125     cout << "*";
126     /*** 吃到了食物 ***/
127     if (snake[0].x==food.x && snake[0].y==food.y)
128     {
129         snake_length++;
130         e=true;
131         snake[snake_length-1]=temp;
132     }
133     /*** 输出此时蛇状态 ***/
134     if (!e)
135     {
136         locate(temp.x,temp.y);
137         cout << " ";
138     }
139     else
140         print_food();
141     locate(snake[0].x,snake[0].y);
142     cout << "@";
143     /*** 如果自撞 ***/
144     if (!is_correct())
145     {
146         system("cls");
147         cout << "You lose!" << endl << "Length: " << snake_length << endl;
148         return false;
149     }
150     return true;
151 }
152 
153 /*** 主函数 ***/
154 int main()
155 {
156     cout << "--------------------贪吃蛇---------------------" << endl;
157     cout << "请先输入两个数,表示地图大小.要求长宽均不小于10." << endl;
158     cout << "请注意窗口大小,以免发生错位.建议将窗口调为最大." << endl;
159     cout << "再选择难度.请在1-10中输入1个数,1最简单,10则最难" << endl;
160     cout << "然后进入游戏画面,以方向键控制方向.祝你游戏愉快!" << endl;
161     cout << "-----------------------------------------------" << endl;
162     cin >> m >> n;
163     if (m<10 || n<10 || m>25 || n>40)
164     {
165         cout << "ERROR" << endl;
166         system("pause");
167         return 0;
168     }
169     int hard;
170     cin >> hard;
171     if (hard<=0 || hard>100)
172     {
173         cout << "ERROR" << endl;
174         system("pause");
175         return 0;
176     }
177     /*** 数据全部初始化,包括蛇长,位置,方向 ***/
178     snake_length=5;
179     clock_t a,b;
180     char ch;
181     double hard_len;
182     for (int i=0;i<=4;i++)
183     {
184         snake[i].x=1;
185         snake[i].y=5-i;
186     }
187     dir=3;
188     /*** 输出初始地图,蛇与食物 ***/
189     system("cls");
190     hide();
191     print_wall();
192     print_food();
193     print_snake();
194     locate(m+2,0);
195     cout << "Now length: ";
196     /*** 开始游戏 ***/
197     while (1)
198     {
199         /*** 难度随长度增加而提高 ***/
200         hard_len=(double)snake_length/(double) (m*n);
201         /*** 调节时间,单位是ms ***/
202         a=clock();
203         while (1)
204         {
205             b=clock();
206             if (b-a>=(int)(400-30*hard)*(1-sqrt(hard_len))) break;
207         }
208         /*** 接受键盘输入的上下左右,并以此改变方向 ***/
209         if (kbhit())
210         {
211             ch=getch();
212             if (ch==-32)
213             {
214                 ch=getch();
215                 switch(ch)
216                 {
217                 case 72:
218                     if (dir==2 || dir==3)
219                         dir=0;
220                     break;
221                 case 80:
222                     if (dir==2 || dir==3)
223                         dir=1;
224                     break;
225                 case 75:
226                     if (dir==0 || dir==1)
227                         dir=2;
228                     break;
229                 case 77:
230                     if (dir==0 || dir==1)
231                         dir=3;
232                     break;
233                 }
234             }
235         }
236         /*** 前进 ***/
237         if (!go_ahead()) break;
238         /*** 在最后输出此时长度 ***/
239         locate(m+2,12);
240         cout << snake_length;
241     }
242     system("pause");
243     return 0;
244 }

http://blog.csdn.net/ckcz123/article/details/8204365

原文地址:https://www.cnblogs.com/forcheryl/p/3976076.html