一个C语言写的简单贪吃蛇源代码

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

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

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

资源描述

#includeconio.h#includegraphics.h#includetime.h#includestring.h#includemalloc.h#includestdio.hintgrade=5,point=0,life=3;voidset(),menu(),move_head(),move_body(),move(),init_insect(),left(),upon(),right(),down(),init_graph(),food_f(),ahead(),crate();structbug{intx;inty;structbug*last;structbug*next;};structfd{intx;inty;intjudge;}food={0,0,0};structbug*head_f=NULL,*head_l,*p1=NULL,*p2=NULL;voidmain(){charch;initgraph(800,600);set();init_insect();while(1){food_f();Sleep(grade*10);setcolor(BLACK);circle(head_l-x,head_l-y,2);setcolor(WHITE);move_body();if(kbhit()){ch=getch();if(ch==27){ahead();set();}elseif(ch==-32){switch(getch()){case72:upon();break;case80:down();break;case75:left();break;case77:right();break;}}elseahead();}else{ahead();}if(head_f-x==food.x&&head_f-y==food.y){Sleep(100);crate();food.judge=0;point=point+(6-grade)*10;if(food.x30||food.y30||food.x570||food.y570)life++;menu();}if(head_f-x5||head_f-x595||head_f-y5||head_f-y595){Sleep(1000);life--;food.judge=0;init_graph();init_insect();menu();}for(p1=head_f-next;p1!=NULL;p1=p1-next){if(head_f-x==p1-x&&head_f-y==p1-y){Sleep(1000);life--;food.judge=0;init_graph();init_insect();menu();break;}}if(life==0){outtextxy(280,300,游戏结束!);getch();return;}move();};}voidinit_graph(){clearviewport();setlinestyle(PS_SOLID,1,5);rectangle(2,2,600,598);setlinestyle(PS_SOLID,1,1);}voidset(){init_graph();outtextxy(640,50,1、开始/返回);outtextxy(640,70,2、退出);outtextxy(640,90,3、难度);outtextxy(640,110,4、重新开始);switch(getch()){case'1':menu();setcolor(GREEN);circle(food.x,food.y,2);setcolor(WHITE);return;case'2':exit(0);break;case'3':outtextxy(700,90,:12345);grade=getch()-48;set();break;case'4':food.judge=0,grade=5;point=0;life=3;init_insect();menu();break;default:outtextxy(250,300,输入错误!);set();break;}}voidmenu(){charstr[20],str1[]={分数:},str2[]={难度:},str3[]={生命值:};init_graph();sprintf(str,%d,point);strcat(str1,str);outtextxy(640,50,str1);sprintf(str,%d,grade);strcat(str2,str);outtextxy(640,70,str2);sprintf(str,%d,life);strcat(str3,str);outtextxy(640,90,str3);outtextxy(640,110,设置:ESC);}voidinit_insect(){head_f=(structbug*)malloc(sizeof(structbug));head_f-last=NULL;head_f-x=300;head_f-y=300;p2=head_f-next=p1=(structbug*)malloc(sizeof(structbug));p1-last=head_f;p1-x=295;p1-y=300;p1=p1-next=(structbug*)malloc(sizeof(structbug));p1-next=NULL;p1-x=290;p1-y=300;p1-last=p2;head_l=p1;}voidmove(){for(p1=head_f;p1!=NULL;p1=p1-next){circle(p1-x,p1-y,2);}}voidmove_head(){}voidmove_body(){for(p1=head_l,p2=p1-last;p2!=NULL;p1=p2,p2=p2-last){p1-x=p2-x;p1-y=p2-y;}}voidahead(){p1=head_f;p2=p1-next;p2=p2-next;if(p1-x==p2-x){if(p1-yp2-y)head_f-y+=5;elsehead_f-y-=5;}else{if(p1-xp2-x){head_f-x+=5;}elsehead_f-x-=5;}}voidupon(){p1=head_f-next;p1=p1-next;head_f-y-=5;if(p1-x==head_f-x&&p1-y==head_f-y){head_f-y+=5;ahead();}}voiddown(){p1=head_f-next;p1=p1-next;head_f-y+=5;if(p1-x==head_f-x&&p1-y==head_f-y){head_f-y-=5;ahead();}}voidleft(){p1=head_f-next;p1=p1-next;head_f-x-=5;if(p1-x==head_f-x&&p1-y==head_f-y){head_f-x+=5;ahead();}}voidright(){p1=head_f-next;p1=p1-next;head_f-x+=5;if(p1-x==head_f-x&&p1-y==head_f-y){head_f-x-=5;ahead();}}voidfood_f(){if(!food.judge){food.x=(rand()%117+1)*5;food.y=(rand()%117+1)*5;food.judge=1;if(food.x30||food.y30||food.x570||food.y570){setcolor(RED);circle(food.x,food.y,2);setcolor(WHITE);}else{setcolor(GREEN);circle(food.x,food.y,2);setcolor(WHITE);}}}voidcrate(){p1=head_l-next=(structbug*)malloc(sizeof(structbug));p1-next=NULL;p1-last=head_l;head_l=p1;}本文由一八网资在线提供:

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

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

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

×
保存成功