c++命令行贪吃蛇

方向键控制贪吃蛇移动

  1 #include <iostream>
  2 #include <time.h>
  3 #include <windows.h>
  4 #include <conio.h>
  5 
  6 using namespace std;
  7 
  8 #define height 25
  9 #define width 25
 10 
 11 const int eachStep = height*width;
 12 char road[height][width];
 13 
 14 void gotoxy(int x,int y)    
 15 {
 16     COORD coord;
 17     coord.X=x;
 18     coord.Y=y; 
 19     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
 20 }
 21 
 22 class board
 23 {
 24     public:
 25         //char road[height][width];
 26         long long recordTime;
 27         board();
 28         void food();
 29         void welcome();
 30         void print(int grade,int score,int gamespeed);
 31 };
 32 
 33 board::board()
 34 {
 35     srand(time(NULL));
 36     for(int i=1;i<=height-2;++i)
 37     {
 38         for(int j=1;j<=width-2;++j)
 39         {
 40             road[i][j]=' ';
 41         }
 42     }
 43     for(int i=0;i<=height-1;++i)
 44         road[i][0]=road[i][width-1]='#';
 45     for(int j=0;j<=width-1;++j)
 46         road[0][j]=road[height-1][j]='#';
 47 }
 48 
 49 void board::welcome()
 50 {
 51     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN);                                                                                                                                                                                                                              
 52     cout << "   SSSSSSSSSSSSSSS                                   kkkkkkkk" << endl;                                                   
 53     cout << " SS:::::::::::::::S                                  k::::::k" << endl;                                                   
 54     cout << "S:::::SSSSSS::::::S                                  k::::::k" << endl;                                                   
 55     cout << "S:::::S     SSSSSSS                                  k::::::k" << endl;                                                   
 56     cout << "S:::::S          nnnn  nnnnnnnn      aaaaaaaaaaaaa    k:::::k    kkkkkkk eeeeeeeeeeee  yyyyyyy           yyyyyyy" << endl;
 57     cout << "S:::::S          n:::nn::::::::nn    a::::::::::::a   k:::::k   k:::::kee::::::::::::ee y:::::y         y:::::y" << endl; 
 58     cout << " S::::SSSS       n::::::::::::::nn   aaaaaaaaa:::::a  k:::::k  k:::::ke::::::eeeee:::::eey:::::y       y:::::y" << endl;  
 59     cout << "  SS::::::SSSSS  nn:::::::::::::::n           a::::a  k:::::k k:::::ke::::::e     e:::::e y:::::y     y:::::y" << endl;   
 60     cout << "    SSS::::::::SS  n:::::nnnn:::::n    aaaaaaa:::::a  k::::::k:::::k e:::::::eeeee::::::e  y:::::y   y:::::y" << endl;    
 61     cout << "       SSSSSS::::S n::::n    n::::n  aa::::::::::::a  k:::::::::::k  e:::::::::::::::::e    y:::::y y:::::y" << endl;     
 62     cout << "            S:::::Sn::::n    n::::n a::::aaaa::::::a  k:::::::::::k  e::::::eeeeeeeeeee      y:::::y:::::y" << endl;      
 63     cout << "            S:::::Sn::::n    n::::na::::a    a:::::a  k::::::k:::::k e:::::::e                y:::::::::y" << endl;       
 64     cout << "SSSSSSS     S:::::Sn::::n    n::::na::::a    a:::::a k::::::k k:::::ke::::::::e                y:::::::y" << endl;        
 65     cout << "S::::::SSSSSS:::::Sn::::n    n::::na:::::aaaa::::::a k::::::k  k:::::ke::::::::eeeeeeee         y:::::y" << endl;         
 66     cout << "S:::::::::::::::SS n::::n    n::::n a::::::::::aa:::ak::::::k   k:::::kee:::::::::::::e        y:::::y" << endl;          
 67     cout << " SSSSSSSSSSSSSSS   nnnnnn    nnnnnn  aaaaaaaaaa  aaaakkkkkkkk    kkkkkkk eeeeeeeeeeeeee       y:::::y" << endl;           
 68     cout << "                                                                                             y:::::y" << endl;            
 69     cout << "________________________________________________________________________________________     y:::::y" << endl;             
 70     cout << "_::::::::::::::::::::::_::::::::::::::::::::::_::::::::::::::::::::::_::::::::::::::::::   y:::::y" << endl;              
 71     cout << "                                                                                          y:::::y" << endl;               
 72     cout << "                                                                                         yyyyyyy" << endl; 
 73     //cout << "____________________________________________________________________________________________________________________" << endl;
 74     //cout << "_::::::::::::::::::::::_::::::::::::::::::::::_::::::::::::::::::::::_::::::::::::::::::::::_::::::::::::::::::::::_" << endl;
 75     //cout << "____________________________________________________________________________________________________________________" << endl;                                                                                                       
 76     
 77     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN |FOREGROUND_BLUE);                                                                                                            
 78     
 79     //cout << "\n\n\tThe game is about to begin." << endl;
 80     
 81     recordTime = clock();
 82     while(clock()-recordTime<=4000);
 83     system("cls");
 84     
 85     for(int i=3;i>=0;--i)
 86     {
 87         recordTime = clock();
 88         while(clock()-recordTime<=1300);
 89         system("cls");
 90         if(i>0)
 91             cout << "\n\n\t\tCountDown: " << i << endl;
 92     }
 93 }
 94 
 95 void board::food()
 96 {
 97     //srand(time(NULL));
 98     int x,y;
 99     do
100     {
101         x=rand()%height;
102         y=rand()%width;
103     }while(road[x][y]!=' ');
104     road[x][y]='$';
105 }
106 
107 void board::print(int grade,int score,int gamespeed)
108 {
109     //cout << endl;
110     for(int i=0;i<height;++i)
111     {
112         //cout << "\t";
113         for(int j=0;j<width;++j)
114         {
115             cout << road[i][j];
116         }
117         if(i==height/4)
118         {
119             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN);
120             cout << "\tscore: " << score;
121             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN |FOREGROUND_BLUE);
122         }
123         if(i==height/4+2)
124         {
125             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN);
126             cout << "\tgrade: " << grade;
127             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN |FOREGROUND_BLUE);
128         }
129         if(i==height/4+4)
130         {
131             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN);
132             cout << "\tgameSpeed: " << gamespeed << "ms";
133             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN |FOREGROUND_BLUE);
134         }
135         cout << endl;
136     }
137 }
138 
139 class snake:public board
140 {
141     public:
142         int length,head,tail,grade,score,gamespeed;
143         int x,y;
144         int lastX,lastY;
145         int mark;
146         char direction;
147         char lastDirection;
148         char testButton;
149         int p[2][eachStep];
150         snake();
151         void move();
152 };
153 
154 snake::snake()
155 {
156     for(int i=1;i<4;++i)
157     {
158         road[1][i]='*';
159     }
160     road[1][4]='@';
161     lastX = 1;
162     lastY = 3;
163     for(int i=0;i<4;++i)
164     {
165         p[0][i]=1;
166         p[1][i]=i+1;
167     }
168     score = 0,head = 3,tail = 0,length = 4;
169     grade = 1,gamespeed = 500;
170     lastDirection = direction = 77;
171 }
172 
173 void snake::move()
174 {
175     while(1)
176     {
177         mark = 1;
178         recordTime = clock();
179         while((mark=((clock()-recordTime)<=gamespeed)) && !kbhit());
180         if(mark)
181         {
182             testButton = getch();
183             if(testButton != (char)224)
184             {
185                 direction = lastDirection;    
186             }
187             else
188             {
189                 direction = getch();
190             }
191         }
192         switch(direction)
193         {
194             case 77 :
195                 x = p[0][head];
196                 y = p[1][head]+1;
197                 break;
198             case 75    :
199                 x = p[0][head];
200                 y = p[1][head]-1;
201                 break;
202             case 72 :
203                 x = p[0][head]-1;
204                 y = p[1][head];
205                 break;
206             case 80:
207                 x = p[0][head]+1;
208                 y = p[1][head];
209                 break;
210             default:
211                 break;
212         }
213         if(x==lastX && y==lastY)
214         {
215             direction = lastDirection;
216             continue;
217         }
218         else
219         {
220             lastX = p[0][head];
221             lastY = p[1][head];
222             lastDirection = direction;
223         }
224         if((x==0 || y==0 || x==height-1 || y==width-1) || (road[x][y]!=' ' && road[x][y]!='$'))
225         {
226             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
227             gotoxy(width+6,height-12);
228             cout << "   ___   _   __  __ ___    _____   _____ ___ " << endl;  
229             gotoxy(width+6,height-11);
230             cout << "  / __| /_\\ |  \\/  | __|  / _ \\ \\ / / __| _ \\" << endl; 
231             gotoxy(width+6,height-10);
232             cout << " | (_ |/ _ \\| |\\/| | _|  | (_) \\ V /| _||   /" << endl;
233             gotoxy(width+6,height-9);
234             cout << "  \\___/_/ \\_\\_|  |_|___|  \\___/ \\_/ |___|_|_\\" << endl; 
235             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN |FOREGROUND_BLUE);
236             gotoxy(0,height);
237             break;
238         }
239         if(road[x][y]=='$')
240         {
241             ++length;
242             score+=100;
243             if(length==8)
244             {
245                 ++grade;
246                 length-=8;
247                 if(gamespeed>=200)
248                     gamespeed = 550-grade*50;        
249             }
250             road[x][y]='@';
251             road[p[0][head]][p[1][head]] = '*';
252             head = (head+1)%eachStep;
253             p[0][head]=x;
254             p[1][head]=y;
255             food();
256             system("cls");
257             print(grade,score,gamespeed);    
258         }
259         else
260         {
261             gotoxy(y,x);
262             cout << '@';
263             road[x][y]='@';
264             gotoxy(p[1][tail],p[0][tail]);
265             cout << ' ';
266             road[p[0][tail]][p[1][tail]] = ' ';
267             tail = (tail+1)%eachStep;
268             gotoxy(p[1][head],p[0][head]);
269             cout << '*';            
270             road[p[0][head]][p[1][head]] = '*';
271             head = (head+1)%eachStep;
272             p[0][head] = x;
273             p[1][head] = y;
274             //print(grade,score,gamespeed);
275         }
276     }
277 }
278 
279 int main()
280 {
281     board Board;
282     snake Snake;
283     Board.welcome();
284     Board.food();
285     Board.print(Snake.grade,Snake.score,Snake.gamespeed);
286     Snake.move();
287     return 0;
288 } 
View Code

猜你喜欢

转载自www.cnblogs.com/kachunyippp/p/10254514.html