您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > C/C++资料 > 2048小游戏c语言编程设计
#includestdio.h#includeconio.h//使用getch()函数#includetime.h#includestdlib.hintnum[4][4];intscore,gameover,ifappear,gamew,gamef,move;charkey;voidexplation(){voidmenu();system(cls);printf(\t\t*****************************************\t\t\n);printf(\t\t*****************************************\n);printf(\t\t******************游戏规则***************\n);printf(\t\t*****************************************\n);printf(\t\t*****************************************\t\t\n);printf(玩家可以选择上、下、左、右或W、A、S、D去移动滑块\n);printf(玩家选择的方向上若有相同的数字则合并\n);printf(合并所得的所有新生成数字相加即为该步的有效得分\n);printf(玩家选择的方向行或列前方有空格则出现位移\n);printf(每移动一步,空位随机出现一个2或4\n);printf(棋盘被数字填满,无法进行有效移动,判负,游戏结束\n);printf(棋盘上出现2048,获胜,游戏结束\n);printf(按上下左右去移动滑块\n);printf(请按任意键返回主菜单...\n);getch();system(cls);main();}voidgamefaile(){inti,j;system(cls);printf(\t\t*****************************************\t\t\n);printf(\t\t*****************************************\n);printf(\t\t******************youfail***************\n);printf(\t\t*****************************************\n);printf(\t\t*****************************************\t\t\n);printf(\t\t\t---------------------\n\t\t\t);for(j=0;j4;j++){for(i=0;i4;i++)if(num[j][i]==0)printf(|);elseprintf(|%4d,num[j][i]);printf(|\n);printf(\t\t\t---------------------\n\t\t\t);}printf(你的成绩:%d,移动了%d步\n,score,move);printf(请按任意键返回主菜单...\n);getch();system(cls);main();}voidgamewin(){inti,j;system(cls);printf(\t\t*****************************************\t\t\n);printf(\t\t*****************************************\n);printf(\t\t*******************youwin***************\n);printf(\t\t*****************************************\n);printf(\t\t*****************************************\t\t\n);printf(\t\t\t---------------------\n\t\t\t);for(j=0;j4;j++){for(i=0;i4;i++)if(num[j][i]==0)printf(|);elseprintf(|%4d,num[j][i]);printf(|\n);printf(\t\t\t---------------------\n\t\t\t);}printf(你的成绩:%d,移动了%d步\n,score,move);printf(请按任意键返回主菜单...\n);getch();system(cls);main();}voidprin(){inti,j;system(cls);printf(\t\t*****************************************\t\t\n);//输出界面printf(\t\t*****************************************\n);printf(\t\t******************游戏开始***************\n);printf(\t\t*****************************************\n);printf(\t\t*****************************************\t\t\n);printf(\t\t请按方向键或W、A、S、D移动滑块\n);//输出操作提示语句printf(\t\t按ESC返回至主菜单\n);printf(\t\t\t---------------------\n\t\t\t);for(j=0;j4;j++)//输出4*4的表格{for(i=0;i4;i++)if(num[j][i]==0)printf(|);elseprintf(|%4d,num[j][i]);printf(|\n);printf(\t\t\t---------------------\n\t\t\t);}printf(你的成绩:%d,移动了%d步\n,score,move);//输出得分和移动步数}voidappear(){inti,j,ran,t[16],x=0,a,b;srand((int)time(0));//随机种子初始化for(j=0;j4;j++)//将空白的区域的坐标保存到中间数组t中for(i=0;i4;i++)if(num[j][i]==0){t[x]=j*10+i;x++;}if(x==1)//在t中随机取一个坐标ran=x-1;elseran=rand()%(x-1);a=t[ran]/10;//取出这个数值的十位数b=t[ran]%10;//取出这个数值的个位数srand((int)time(0));if((rand()%9)2)//在此空白区域随机赋值2或4num[a][b]=2;elsenum[a][b]=4;}voidclose(){exit(0);}voidadd(int*p){inti=0,b;while(i3){if(*(p+i)!=0){for(b=i+1;b4;b++){if(*(p+b)!=0)if(*(p+i)==*(p+b)){score=score+(*(p+i))+(*(p+b));*(p+i)=*(p+i)+*(p+b);if(*(p+i)==2048)gamew=1;*(p+b)=0;i=b+i;++ifappear;break;}else{i=b;break;}}if(b==4)i++;}elsei++;}}voidGameplay(){inti,j,g,e,a,b[4];appear();appear();while(1){if(ifappear!=0)appear();prin();key=getch();switch(key){case'w':case'W':case72:ifappear=0;for(j=0;j4;j++){for(i=0;i4;i++){b[i]=num[i][j];num[i][j]=0;}add(b);e=0;for(g=0;g4;g++){if(b[g]!=0){num[e][j]=b[g];if(g!=e)++ifappear;e++;}}}if(ifappear!=0)++move;break;case's':case'S':case80:ifappear=0;for(j=0;j4;j++){for(i=0;i4;i++){b[i]=num[i][j];num[i][j]=0;}add(b);e=3;for(g=3;g=0;g--){if(b[g]!=0){num[e][j]=b[g];if(g!=e)++ifappear;e--;}}}if(ifappear!=0)++move;break;case'a':case'A':case75:ifappear=0;for(j=0;j4;j++){for(i=0;i4;i++){b[i]=num[j][i];num[j][i]=0;}add(b);e=0;for(g=0;g4;g++){if(b[g]!=0){num[j][e]=b[g];if(g!=e)++ifappear;e++;}}}if(ifappear!=0)++move;break;case'd':case'D':case77:ifappear=0;for(j=0;j4;j++){for(i=0;i4;i++){b[i]=num[j][i];num[j][i]=0;}add(b);e=3;for(g=3;g=0;g--){if(b[g]!=0){num[j][e]=b[g];if(g!=e)++ifappear;e--;}}}if(ifappear!=0)++move;break;case27:system(cls);main();break;}for(j=0;j4;j++){for(i=0;i4;i++){if(j3){if(i3){if(num[j][i]==num[j+1][i]||num[j][i]==num[j][i+1]||num[j][i]==0){gamef=0;break;}elsegamef=1;}else{if(num[j][i]==num[j+1][i]||num[j][i]==0){gamef=0;break;}elsegamef=1;}}else{if(i3){if(num[j][i]==num[j][i+1]||num[j][i]==0||num[j][i+1]==0){gamef=0;break;}elsegamef=1;}}}if(gamef==0)break;}if(gamef==1||gamew==1)break;}if(gamef==1)gamefaile();elsegamewin();}voidmenu(){intn;printf(\t\t*****************************************\t\t\n);//输出游戏菜单的图形printf(\t\t*1、开始游戏*\n);printf(\t\t*2、游戏规则*\n);printf(\t\t*3、退出游戏*\n);printf(\t\t*****************************************\n);printf(请输入1或2或3:[]\b\b);scanf(%d,&n);switch(n){case1:Gameplay();//游戏开始函数break;case2:explation();//游戏规则函数break;case3:close();//关闭游戏函数break;}}intmain(){intj,i;for(j=0;j4;j++)//对4*4进行初始赋值为0for(i=0;i4;i++)num[j][i]=0;gamew=0;//游
本文标题:2048小游戏c语言编程设计
链接地址:https://www.777doc.com/doc-7028553 .html