贪吃蛇代码
1 #include<stdio.h> 2 #include<windows.h> 3 #include<time.h> 4 #include<conio.h> 5 #include<stdlib.h> 6 #include <iostream> 7 #include <cmath> 8 #include <cstring> 9 using namespace std; 10 struct node{ 11 int x,y; 12 node *next; 13 }; 14 15 int Life=1;//是否死亡 0代表死亡; 16 int x_food,y_food;//食物位置 17 int x_block[36],y_block[36];//障碍物的位置 18 char movedir='R';//方向 19 node* SHead=NULL;// 20 node* STail=NULL; 21 node* p=NULL; 22 int foodnum=0; 23 int score=0; 24 int level=0; 25 int death=0; 26 int speed=400; 27 28 void creatsnakemap();//创建地图 29 void creatsnake();//创建蛇 30 void gotoxy(int,int);//光标定位函数* 31 void SetColor(unsigned short);//设置颜色 32 void startgame();//开始游戏 33 void makeblock();//产生障碍物 34 void isdead();//判断是否死亡; 35 void nofood(int x,int y);//前方没有食物 36 void yesfood();//前方有食物 37 void makefood();//随机产生食物 38 void drawsnake();//画出蛇 39 void Snakedelete();//删除蛇的尾巴 40 void isfood();//判断前方是否有食物 41 void creatscreen();//打印开始屏幕 42 void ending();//结束 43 bool judge();//判断食物是否出现在蛇身体上 44 int main() 45 { 46 system("mode con cols=80 lines=35"); 47 system("color 1f"); 48 creatscreen(); 49 creatsnakemap(); 50 creatsnake(); 51 startgame(); 52 ending(); 53 while(getch()!=27) 54 continue; 55 } 56 void creatscreen() 57 { 58 SetColor(7); 59 gotoxy(31,10);printf("经典贪吃蛇"); 60 gotoxy(28,12);printf("按任意键开始游戏"); 61 gotoxy(28,30);printf("Copyright@_御心飞行"); 62 getch(); 63 system("cls"); 64 } 65 void creatsnakemap() 66 { 67 int i; 68 SetColor(3); 69 for(i=4;i<50;i+=2)//横墙 70 { 71 gotoxy(i,4); 72 printf("■"); 73 gotoxy(i,29); 74 printf("■"); 75 Sleep(30); 76 } 77 for(i=4;i<29;i++)//竖墙 78 { 79 gotoxy(4,i); 80 printf("■"); 81 gotoxy(48,i); 82 printf("■"); 83 Sleep(30); 84 } 85 SetColor(7); 86 gotoxy(58,13);printf("人头:"); 87 gotoxy(58,15);printf("等级:"); 88 gotoxy(58,19);printf("还没有穿墙技能"); 89 makeblock(); 90 } 91 void creatsnake() 92 { 93 node* newsnake; 94 int i; 95 SHead=(node *)malloc(sizeof(node)); 96 STail=SHead; 97 STail->next=NULL; 98 for(i=0;i<3;i++) 99 { 100 newsnake=(node *)malloc(sizeof(node)); 101 newsnake->next=NULL; 102 STail->next =newsnake; 103 STail=newsnake; 104 } 105 STail->next=NULL; 106 p=SHead; 107 SetColor(2); 108 gotoxy(p->x=16,p->y=10),printf("◆"); 109 p=p->next; 110 SetColor(3); 111 gotoxy(p->x=14,p->y=10),printf("□"); 112 p=p->next; 113 gotoxy(p->x=12,p->y=10),printf("□"); 114 p=p->next; 115 gotoxy(p->x=10,p->y=10),printf("□"); 116 117 } 118 void startgame() 119 { 120 while(1) 121 { 122 isdead(); 123 if(Life==0) 124 break; 125 if(foodnum==0) 126 { 127 makefood(); 128 foodnum=1; 129 } 130 if(GetAsyncKeyState(VK_UP)&&movedir!='D') 131 movedir='U'; 132 if(GetAsyncKeyState(VK_DOWN)&&movedir!='U') 133 movedir='D'; 134 if(GetAsyncKeyState(VK_LEFT)&&movedir!='R') 135 movedir='L'; 136 if(GetAsyncKeyState(VK_RIGHT)&&movedir!='L') 137 movedir='R'; 138 isfood(); //判断前方是否有食物,然后移动一格 139 Sleep(speed);//刷新时间,即速度; 140 int Olevel=level; 141 SetColor(7); 142 gotoxy(68,13);printf("%d",score); 143 gotoxy(68,15);printf("%d",level=score/7+1); 144 if(Olevel==level-1)//等级判断 145 { 146 if(speed>200) 147 speed-=60; 148 else if(speed>100) 149 speed-=40; 150 else 151 speed-=14; 152 } 153 SetColor(5); 154 if(level>=5) 155 { 156 gotoxy(58,19);printf("穿墙技能GET.22333"); 157 gotoxy(58,20);printf("你可放心穿过白墙"); 158 } 159 } 160 system("cls"); 161 } 162 void isfood() 163 { 164 if(movedir=='U') 165 { 166 if(SHead->x==x_food&&SHead->y-1==y_food)//判断 167 yesfood(); //前方有食物 168 else 169 nofood(SHead->x,SHead->y-1);//前方没有食物 170 } 171 if(movedir=='D') 172 { 173 if(SHead->x==x_food&&SHead->y+1==y_food) 174 yesfood(); 175 else 176 nofood(SHead->x,SHead->y+1); 177 } 178 if(movedir=='L') 179 { 180 if(SHead->x-2==x_food&&SHead->y==y_food) 181 yesfood(); 182 else 183 nofood(SHead->x-2,SHead->y); 184 } 185 if(movedir=='R') 186 { 187 if(SHead->x+2==x_food&&SHead->y==y_food) 188 yesfood(); 189 else 190 nofood(SHead->x+2,SHead->y); 191 192 } 193 drawsnake(); 194 } 195 void nofood(int x,int y) 196 { 197 node* Nh;//临时头节点 198 Nh=(node *)malloc(sizeof(node));//设定新的蛇头 199 Nh->x=x; 200 Nh->y=y; 201 Nh->next=SHead; 202 SHead=Nh; 203 Snakedelete();//删除最后一个节点 204 } 205 void yesfood() 206 { 207 node* food; 208 food=(node*)malloc(sizeof(node)); 209 food->x=x_food; 210 food->y=y_food; 211 food->next=SHead; 212 SHead=food; 213 foodnum=0; 214 makefood(); 215 score++; 216 } 217 void Snakedelete() 218 { 219 p=SHead; 220 while((p->next)->next!=NULL) 221 { 222 p=p->next; 223 } 224 gotoxy(STail->x,STail->y),printf(" "); 225 free(STail); 226 STail=p; 227 STail->next=NULL; 228 } 229 void drawsnake() 230 { 231 p=SHead->next; 232 SetColor(2); 233 gotoxy(SHead->x,SHead->y); 234 printf("◆"); 235 while(p!=NULL) 236 { 237 SetColor(2); 238 gotoxy(p->x,p->y),printf("□"); 239 p=p->next; 240 } 241 } 242 void isdead()//判断是否死亡 243 { 244 node* p; 245 if(SHead->y==4&&movedir=='U')//预判是否撞到墙 246 { 247 Life=0; 248 return; 249 } 250 if(SHead->y==29&&movedir=='D') 251 { 252 Life=0; 253 return; 254 } 255 if(SHead->x==4&&movedir=='L') 256 { 257 Life=0; 258 return; 259 } 260 if(SHead->x==48&&movedir=='R') 261 { 262 Life=0; 263 return; 264 } 265 for(p=SHead->next;p!=NULL;p=p->next)//判断是否撞到自己 266 { 267 if(SHead->x==p->x&&SHead->y==p->y) 268 { 269 Life=0; 270 break; 271 } 272 } 273 if(level<5) 274 { 275 for(int i=0;i<36;i++) 276 { 277 if(SHead->x==x_block[i]&&SHead->y==y_block[i]) 278 { 279 Life=0; 280 break; 281 } 282 } 283 } 284 } 285 void makefood()//随机产生食物 286 { 287 srand(time(NULL)); 288 do 289 { 290 x_food=rand()%41+5; 291 if(x_food%2==1) 292 x_food++; 293 y_food=(rand()*rand())%23+5; 294 }while(judge());//判断食物是否会产生在蛇的身体上 295 gotoxy(x_food,y_food); 296 SetColor(4); 297 printf("●"); 298 foodnum=1; 299 } 300 void makeblock()//随机产生障碍物 301 { 302 memset(x_block,0,sizeof(x_block)); 303 memset(y_block,0,sizeof(y_block)); 304 int i=0,j=8,k=7,p=2; 305 for(;i<12;i++) 306 { 307 srand(time(NULL)); 308 x_block[i]=(rand()*(++p))%39+8; 309 if(x_block[i]%2==1) 310 x_block[i]++; 311 y_block[i]=(rand()*(++k)*i)%22+5; 312 if(y_block[i]%3==0) 313 { 314 gotoxy(x_block[i],y_block[i]); 315 printf("■■"); 316 x_block[j]=x_block[i]+2; 317 y_block[j++]=y_block[i]; 318 } 319 else if(y_block[i]%3==1)//最坏的情况 320 { 321 gotoxy(x_block[i],y_block[i]); 322 printf("■"); 323 gotoxy(x_block[i],y_block[i]+1); 324 printf("■"); 325 gotoxy(x_block[i],y_block[i]+2); 326 printf("■"); 327 x_block[j]=x_block[i]; 328 y_block[j]=y_block[i]+1; 329 x_block[j+1]=x_block[i]; 330 y_block[j+1]=y_block[i]+2; 331 j+=2; 332 } 333 else 334 { 335 gotoxy(x_block[i],y_block[i]); 336 printf("■■"); 337 x_block[j]=x_block[i]+2; 338 y_block[j]=y_block[i]; 339 } 340 } 341 } 342 bool judge() 343 { 344 node* p=SHead; 345 int i=0; 346 for(;p!=NULL;p=p->next) 347 { 348 if(p->x==x_food&&p->y==y_food) 349 return 1; 350 } 351 for(;i<36;i++) 352 { 353 if(x_food==x_block[i]&&y_food==y_block[i]) 354 return 1; 355 } 356 return 0; 357 } 358 void ending() 359 { 360 SetColor(6); 361 gotoxy(27,10);printf("game over! -_-!"); 362 SetColor(7); 363 gotoxy(30,14);printf("成绩:%d",score); 364 gotoxy(21,30);printf("我的博客:www.cnblog.com/a1225234/"); 365 } 366 void gotoxy(int x, int y)//光标定位函数 x列,y行 367 { 368 COORD coord = {x, y}; 369 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 370 } 371 372 void SetColor(unsigned short ForeColor) // ForeColor前景色 373 { 374 HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); //获得缓冲区句柄 375 SetConsoleTextAttribute(hCon,ForeColor);//设置文本及背景颜色 376 }