#include#include#include#includeusing namespace std;int speed,count;typedef struct _SNAKE{ int x; int y; struct _SNAKE *next;}Snake;Snake *s_Head = NULL;typedef struct _FOOD{ int x; int y;}food;food _food = NULL;enum{up=72,down=80,left=75,right=77};void BeginGame();//开始游戏 void RunGame();//运行游戏 void Welcomeplayer();//欢迎玩家 void setCursor(int x,int y);//设置光标 void color(int m);//设置颜色 void buildmap();//建立地图void setSnake();//生成一条蛇 void MoveSnake(int direction);//移动蛇 void ProduceFood();//随机生成食物void extend(int direction);//伸长蛇身void deadPrint();//死亡画面 int dead();//碰撞死亡 int check();//判断是否吃到食物 int main(){ BeginGame(); return 0;}void BeginGame(){ system(“c:windowssystem32mode.com con cols=120 lines=50”); Welcomeplayer();}void Welcomeplayer(){ while(1) { setCursor(39,15); printf(“t 欢迎来到贪吃蛇的世界n”); setCursor(39,17); puts(“t在这里你将体会到一种别样的贪吃蛇n”); puts(“nnnnnnnnnn”); puts(“按任意键继续”); puts(“按Esc退出”); char ch=getch(); if(ch==27)return; system(“cls”); RunGame(); }}void buildmap(){ for(int i=0;itemp = (Snake)malloc(sizeof(Snake)); temp->x=20; temp->y=15; temp->next=NULL; s_Head=temp; for(int i=1;ip=(Snake)malloc(sizeof(Snake)); p->x=20+2i; p->y=15; p->next=NULL; temp->next=p; temp=p; } temp=s_Head; while(temp!=NULL) { setCursor(temp->x,temp->y); printf(“■”); temp=temp->next; }}void RunGame(){ loop: setCursor(33,15); puts(“tt 请选择游戏难度n”); puts(“tttttt 1.简单n”); puts(“tttttt 2.一般n”); puts(“tttttt 3.困难n”); puts(“tttttt 4.特别困难”); char ch=getch(); while(ch!=‘1’&&ch!=‘2’&&ch!=‘3’&&ch!=‘4’) { ch=getch(); } speed=(5-(ch-‘0’))*90; system(“cls”); buildmap(); setSnake(); int direction=right; ProduceFood(); int res=0; res= res>count?res:count; setCursor(80,15); printf(“历史最高分:n”); setCursor(89,16); printf(“%d”,res); setCursor(80,20); count=0; printf(“得分:n”); while(1) { setCursor(83,21); printf(“%d”,count); char ch; if(kbhit()) { ch=getch(); switch(ch) { case 72://上 if(fabs(ch-direction)!=8) direction=up; break; case 75://左 if(fabs(ch-direction)!=2) direction=left; break; case 77://右 if(fabs(ch-direction)!=2) direction=right; break; case 80://下 if(fabs(ch-direction)!=8) direction=down; break; } } MoveSnake(direction); if(check()) { extend(direction); count++; ProduceFood(); } if(dead()1) { system(“cls”); goto loop; } else if(dead()0) { system(“cls”); setCursor(55,20); printf(“游戏结束”); puts(“nnnnnnnnnnnnnnn”); system(“pause”); system(“cls”); return; } Sleep(speed); } }int check(){ Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } if(p->x_food->x&&p->y_food->y) return 1; return 0;} void extend(int direction){ Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } if(p->x==_food->x&&p->y==_food->y) { Snake *temp=(Snake *)malloc(sizeof(Snake)); temp->next = NULL; if(direction == right) { temp->x = p->x + 2; temp->y = p->y; } if(direction == left) { temp->x = p->x – 2; temp->y = p->y; } if(direction == up) { temp->x = p->x; temp->y = p->y – 1; } if(direction == down) { temp->x = p->x; temp->y = p->y + 1; } p->next = temp; setCursor(temp->x,temp->y); printf(“■”); }}void MoveSnake(int direction){ //找到链尾 (即蛇头) Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } //增尾 Snake temp = (Snake)malloc(sizeof(Snake)); temp->next = NULL; if(direction == right) { temp->x = p->x + 2; temp->y = p->y; } if(direction == left) { temp->x = p->x – 2; temp->y = p->y; } if(direction == up) { temp->x = p->x; temp->y = p->y – 1; } if(direction == down) { temp->x = p->x; temp->y = p->y + 1; } p->next = temp; //打印 setCursor(temp->x,temp->y); printf(“■”); setCursor(s_Head->x,s_Head->y); printf(” “); //删头 temp=s_Head->next; delete s_Head; s_Head=temp; }void ProduceFood(){ srand(time(NULL)); food p=(food)malloc(sizeof(food)); while(1) { p->x=rand()%60; p->y=rand()%29; if(p->x > 4&&p->xy > 4&&p->yx%20&&p->y%20) break; } _food=p; setCursor(p->x,p->y); color(4); printf(“●”); color(7);}int dead(){ Snake *temp=s_Head->next; while(temp!=NULL) { if(temp->xs_Head->x && temp->ys_Head->y) { deadPrint(); char ch=getch(); while(ch!=‘Y’&&ch!=‘y’&&ch!=‘N’&&ch!=‘n’) { ch=getch(); } if(ch==‘Y’||ch==‘y’)return 1; if(ch==‘N’||ch==‘n’)return 0; } temp=temp->next; } Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } if((p->x == 60||p->x2)||(p->y1||p->y29)){ deadPrint(); char ch=getch(); while(ch!=‘Y’&&ch!=‘y’&&ch!=‘N’&&ch!=‘n’) { ch=getch(); } if(ch’Y’||ch==‘y’)return 1; if(ch==‘N’||ch==‘n’)return 0; }} void deadPrint(){ system(“cls”); setCursor(54,15); printf(“你已经死亡”); setCursor(54,17); puts(“是否重新开始”); setCursor(45,19); puts(“是Yttt否N “); puts(”nnnnnnn”);}void color(int m){ if(m>=0 && m