《c语言贪吃蛇游戏源码.pdf》由会员分享,可在线阅读,更多相关《c语言贪吃蛇游戏源码.pdf(8页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、/*运行是按任意键开始,不过呢,反向也算输哟*/#include#include#include#include#include#include#include#define UP 0 x4800#define DOWN 0 x5000#define LEFT 0 x4b00#define RIGHT 0 x4d00#define ESC 0 x011b#define SPACE 0 x3920#define M 25#define N 25#define blue 1#define green 2#define red 4#define yellow 14#define yes 1#defi
2、ne no 0struct snake int l,t,r,b;int color;int direction;右 蛇头的方向*/qu1M*N;int d4=-25,25,-1,1;int length;int speed=50000000;int head;int food;int appear;int key;int direct;int all_food;int tail,mark;int Q=1;void draw_map()int i,j,n=17,v=1;/*0 不动 1 上 2 下 3 左 4蛇头移动的方向*/*蛇身长度*/*移动的速度*/蛇头的位置*/地图*/*/*/*int
3、a2525;for(i=0;i=24;i+)for(j=0;j=24;j+)if(j=0|j=24|i=24|i=0)aij=0;/*0 stansfor block*/else aij=1;for(i=0;i=24;i+)for(j=0;j=24;j+)if(aij=0)setfillstyle(1,green);qu1v.color=green;if(aij=1)setfillstyle(1,blue);qu1v.color=blue;qu1v.l=20+n*j;qu1v.t=20+n*i;qu1v.r=35+n*j;qu1v.b=35+n*i;qu1v.direction=0;bar(q
4、u1v.l,qu1v.t,qu1v.r,qu1v.b);v+;void draw_snake()int v,j;v=head=303;all_food=10;length=0;for(j=0;j=1;j+,v-)setfillstyle(1,yellow);bar(qu1v.l,qu1v.t,qu1v.r,qu1v.b);qu1v.color=red;qu1v.direction=4;length+;void diamond()float t;int x0=540,y0=165;int n=16,i,j,r=90;int x50,y50;t=6.28318/n;for(i=0;in;i+)xi
5、=x0+r*cos(i*t);yi=y0+r*sin(i*t);setcolor(yellow);for(i=0;i=n-2;i+)setcolor(YELLOW);for(j=1;j=n-1;j+)line(xi,yi,xj,yj);main()int gdrive=VGA;int gmode=VGAHI;int class;initgraph(&gdrive,&gmode,c:tc);setcolor(LIGHTRED);outtextxy(450,280,SPACE:Pause);outtextxy(450,300,ESC:Exit);outtextxy(450,320,Left);ou
6、ttextxy(450,340,Right);outtextxy(450,360,Up);outtextxy(450,380,Down);outtextxy(450,400,THE FOUR KEYES TO MOVE);setcolor(YELLOW);outtextxy(40,2,Welcome to snake game and Please any key begin!);outtextxy(450,33,Class:);outtextxy(450,51,All_food:);randomize();diamond();for(class=1;class=10)printf(%d,al
7、l_food);else printf(0%d,all_food);if(all_food=0)setcolor(red+class);outtextxy(50,450,Congratulation andPlease any key goto the next!);break;if(appear=no)do food=random(600);while(qu1food.color=yellow|qu1food.color=red|qu1food.color=green|food=0);qu1food.color=red;qu1food.direction=0;setfillstyle(1,r
8、ed);bar(qu1food.l,qu1food.t,qu1food.r,qu1food.b);appear=yes;star:do tail=head;mark=length;switch(qu1head.direction)case 1:head=head+d0;direct=1;break;case 2:head=head+d1;direct=2;break;case 3:head=head+d2;direct=3;break;case 4:head=head+d3;direct=4;if(qu1head.direction!=0|qu1head.color=green)/*撞墙或撞自
9、己*/if(Q=1)setcolor(red);outtextxy(450,200,Any key toexit!);getch();closegraph();exit(0);else Q=1;qu1head.direction=direct;if(head=food)appear=no;delay(speed);all_food-;length+;continue;qu1head.color=yellow;setfillstyle(1,yellow);bar(qu1head.l,qu1head.t,qu1head.r,qu1head.b);setfillstyle(1,red);bar(qu
10、1tail.l,qu1tail.t,qu1tail.r,qu1tail.b);for(;length1;length-)tail=tail-dqu1tail.direction-1;setfillstyle(1,blue);bar(qu1tail.l,qu1tail.t,qu1tail.r,qu1tail.b);qu1tail.color=blue;qu1tail.direction=0;length=mark;delay(speed);while(!kbhit();key=bioskey(0);if(qu1head.direction=1)if(key=UP)Q=0;continue;if(
11、qu1head.direction=2)if(key=DOWN)0;direct=1;break;DOWN:direct=2;break;LEFT:direct=3;Q=0;continue;if(qu1head.direction=3)if(key=LEFT)Q=0;continue;if(qu1head.direction=4)if(key=RIGHT)Q=0;continue;tail=head;mark=length;switch(key)case case case head=head+d UP:head=head+d1;head=head+d2;break;caseRIGHT:he
12、ad=head+d3;direct=4;break;case ESC:exit(0);case SPACE:getch();gotostar;/*暂停*/if(qu1head.direction!=0|qu1head.color=green)/*撞墙或撞自己*/if(Q=1)setcolor(red);outtextxy(450,200,Any key toexit!);getch();closegraph();exit(0);else Q=1;qu1head.direction=direct;if(head=food)appear=no;delay(speed);all_food-;leng
13、th+;continue;qu1head.color=yellow;setfillstyle(1,yellow);bar(qu1head.l,qu1head.t,qu1head.r,qu1head.b);setfillstyle(1,red);bar(qu1tail.l,qu1tail.t,qu1tail.r,qu1tail.b);for(;length1;length-)tail=tail-dqu1tail.direction-1;setfillstyle(1,blue);bar(qu1tail.l,qu1tail.t,qu1tail.r,qu1tail.b);qu1tail.color=blue;qu1tail.direction=0;length=mark;delay(speed);speed=speed-15000;setcolor(LIGHTRED);outtextxy(200,460,You are wining the game.);getch();closegraph();