标签:
1 #include<stdio.h> 2 #include<windows.h> 3 #include<time.h> 4 #include<conio.h> 5 #include<stdlib.h> 6 struct node{ 7 int x,y; 8 node *next; 9 }; 10 11 int Life=1;//是否死亡 0代表死亡; 12 int x_food,y_food;//食物位置 13 char movedir=‘R‘;//方向 14 node* SHead=NULL;// 15 node* STail=NULL; 16 node* p=NULL; 17 int foodnum=0; 18 int score=0; 19 int level=0; 20 int death=0; 21 int speed=400; 22 int Stime=0; 23 int count=0; 24 25 void creatsnakemap();//创建地图 26 void creatsnake();//创建蛇 27 void gotoxy(int,int);//光标定位函数* 28 void SetColor(unsigned short);//设置颜色 29 void startgame();//开始游戏 30 void isdead();//判断是否死亡; 31 void nofood(int x,int y);//前方没有食物 32 void yesfood();//前方有食物 33 void makefood();//随机产生食物 34 void drawsnake();//画出蛇 35 void Snakedelete();//删除蛇的尾巴 36 void isfood();//判断前方是否有食物 37 void creatscreen();//打印开始屏幕 38 void ending();//结束 39 bool judge();//判断食物是否出现在蛇身体上 40 int time();//计算时间 41 42 int main() 43 { 44 system("mode con cols=80 lines=35"); 45 creatscreen(); 46 creatsnakemap(); 47 creatsnake(); 48 startgame(); 49 ending(); 50 while(getch()!=27) 51 continue; 52 } 53 void creatscreen() 54 { 55 SetColor(7); 56 gotoxy(31,10);printf("经典贪吃蛇"); 57 gotoxy(28,12);printf("按任意键开始游戏"); 58 gotoxy(28,30);printf("Copyright@_御心飞行"); 59 getch(); 60 system("cls"); 61 } 62 void creatsnakemap() 63 { 64 int i; 65 SetColor(3); 66 for(i=4;i<50;i+=2)//横墙 67 { 68 gotoxy(i,4); 69 printf("■"); 70 gotoxy(i,29); 71 printf("■"); 72 Sleep(30); 73 } 74 for(i=4;i<29;i++)//竖墙 75 { 76 gotoxy(4,i); 77 printf("■"); 78 gotoxy(48,i); 79 printf("■"); 80 Sleep(30); 81 } 82 SetColor(7); 83 gotoxy(58,13);printf("人头:"); 84 gotoxy(58,15);printf("等级:"); 85 gotoxy(58,17);printf("速度:"); 86 gotoxy(58,19);printf("时间:"); 87 } 88 void creatsnake() 89 { 90 node* newsnake; 91 int i; 92 SHead=(node *)malloc(sizeof(node)); 93 STail=SHead; 94 STail->next=NULL; 95 for(i=0;i<3;i++) 96 { 97 newsnake=(node *)malloc(sizeof(node)); 98 newsnake->next=NULL; 99 STail->next =newsnake; 100 STail=newsnake; 101 } 102 STail->next=NULL; 103 p=SHead; 104 SetColor(2); 105 gotoxy(p->x=16,p->y=10),printf("◆"); 106 p=p->next; 107 SetColor(3); 108 gotoxy(p->x=14,p->y=10),printf("□"); 109 p=p->next; 110 gotoxy(p->x=12,p->y=10),printf("□"); 111 p=p->next; 112 gotoxy(p->x=10,p->y=10),printf("□"); 113 114 } 115 void startgame() 116 { 117 while(1) 118 { 119 isdead(); 120 if(Life==0) 121 break; 122 if(foodnum==0) 123 { 124 makefood(); 125 foodnum=1; 126 } 127 if(GetAsyncKeyState(VK_UP)&&movedir!=‘D‘) 128 movedir=‘U‘; 129 if(GetAsyncKeyState(VK_DOWN)&&movedir!=‘U‘) 130 movedir=‘D‘; 131 if(GetAsyncKeyState(VK_LEFT)&&movedir!=‘R‘) 132 movedir=‘L‘; 133 if(GetAsyncKeyState(VK_RIGHT)&&movedir!=‘L‘) 134 movedir=‘R‘; 135 isfood(); //判断前方是否有食物,然后移动一格 136 Sleep(speed);//刷新时间,即速度; 137 count++; 138 time(); 139 int Olevel=level; 140 SetColor(7); 141 gotoxy(68,13);printf("%d",score); 142 gotoxy(68,15);printf("%d",level=score/7+1); 143 gotoxy(68,17);printf("%d",speed); 144 gotoxy(68,19);printf("%d",Stime); 145 if(Olevel==level-1)//等级判断 146 if(speed>200) 147 speed-=60; 148 else if(speed>100) 149 speed-=40; 150 else if(speed>15) 151 speed-=14; 152 } 153 system("cls"); 154 } 155 void isfood() 156 { 157 if(movedir==‘U‘) 158 { 159 if(SHead->x==x_food&&SHead->y-1==y_food)//判断 160 yesfood(); //前方有食物 161 else 162 nofood(SHead->x,SHead->y-1);//前方没有食物 163 } 164 if(movedir==‘D‘) 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==‘L‘) 172 { 173 if(SHead->x-2==x_food&&SHead->y==y_food) 174 yesfood(); 175 else 176 nofood(SHead->x-2,SHead->y); 177 } 178 if(movedir==‘R‘) 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 } 186 drawsnake(); 187 } 188 void nofood(int x,int y) 189 { 190 node* Nh;//临时头节点 191 Nh=(node *)malloc(sizeof(node));//设定新的蛇头 192 Nh->x=x; 193 Nh->y=y; 194 Nh->next=SHead; 195 SHead=Nh; 196 Snakedelete();//删除最后一个节点 197 } 198 void yesfood() 199 { 200 node* food; 201 food=(node*)malloc(sizeof(node)); 202 food->x=x_food; 203 food->y=y_food; 204 food->next=SHead; 205 SHead=food; 206 foodnum=0; 207 makefood(); 208 score++; 209 } 210 void Snakedelete() 211 { 212 p=SHead; 213 while((p->next)->next!=NULL) 214 { 215 p=p->next; 216 } 217 gotoxy(STail->x,STail->y),printf(" "); 218 free(STail); 219 STail=p; 220 STail->next=NULL; 221 } 222 void drawsnake() 223 { 224 p=SHead->next; 225 SetColor(2); 226 gotoxy(SHead->x,SHead->y); 227 printf("◆"); 228 while(p!=NULL) 229 { 230 SetColor(2); 231 gotoxy(p->x,p->y),printf("□"); 232 p=p->next; 233 } 234 } 235 void isdead()//判断是否死亡 236 { 237 node* p; 238 if(SHead->y==4&&movedir==‘U‘)//预判是否撞到墙 239 Life=0; 240 if(SHead->y==29&&movedir==‘D‘) 241 Life=0; 242 if(SHead->x==4&&movedir==‘L‘) 243 Life=0; 244 if(SHead->x==48&&movedir==‘R‘) 245 Life=0; 246 for(p=SHead->next;p!=NULL;p=p->next)//判断是否撞到自己 247 { 248 if(SHead->x==p->x&&SHead->y==p->y) 249 Life=0; 250 } 251 } 252 void makefood() 253 { 254 srand(time(NULL)); 255 do 256 { 257 x_food=rand()%41+5; 258 if(x_food%2==1) 259 x_food++; 260 y_food=(rand()*rand())%23+5; 261 }while(judge()); 262 gotoxy(x_food,y_food); 263 SetColor(4); 264 printf("●"); 265 foodnum=1; 266 } 267 bool judge() 268 { 269 node* p=SHead; 270 for(;p!=NULL;p=p->next) 271 { 272 if(p->x==x_food&&p->y==y_food) 273 return 1; 274 } 275 return 0; 276 } 277 int time() 278 { 279 Stime=(int)(count/(1000/(double)speed)); 280 } 281 void ending() 282 { 283 SetColor(6); 284 gotoxy(27,10);printf("game over! -_-!"); 285 SetColor(7); 286 gotoxy(30,14);printf("成绩:%d",score); 287 gotoxy(30,16);printf("时间:%d",Stime); 288 gotoxy(21,30);printf("我的博客:www.cnblog.com/a1225234/"); 289 } 290 void gotoxy(int x, int y)//光标定位函数 x列,y行 291 { 292 COORD coord = {x, y}; 293 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 294 } 295 296 void SetColor(unsigned short ForeColor) // ForeColor前景色 297 { 298 HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); //获得缓冲区句柄 299 SetConsoleTextAttribute(hCon,ForeColor);//设置文本及背景颜色 300 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/4582056.html