贪吃蛇

  1 #include<iostream>
  2 #include<windows.h>
  3 #include<conio.h>
  4 #include<time.h>
  5 #include<stdlib.h>
  6 using namespace std;
  7 const int N=21;
  8 void Get_xy(int x,int y)   //定位光标位置
  9 {
 10     HANDLE hout;
 11     COORD pos;
 12     pos.X=x*2;
 13     pos.Y=y;
 14     hout=GetStdHandle(STD_OUTPUT_HANDLE);
 15     SetConsoleCursorPosition(hout,pos);
 16 }
 17 
 18 void Color(int num)   //设置颜色
 19 {
 20     HANDLE hout;
 21     hout=GetStdHandle(STD_OUTPUT_HANDLE);
 22     SetConsoleTextAttribute(hout,num);
 23 }
 24 
 25 void Initial()   //初始化
 26 {
 27     int i,j;
 28     int wall[N+2][N+2]={{0}};
 29     for(i=1;i<=N;i++)
 30         for(j=1;j<=N;j++)
 31             wall[i][j]=1;
 32     Color(11);
 33     for(i=0;i<N+2;i++)
 34     {
 35         for(j=0;j<N+2;j++)
 36         {
 37             if(wall[i][j])
 38                 cout<<"";
 39             else cout<<"";
 40         }
 41         cout<<endl;
 42     }
 43     Get_xy(N+3,1); Color(20);
 44     cout<<"按'W','S','A','D'进行操作"<<endl;
 45     Get_xy(N+3,2); Color(20);
 46     cout<<"按任意键暂停"<<endl;
 47     Get_xy(N+3,3); Color(20);
 48     cout<<"得分:"<<endl;
 49 }
 50 
 51 void game()
 52 {
 53     int** snake=NULL;
 54     int len=1;
 55     int i;
 56     int score=0;
 57     int apple[2];
 58     int tail[2];
 59     char ch='p';
 60     Initial();
 61     srand((unsigned)time(NULL));
 62     apple[0]=rand()%N+1;
 63     apple[1]=rand()%N+1;
 64     Get_xy(apple[0],apple[1]);
 65     Color(12);
 66     cout<<""<<endl;
 67     snake=(int**)realloc(snake,sizeof(int*)*len);
 68     for(i=0;i<len;i++)
 69         snake[i]=(int*)malloc(sizeof(int)*2);
 70     snake[0][0]=N/2;
 71     snake[0][1]=N/2+1;
 72     Get_xy(snake[0][0],snake[0][1]); Color(14);
 73     cout<<""<<endl;
 74     int flag=1;
 75     while(1)
 76     {
 77         if(flag)
 78         {
 79         tail[0]=snake[len-1][0];
 80         tail[1]=snake[len-1][1];
 81         Get_xy(tail[0],tail[1]);
 82         Color(11);
 83         cout<<""<<endl;
 84         }
 85         flag=1;
 86         for(i=len-1;i>0;i--)
 87         {
 88             snake[i][0]=snake[i-1][0];
 89             snake[i][1]=snake[i-1][1];
 90             Get_xy(snake[i][0],snake[i][1]);
 91             Color(14);
 92             cout<<""<<endl;
 93         }
 94         if(kbhit())
 95         {
 96             Get_xy(0,N+3);
 97             ch=getche();
 98         }
 99         switch(ch)
100         {
101         case 'W':
102         case 'w': snake[0][1]--; break;
103         case 'S':
104         case 's': snake[0][1]++; break;
105         case 'A':
106         case 'a': snake[0][0]--; break;
107         case 'D':
108         case 'd': snake[0][0]++; break;
109         default :break;
110         }
111         for(i=1;i<len;i++)
112         {
113             if(snake[0][0]==snake[i][0] && snake[0][1]==snake[i][1])
114             {
115                 Get_xy(N/2,N/2); Color(30);
116                 cout<<"Game over!"<<endl;
117                 exit(0);
118             }
119         }
120         Get_xy(snake[0][0],snake[0][1]);
121         Color(14); cout<<""<<endl;
122         Sleep(abs(200-0.5*score));
123         if(snake[0][0]==apple[0] && snake[0][1]==apple[1])
124         {
125             flag=0; score++; len++;   srand((unsigned)time(NULL));
126             snake=(int**)realloc(snake,sizeof(int*)*len);
127             snake[len-1]=(int*)malloc(sizeof(int)*2);
128             Get_xy(N+6,3); Color(20); cout<<score<<endl;
129             apple[0]=rand()%N+1; apple[1]=rand()%N+1;
130             Get_xy(apple[0],apple[1]);
131             Color(12);
132             cout<<""<<endl;
133         }
134         if(snake[0][0]==0 || snake[0][0]==N || snake[0][1]==0 || snake[0][1]==N)
135         {
136             Get_xy(N/2,N/2); Color(30);
137             cout<<"Game Over!"<<endl;
138             for(i=0;i<len;i++)
139                 free(snake[i]);
140             Sleep(INFINITE);
141             exit(0);
142         }
143         if(len>=N*N/20)
144         {
145             Get_xy(N/2,N/2); Color(30);
146             cout<<"Win!"<<endl;
147             for(i=0;i<len;i++)
148                 free(snake[i]);
149             Sleep(INFINITE);
150             exit(0);
151         }
152     }
153 }
154 int main()
155 {
156     game();
157     return 0;
158 }
View Code
原文地址:https://www.cnblogs.com/zsj-93/p/3149960.html