您好,欢迎访问三七文档
#includestdio.h#includeprocess.h#includewindows.h#includeconio.h#includetime.h#includestdlib.h#defineWIDTH40#defineHEIGH12enumdirection{//方向LEFT,RIGHT,UP,DOWN};structFood{//食物intx;inty;};structNode{//画蛇身intx;inty;structNode*next;};structSnake{//蛇属性intlenth;//长度enumdirectiondir;//方向};structFood*food;//食物structSnake*snake;//蛇属性structNode*snode,*tail;//蛇身intSPEECH=200;intscore=0;//分数intsmark=0;//吃食物标记inttimes=0;intSTOP=0;voidInitfood();//产生食物voidInitsnake();//构造snakevoidEatfood();//头部前进voidAddnode(intx,inty);//增加蛇身voiddisplay(structNode*shead);//显示蛇身坐标voidmove();//蛇移动voiddraw();//画蛇voidHomepage();//主页voidkeybordhit();//监控键盘按键voidAddtail();//吃到食物voidgotoxy(intx,inty)//定位光标{COORDpos;pos.X=x-1;pos.Y=y-1;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}voidInitsnake()//构造snake{inti;snake=(structSnake*)malloc(sizeof(structSnake));tail=(structNode*)malloc(sizeof(structNode));food=(structFood*)malloc(sizeof(structFood));snake-lenth=5;//初始长度5snake-dir=RIGHT;//初始蛇头方向右for(i=2;i=snake-lenth+2;i++)//增加5个结点{Addnode(i,2);}}voidInitfood()//产生食物{structNode*p=snode;intmark=1;srand((unsigned)time(NULL));//以时间为种子产生随机数while(1){food-x=rand()%(WIDTH-2)+2;//食物X坐标food-y=rand()%(HEIGH-2)+2;//食物Y坐标while(p!=NULL){if((food-x==p-x)&&(food-y==p-y))//如果食物产生在蛇身上{//则重新生成食物mark=0;//食物生成无效break;}p=p-next;}if(mark==1)//如果食物不在蛇身上,生成食物,否则重新生成食物{gotoxy(food-x,food-y);printf(%c,3);break;}mark=1;p=snode;}}voidmove()//移动{structNode*q,*p=snode;if(snake-dir==RIGHT){Addnode(p-x+1,p-y);if(smark==0){while(p-next!=NULL){q=p;p=p-next;}q-next=NULL;free(p);}}if(snake-dir==LEFT){Addnode(p-x-1,p-y);if(smark==0){while(p-next!=NULL){q=p;p=p-next;}q-next=NULL;free(p);}}if(snake-dir==UP){Addnode(p-x,p-y-1);if(smark==0){while(p-next!=NULL){q=p;p=p-next;}q-next=NULL;free(p);}}if(snake-dir==DOWN){Addnode(p-x,p-y+1);if(smark==0){while(p-next!=NULL){q=p;p=p-next;}q-next=NULL;free(p);}}}voidAddnode(intx,inty)//增加蛇身{structNode*newnode=(structNode*)malloc(sizeof(structNode));structNode*p=snode;newnode-next=snode;newnode-x=x;newnode-y=y;snode=newnode;//结点加到蛇头if(x2||x=WIDTH||y2||y=HEIGH)//碰到边界{STOP=1;gotoxy(10,19);printf(撞墙,游戏结束,任意键退出!\n);//失败_getch();free(snode);//释放内存free(snake);exit(0);}while(p!=NULL)//碰到自身{if(p-next!=NULL)if((p-x==x)&&(p-y==y)){STOP=1;gotoxy(10,19);printf(撞到自身,游戏结束,任意键退出!\n);//失败_getch();free(snode);//释放内存free(snake);exit(0);}p=p-next;}}voidEatfood()//吃到食物{Addtail();score++;}voidAddtail()//增加蛇尾{structNode*newnode=(structNode*)malloc(sizeof(structNode));structNode*p=snode;tail-next=newnode;newnode-x=50;newnode-y=20;newnode-next=NULL;//结点加到蛇头tail=newnode;//新的蛇尾}voiddraw()//画蛇{structNode*p=snode;inti,j;while(p!=NULL){gotoxy(p-x,p-y);printf(%c,2);tail=p;p=p-next;}if(snode-x==food-x&&snode-y==food-y)//蛇头坐标等于食物坐标{smark=1;Eatfood();//增加结点Initfood();//产生食物}if(smark==0){gotoxy(tail-x,tail-y);//没吃到食物清除之前的尾结点printf(%c,'');//如果吃到食物,不清楚尾结点}else{times=1;}if((smark==1)&&(times==1)){gotoxy(tail-x,tail-y);//没吃到食物清除之前的尾结点printf(%c,'');//如果吃到食物,不清楚尾结点smark=0;}gotoxy(50,12);printf(食物:%d,%d,food-x,food-y);gotoxy(50,5);printf(分数:%d,score);gotoxy(50,7);printf(速度:%d,SPEECH);gotoxy(15,14);printf(按o键加速);gotoxy(15,15);printf(按p键减速);gotoxy(15,16);printf(按空格键暂停);}voidHideCursor()//隐藏光标{CONSOLE_CURSOR_INFOcursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);}voidHomepage()//绘主页{intx,y;HideCursor();//隐藏光标printf(----------------------------------------\n);printf(|\t\t\t\t|\n);printf(|\t\t\t\t|\n);printf(|\t\t\t\t|\n);printf(|\t\t\t\t|\n);printf(|\t\t\t\t|\n);printf(|\t\t\t\t|\n);printf(|\t\t\t\t|\n);printf(|\t\t\t\t|\n);printf(|\t\t\t\t|\n);printf(|\t\t\t\t|\n);printf(----------------------------------------\n);gotoxy(5,13);printf(任意键开始游戏!按W.A.S.D控制方向);_getch();Initsnake();Initfood();gotoxy(5,13);printf();}voidkeybordhit()//监控键盘{charch;if(_kbhit()){ch=getch();switch(ch){case'W':case'w':if(snake-dir==DOWN)//如果本来方向是下,而按相反方向无效{break;}elsesnake-dir=UP;break;case'A':case'a':if(snake-dir==RIGHT)//如果本来方向是右,而按相反方向无效{break;}elsesnake-dir=LEFT;break;case'S':case's':if(snake-dir==UP)//如果本来方向是上,而按相反方向无效{break;}elsesnake-dir=DOWN;break;case'D':case'd':if(snake-dir==LEFT)//如果本来方向是左,而按相反方向无效{break;}elsesnake-dir=RIGHT;break;case'O':case'o':if(SPEECH=150)//速度加快{SPEECH=SPEECH-50;}break;case'P':case'p':if(SPEECH=400)//速度减慢{SPEECH=SPEECH+50;}break;case''://暂停gotoxy(15,18);printf(游戏已暂停,按任意键恢复游戏);system(pausenul);gotoxy(15,18);printf();break;default:break;}}}intmain(void)//程序入口{Homepage();while(!STOP){keybordhit();//监控键盘按键move();//蛇的坐标变化draw();//蛇的重绘Sleep(SPEECH);//暂时挂起线程}return0;}
本文标题:C语言贪吃蛇源代码
链接地址:https://www.777doc.com/doc-4586475 .html