资源描述:
《贪吃蛇代码 以及游戏程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、贪吃蛇代码以及游戏程序#include#include#include#include#include#includeintsnake_len=1;//蛇的长度intsnake_loc[50][2]={31,12};//整条蛇的位置,最长为50intsnake_head[2]={31,12};//蛇头位置,初始值为11,12;intfood[2];//食物位置charsnake_direction='s';int
2、delay=200;//蛇每delay个时间走一步inteat_flag=0;//1表示吃了食物,0表示未吃intliv_stat=0;//1表示死了,游戏该结束了;0表示还活着voidgotoxy(intx,inty)//定位光标,x为行坐标,y为列坐标{COORDpos={x,y};HANDLEhOut=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOut,pos);}voidhidden()//隐藏光标{HANDLEhOut=GetStdHandle
3、(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFOcci;GetConsoleCursorInfo(hOut,&cci);cci.bVisible=0;//赋1为显示,赋0为隐藏SetConsoleCursorInfo(hOut,&cci);}voidinit()//初始化{inti;snake_len=1;//蛇的长度snake_loc[0][0]=31;//整条蛇的位置snake_loc[0][1]=12;snake_head[0]=31;//蛇头位置,初始值为11,12;snake_head
4、[1]=12;snake_direction='s';delay=200;eat_flag=0;liv_stat=0;for(i=1;i<50;i++){snake_loc[i][0]=0;//整条蛇的位置snake_loc[i][1]=0;}}voidcreate_window()//创建窗口{gotoxy(0,0);printf("********************************************************************************");printf("***")
5、;printf("***");printf("**分数:1*");printf("**按键说明:*");printf("**上:w*");printf("**下:s*");printf("**左:a*");printf("**右:d*");printf("**暂停:空格*");printf("*袁楚瑶捉蛇*退出:Esc键*");printf("***");printf("***");printf("***");printf("***");printf("***");printf("*高敏放蛇**");printf("***"
6、);printf("***");printf("***");printf("***");printf("***");printf("***");printf("********************************************************************************");}voidupdate_score()//更新分数{gotoxy(73,3);printf("%2d",snake_len);}voidcreate_food()//产生食物的位置{time_tt;sra
7、nd(time(&t));while(1){food[0]=rand()%62+1;//生成1~62之间的随机数,其中random函数生成0~77之间的随机数food[1]=rand()%22+1;//生成1~22之间的随机数,其中random函数生成0~17之间的随机数if(food[0]!=snake_head[0]&&food[1]!=snake_head[1])break;}gotoxy(food[0],food[1]);printf("*");}voiddirection(){charkeyhit=0,i;whil
8、e(kbhit()!=0)keyhit=getch();if(((keyhit=='a')
9、
10、(keyhit=='d')
11、
12、(keyhit=='w')
13、
14、(keyhit=='s'))&&(abs(snake_direction/16-keyhit/16)==1))snake_directi