资源描述:
《贪吃蛇c语言精简版.docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、贪吃蛇c语言精简版//贪吃蛇#include#include#include#include#includestructall_xy{POINTpoint;structall_xy*next;};intx=2,y=0,key,i,found_time;POINTsave_point,save_point2,food_xy={20,10};BOOLend_self=FALSE,flag;structall_xy*head=NULL
2、,*node1,*node2;voidgotoxy(intx,inty){COORDcoord;coord.X=x,coord.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);}voidsnake_add(){for(node2=head;head!=NULL&&node2->next!=NULL;node2=node2->next);node1=(structall_xy*)malloc(sizeof(structall_xy));
3、node1->point=(head==NULL?food_xy:node2->point);node1->next=NULL;if(head==NULL){head=node1;return;}else{for(node2=head;node2->next!=NULL;node2=node2->next);node2->next=node1;}}voidmake_food(long*x,long*y){for(found_time=0;found_time<500;found_time++){*x=(rand()%38
4、+1)*2,*y=rand()%23+1;for(node1=head,flag=FALSE;node1!=NULL;node1=node1->next)if(*x==node1->point.x&&*y==node1->point.y){flag=TRUE;break;}if(!flag){gotoxy(*x,*y);printf("○");return;}}MessageBox(NULL,"已找不到空点放食物了,程序结束!","你太牛了!",MB_ICONASTERISK);exit(0);}intmain(){fo
5、r(i=0;i<3;i++)snake_add();for(srand(time(NULL)),make_food(&food_xy.x,&food_xy.y);;Sleep(400)){if(kbhit()){if((key=getch())==224)key=getch();switch(key){case80:y!=-1?(x=0,y=1):printf("a");break;case72:y!=1?(x=0,y=-1):printf("a");break;case75:x!=2?(x=-2,y=0):prin
6、tf("a");break;case77:x!=-2?(x=2,y=0):printf("a");break;}}node2=head,node1=node2->next,save_point=node1->point;node1->point=node2->point,node1=node1->next;head->point.x+=x,head->point.y+=y;for(node2=head->next;node2!=NULL;node2=node2->next)//依次检查是否自己撞到自己if(node2
7、->point.x==head->point.x&&node2->point.y==head->point.y)//如果发现头结点和任意一个结点的XY相同则设置end_self的值并跳出{end_self=TRUE;break;}if(head->point.x<0
8、
9、head->point.x>=78
10、
11、head->point.y<0
12、
13、head->point.y>=25
14、
15、end_self==TRUE){gotoxy(32,5);printf("游戏结束!");getch();return0;}for(;node1!
16、=NULL;node1=node1->next){save_point2=node1->point,node1->point=save_point;node2=node1,save_point=save_point2;}gotoxy(save_point.x,save_point.y);printf("");if(h