c语言编写的贪吃蛇小游戏

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdio.h#includestdlib.h#includetime.h#includeconio.hstructNode{intx;inty;structNode*pre;structNode*next;};structFood{intx;inty;charc;};voidmain(){inta[15][15]={0};inti,j,t,flag=0;charc='d',c1='d';structFoodfood={5,8,'A'};;intgameover=0;structNode*head,*p,*rear,*pt;head=(structNode*)malloc(sizeof(structNode));head-x=5;head-y=8;head-pre=NULL;head-next=NULL;rear=head;srand((unsigned)time(NULL));while(1){if(food.x==head-x&&food.y==head-y){p=(structNode*)malloc(sizeof(structNode));pt=head;while(pt-next!=NULL)pt=pt-next;p-pre=pt;pt-next=p;p-next=NULL;rear=p;food.x=rand()%15;food.y=rand()%15;food.c=65+rand()%26;flag=1;t=0;while(flag==1){if(t5)break;flag=0;pt=head;while(pt!=NULL){if(food.x==pt-x&&food.y==pt-y){flag=1;food.x=rand()%15;food.y=rand()%15;break;}pt=pt-next;}t++;}if(t5){if(c=='d'){food.x=head-x+1;food.y=head-y;if(food.x=15)food.x-=15;}elseif(c=='a'){food.x=head-x-1;food.y=head-y;if(food.x0)food.x+=15;}elseif(c=='w'){food.x=head-x;food.y=head-y+1;if(food.y=15)food.y-=15;}elseif(c=='s'){food.x=head-x;food.y=head-y-1;if(food.y0)food.y+=15;}}}if(kbhit()){c1=getch();if(c1==27)break;if(c!='d'&&c1=='a')c=c1;elseif(c!='a'&&c1=='d')c=c1;elseif(c!='w'&&c1=='s')c=c1;elseif(c!='s'&&c1=='w')c=c1;}pt=rear;while(pt!=head){pt-x=pt-pre-x;pt-y=pt-pre-y;pt=pt-pre;}if(c=='d'){head-y+=1;if(head-y=15)head-y-=15;}elseif(c=='a'){head-y-=1;if(head-y0)head-y+=15;}elseif(c=='w'){head-x-=1;if(head-x0)head-x+=15;}elseif(c=='s'){head-x+=1;if(head-x=15)head-x-=15;}pt=head-next;while(pt!=NULL){if(head-x==pt-x&&head-y==pt-y){gameover=1;break;}pt=pt-next;}if(gameover==1)break;system(cls);printf(©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤\n);for(i=0;i15;i++){printf(©¦);for(j=0;j15;j++){flag=0;pt=head;while(pt!=NULL){if(i==pt-x&&j==pt-y){if(pt==head)printf(¡ö);elseprintf(¡õ);flag=1;break;}pt=pt-next;}if(flag==0){if(i==food.x&&j==food.y){putchar(food.c);putchar(food.c);continue;}printf();}}printf(©¦);putchar('\n');}printf(©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤\n);_sleep(200);}if(gameover==1)puts(gameover!\n);getch();}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功