一个C语言编写的推箱子游戏源代码 (1)

合集下载

推箱子代码1

推箱子代码1
cout<<"| 上关战绩:"<<afterScore<<"\t|"<<endl; } else if(i==10) {
cout<<"| 已走步数:"<<steep<<"\t|"<<endl; } else if(i==3) {
cout<<"| \t\t\t|<O>操作说明"<<endl; } else if(i==5) {
} void tableThree() {
set=3; cout<<endl; cout<<"\t\t\t\t 推箱子<第三关>"<<endl; cout<<"====================================================================
============"; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { t_box[i][j]='+';
if(i==4&&j==5||i==3&&j==8||i==5&&j==5||i==5&&j==8||i==13&&j==14||i==11&&j==1) { t_box[i][j]='#'; } if(i==row/2&&j==col/2) { t_box[i][j]=12; } cout<<t_box[i][j]<<" ";

C语言实现推箱子游戏完整代码

C语言实现推箱子游戏完整代码

C语⾔实现推箱⼦游戏完整代码C语⾔实现推箱⼦游戏完整代码,供⼤家参考,具体内容如下前⾔⾃⼰做的,可能有些代码不够⼯整,或者有些⼩问题,但游戏的基本操作是可以实现的代码效果代码⼀共分为8个部分,4个控制上下左右移动,2个判断输赢,1个统计归为的个数,⼀个作图。

⼿动设置地图⽤'0'表⽰空格,“1”表⽰墙,“2”表⽰箱⼦,“3”表⽰⼈,“4”表⽰终点这样可以提⾼代码的移植性如需改为⼿动输⼊地图可以直接定义⼀个⼆维数组,在给他赋值就可以了int screen[9][11]={{0,1,1,1,1,1,1,1,1,0,0},{0,1,0,0,0,1,0,0,0,1,0},{0,1,0,2,2,2,2,2,0,1,0},{0,1,0,2,0,2,0,2,0,1,1},{0,1,0,0,0,3,0,0,2,0,1},{1,1,0,1,1,1,1,0,2,0,1},{1,0,4,4,4,4,4,1,0,0,1},{1,0,4,4,4,4,4,0,0,1,1},{1,1,1,1,1,1,1,1,1,1,0}};//定义为全局变量(地图) i表⽰⾏,j表⽰列计算地图中终点的个数这⼀步主要是为了后⾯判断游戏输赢的int cum(){int i,j,k=0;for(i=0;i<9;i++){for(j=0;j<11;j++){if(screen[i][j]==2){k++;}}}//遍历整个⼆维数组return k;}//计算地图中有多少个终点打印地图函数通过switch函数对⼆维数组中的值进⾏可视化,也就是画出地图注意:这⾥还定义出了6和7,是通过重叠的关系来算的,就是箱⼦在终点上,这个位置⼜有箱⼦⼜有终点2个标识,所以让两个的数值加起来,⽅便理解,也⽅便后⾯的计算void print(){int i,j;printf("请⽤wsad代表上下左右来进⾏游戏\n");for(i=0;i<9;i++){for(j=0;j<11;j++){switch(screen[i][j]){case 0:printf(" ");//空break;case 1:printf("■");//墙break;case 2:printf("★");//箱⼦break;case 3:printf("♀");//⼈break;case 4:printf("○");//终点break;case 6:printf("★");break;//箱⼦和终点case 7://⼈和终点显⽰⼈printf("♀");break;}}printf("\n");}}判断游戏输赢这⾥我写了2个函数,⼀个判断赢,⼀个判断输,并返回值,然后在主函数的最后⾯通过判断返回值来确定游戏的输赢判断赢int win(){int i,j,k=0;int t=0;for(i=0;i<9;i++){for(j=0;j<11;j++){if(screen[i][j]==6){k++;}}}//遍历整个⼆维数组,计算箱⼦在终点上的个数if(k==cum()){t=1;}//如果个数等于前⾯计算出的终点个数,则说明所有终点都放了箱⼦,说明游戏胜利return t;} //判断赢判断输int lose(){int i,j;int k=0;for(i=0;i<9;i++){for(j=0;j<11;j++){if(i>0 && j>0 ){if(screen[i][j] == 2 || screen[i][j] == 6){if(((screen[i-1][j] == 1 || screen[i-1][j] == 2 || screen[i-1][j] == 6) && (screen[i][j-1] == 1 || screen[i][j-1] == 2 || screen[i][j-1] == 6))|| ((screen[i][j-1] == 1 || screen[i][j-1] == 2 || screen[i][j-1] == 6) && (screen[i+1][j] == 1 || screen[i+1][j] == 2 || screen[i+1][j] == 6))|| ((screen[i+1][j] == 1 || screen[i+1][j] == 2 || screen[i+1][j] == 6) && (screen[i][j+1] == 1 || screen[i][j+1] == 2 || screen[i][j+1] == 6))|| ((screen[i][j+1] == 1 || screen[i][j+1] == 2 || screen[i][j+1] == 6) && (screen[i-1][j] == 1 || screen[i-1][j] == 2 || screen[i-1][j] == 6))){k++;}}}}/*这⾥也是遍历了整个数组,判断所有的箱⼦四个⽅向的情况,如果有三个⽅向被堵住了说明箱⼦⽆法移动了,也表明这个箱⼦失效了,⽤k来记录失效的个数,当全部失效时游戏失败(这是游戏的玩法,其实有⼀个被堵住就已经不可能胜利了)*/}if(k==cum()){k=1;return k;//返回1说明游戏失败}接下来是最重要的四个控制函数向上移动通过数字的变化来控制⼆维数组的变化,进⽽控制地图的更新这⾥⾮常重要的就是要理解:加1,加2,加3减3都是什么意思加1:箱⼦的值是2,⼈的值是3,所以箱⼦的位置变成⼈需要加1来实现加2:空地的值是0,箱⼦的值是2,箱⼦和终点在⼀起的值是6,所以在推箱⼦的时候,前⽅的空格或者终点放上箱⼦后数值会加2加3减3:⼈的值是3,⼈要动的话,它原先在的格⼦就会因为⼈⾛了导致数值减3,⾛到的那个格⼦就会因为站了⼈⽽加3如果这个理解的话,代码就⾮常简单了void movew(){if(x>0){if(screen[x-1][y]==1){return ;/*如果箱⼦的上⾯是墙,则地图不会发⽣变化,因为推不动嘛*/}else if(screen[x-1][y]==0){screen[x-1][y]+=3;screen[x][y]-=3;x--;/*如果前⾯是空地,则需要向前移动⼀格,也就是原先⼈的位置变成空地,前⽅的空地变成⼈,空地(0)变成⼈(3)需要加3,⼈变成空地需要减3*/}else if(screen[x-1][y]==4){screen[x-1][y]+=3;screen[x][y]-=3;x--;}//⼀样的else if(screen[x-1][y]==2||screen[x-1][y]==6){if(screen[x-2][y]==0){screen[x-2][y]+=2;//箱⼦前⾯的格变成箱⼦(2)screen[x-1][y]+=1;//箱⼦的位置变成⼈(3)screen[x][y]-=3;/*如果前⾯是空地,则需要向前移动⼀格,也就是原先是箱⼦的格⼦变成⼈,⼈的位置变成空地,原先的空地变成箱⼦,箱⼦(2)变成⼈(3)需要减3,空地变成⼈*/x--;}else if(screen[x-2][y]==1){return ;}else if(screen[x-2][y]==2){return;//如果箱⼦的前⾯是墙或者其他的箱⼦,则箱⼦推不动}else if(screen[x-2][y]==4){screen[x-2][y]+=2;screen[x-1][y]+=1;screen[x][y]-=3;x--;}//这个情况别漏了}}}其他三个⽅向的代码思路和这个是⼀样的向下移动void moves(){if(x<9){if(screen[x+1][y]==1){return ;}else if(screen[x+1][y]==0){screen[x+1][y]+=3;screen[x][y]-=3;x++;}else if(screen[x+1][y]==4){screen [x+1][y]+=3;screen[x][y]-=3;}else if(screen[x+1][y]==2||screen[x+1][y]==6){ if(screen[x+2][y]==1){return;}else if(screen[x+2][y]==0){screen[x+2][y]+=2;screen[x+1][y]+=1;screen[x][y]-=3;x++;}else if(screen[x+2][y]==2){return ;}else if(screen[x+2][y]==4){screen[x+2][y]+=2;screen[x+1][y]+=1;screen[x][y]-=3;x++;}}}}向左移动void movea(){if(y>0){if(screen[x][y-1]==1){return;}else if(screen[x][y-1]==4){screen[x][y-1]+=3;screen[x][y]-=3;y--;}else if(screen[x][y-1]==0){screen[x][y-1]+=3;screen[x][y]-=3;y--;}else if(screen[x][y-1]==2||screen[x][y-1]==6){ if(screen[x][y-2]==0){screen[x][y-2]+=2;screen[x][y-1]+=1;screen[x][y]-=3;y--;}else if(screen[x][y-2]==1){return;}else if(screen[x][y-2]==2){return;}else if(screen[x][y-2]=4){screen[x][y-2]+=2;screen[x][y-1]+=1;screen[x][y]-=3;y--;}}}}向右移动void moved(){if(y<9){if(screen[x][y+1]==1){return;}else if(screen[x][y+1]==4){screen[x][y+1]+=3;screen[x][y]-=3;y++;}else if(screen[x][y+1]==0){screen[x][y+1]+=3;screen[x][y]-=3;y++;}elseif(screen[x][y+1]==2||screen[x][y+1]==6){if(screen[x][y+2]==0){screen[x][y+2]+=2;screen[x][y+1]+=1;screen[x][y]-=3;y++;}else if(screen[x][y+2]==4){screen[x][y+2]+=2;screen[x][y+1]+=1;screen[x][y]-=3;y++;}else if(screen[x][y+2]==2){return;}else if(screen[x][y+2]==1){return;}}}}主函数这个主函数写的有点乱,直接看注释吧int main(){int n,t;int j,k;int b=1;here:system("cls");//printf("开始游戏请按1\n退出游戏请按2\n");scanf("%d",&j);if(j==1){printf("请⽤wsad代表上下左右来进⾏游戏\n");//这个就引导进⼊游戏while(1){system("cls");/*在每⼀次移动过后都清除上⼀个地图,不然就会每⾛⼀步⽣成⼀个图*/print();//先打印地图scanf("%c",&n);//读⼊⽤户的操作switch(n){case 'w':movew();break;case 's':moves();break;case 'a':movea();break;case 'd':moved();break;} //控制⼈移动t=win();if(t==1){goto there;}//每次操作完先判断游戏是否胜利,如果胜利了直接跳到函数最后if(b == lose()){system("cls");print();printf("游戏失败");return 0;} //游戏失败提⽰}}else {system("cls");printf("您确认要退出游戏吗\n确认退出按1\t返回上⼀层按2\n");scanf("%d",&k);if(k==1){printf("你已退出游戏,期待你的再次到来,谢谢");return 0;}else {goto here;}}//这⼀块是最前⾯⽤户进⼊游戏那⾥的,如果⽤户选择退出游戏执⾏的操作 there:printf("恭喜你通过了游戏!");return 0;}//主函数所有的代码就到这⾥了,如果需要完整代码可以留⾔以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

C语言推箱子源代码推箱子代码完善

C语言推箱子源代码推箱子代码完善

#include <stdio.h>#include <stdlib.h>#include <Windows.h>typedef struct MAP//地图{int wall[50][2];//墙int box[9][2];//箱子int des[9][2];//终点int overlap[9][2];//箱子与终点的重叠位置int all,now;//总箱子个数,到位箱子个数int x,y;//起点} MAP,*map;//全局变量//int U=1,D=2,L=3,R=4; //定义按键状态//int jump;int ofx=10,ofy=9; //定义xy偏移量int player[2];//玩家位置int nextp[2],nextb[2];//玩家下一步位置,箱子下一步位置MAP Pass[5];//关卡数组map Group,p;//关卡数组指针,当前关卡指针int level;//关卡等级int status;//玩家状态int boxc[9][2],overlapc[9][2];//箱子状态栈,重叠箱子状态栈int nowbox;//当前到位箱子个数int reset;//是否重玩//声明全部函数//void Pos(int x,int y)//设置光标位置{COORD pos;HANDLE hOutput;pos.X=x;pos.Y=y;hOutput=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput,pos);system("color 3B");}void welcometogame()//开始界面{system("title 2017程序设计之推箱子");Pos(28,3);printf("欢迎来到推箱子!");Pos(50,24);printf(" 软件1603 刘刈16110572087\n");Pos(27,9);printf("用↑.↓.←.→控制人物的移动。

c++实现推箱子游戏

c++实现推箱子游戏

c++实现推箱⼦游戏代码实现1 #include<stdio.h>2 #include<windows.h>3 #include<conio.h>4char* playerPosition, *boxPosition[2];5/*初始化地图6 map:地图数组7 n: 地图的⾏数8 m: 地图的列数*/9void csh(char map[15][15], int n, int m)10 {1112for (int i = 0;i < n;i++)13for (int j = 0;j < m;j++)14 map[i][j] = '';1516 }17/*控制⾓⾊向上移动18 map:地图数组*/19void move(int moveX, int moveY)20 {21int i = 0;2223 moveY = moveY * 15;24if (*(playerPosition + (moveX + moveY)) != '@')25 {26if (*(playerPosition + (moveX + moveY)) == '*' )27 {28if (*(playerPosition + 2* (moveX + moveY)) != '@')29 {30if (playerPosition + (moveX + moveY) == boxPosition[0])31 i = 0;32else33 i = 1;34 *boxPosition[i] = '';35 boxPosition[i] += (moveX + moveY);36 *boxPosition[i] = '*';37 *playerPosition = '';38 playerPosition += (moveX + moveY);39 *playerPosition = '8';40 }41 }42else43 {44 *playerPosition = '';45 playerPosition += (moveX+moveY);46 *playerPosition = '8';47 }48 }4950 }51/*输出结果52 map:地图数组53 n: 地图的⾏数54 m: 地图的列数*/55void sc(char map[15][15],int n,int m)56 {57for (int i = 0;i < n;i++)58 {59for (int j = 0;j < m;j++)60 printf("%c ", map[i][j]);61 printf("\n");62 }63 }64/*进⾏重开、跳关、移动的判断,进⼊相应的函数,并传参给相应函数65 map:地图数组*/66char pd(char map[15][15])67 {68char pd;6970 pd = _getch();71switch (pd)72 {73case'w':move(0,-1);break;74case's':move(0, 1);break;75case'a':move(-1, 0);break;76case'd':move(1, 0);break;77case13:return1;break;78case32:return2;79 }80return0;81 }8283/*第⼀关*/84void diyiguan()85 {86int cnt = 0;87char map[15][15],cz;88 boolean isEnd = false, isAgain = false, isNext = false;8990 csh(map, 8, 6);91//⽣成地图92 map[0][1] = map[0][2] = map[0][0] = map[1][0] = map[2][0] = map[3][0] = map[4][0] = map[5][0] = map[6][0] = map[7][0] = map[7][1] = map[7][2] = map[7][3] = map[6][3] = map[5][3] = map[4][3] = map[4][4] = map[4][5] = map[93 map[6][2] = 'O';94 playerPosition = &map[3][1];95 *playerPosition = '8';96 boxPosition[0] = &map[4][2];97 *boxPosition[0] = '*';9899100//开始游戏101 sc(map, 8, 6);102 printf("第⼀关");103while (!isEnd && !isAgain && !isNext)104 {105 cz = pd(map);106 cnt++;107if (map[6][2] == '')108 map[6][2] = 'O';109if (cz == 1) isAgain = true;110if (cz == 2) isNext = true;111 system("cls");112 sc(map, 8, 6);113 printf("已⾛ % d步\n第⼆关, 点击回车重开本关,点击空格跳过本关",cnt);114if (map[6][2] == '*')115 isEnd = true;116 }117 system("cls");118if (cz == 1)119 diyiguan();120121122123 }124/*第⼆关*/125void dierguan()126 {127int cnt = 0;128char map[15][15], cz;129 boolean isEnd = false, isAgain = false, isNext = false;130131 csh(map, 8, 6);132//⽣成地图133for (int i = 0;i <= 5; i++)134 map[0][i] = map[7][i]='@';135for (int i = 0;i <= 7; i++)136 map[i][0] = map[i][5] = '@';137 map[4][4]='@';138 map[6][2] = 'O';139 map[6][4] = 'O';140 playerPosition = &map[3][1];141 *playerPosition = '8';142 boxPosition[0] = &map[4][2];143 *boxPosition[0] = '*';144 boxPosition[1] = &map[3][3];145 *boxPosition[1] = '*';146147148//开始游戏149 sc(map, 8, 6);150 printf("第⼆关");151while (!isEnd && !isAgain && !isNext)152 {153 cz = pd(map);154 cnt++;155if (map[6][2] == '')map[6][2] = 'O';156if (map[6][4] == '')map[6][4] = 'O';157if (cz == 1) isAgain = true;158if (cz == 2) isNext = true;159 system("cls");160 sc(map, 8, 6);161 printf("已⾛%d步\n第⼆关,点击回车重开本关,点击空格跳过本关",cnt); 162if (map[6][2] == '*'&&map[6][4]=='*')163 isEnd = true;164 }165 system("cls");166if (cz == 1)167 dierguan();168 }169170void main()171 {172 diyiguan();173 dierguan();174 printf("\n恭喜你成功通关(这么简单的关卡真的有什么好恭喜的吗?)\n"); 175 }运⾏截图。

C语言推箱子代码

C语言推箱子代码

#include<stdio.h>#include<stdlib.h>int i,j,steps = 0,gk = 1;char command,t,m = 0,n = 0;int map_1[8][11],map[8][11] = {{0,0,0,0,0,0,0,0,0,0,0},{1,1,1,1,0,0,0,0,0,0,0},{1,0,0,1,1,1,1,1,0,0,0},{1,0,0,6,0,0,4,1,0,0,0},{1,3,0,2,0,0,4,1,0,0,0},{1,0,0,1,1,1,1,1,0,0,0},{1,1,1,1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0}};int map_2[8][11] = {{0,0,0,1,1,1,1,0,0,0,0},{0,0,0,1,4,0,1,0,0,0,0},{1,1,1,1,0,0,1,1,0,0,0},{1,0,0,0,3,2,0,1,0,0,0},{1,0,1,1,0,0,0,1,0,0,0},{1,0,6,0,0,0,0,1,1,0,0},{1,1,1,1,0,0,0,4,1,0,0},{0,0,0,1,1,1,1,1,1,0,0}};int map_3[8][11] = {{0,0,0,1,1,1,1,0,0,0,0},{0,0,0,1,4,4,1,0,0,0,0},{1,1,1,1,0,0,1,1,0,0,0},{1,0,0,0,0,0,0,1,0,0,0},{1,0,0,0,0,0,0,1,1,0,0},{1,1,1,0,1,6,2,0,1,0,0},{0,0,1,0,0,3,0,0,1,0,0},{0,0,1,1,1,1,1,1,1,0,0}};typedef struct position{int x;int y;}location;location agg[3];//全部通过void clearance(){if(map[agg[1].x][agg[1].y] == 5&&map[agg[2].x + n][agg[2].y + m] == 5&&gk == 3) {printf("你赢了!\n");system("pause");exit(0);}}//判断是否通过void control_adopt(){if(map[agg[1].x][agg[1].y] == 5&&map[agg[2].x + n][agg[2].y + m] == 5) {if(gk != 3){for(i = 0;i < 8;i++)for(j = 0;j < 11;j++){map[i][j] = map_2[i][j];map_2[i][j] = map_3[i][j];map_1[i][j] = map[i][j];}gk++;}steps = 0;}}//重新开始void control_reset(char reset_1){if(reset_1 == 'f'){for(i = 0;i < 8;i++)for(j = 0;j < 11;j++)map[i][j] = map_1[i][j];steps = 0;}}//结束void control_end(char end_0){if(end_0 == 't')exit(0);}//控制方向+ 箱子堵住陷阱void control_direction(char direction_){if(direction_ == 'd'||direction_ == 'w'||direction_ == 'a'||direction_ == 's'){if(direction_ == 'd')m = 1;if(direction_ == 'a')m = -1;if(direction_ == 'w')n = -1;if(direction_ == 's')n = 1;if(agg[0].x + n == agg[1].x&&(agg[0].y + m) == agg[1].y&&map[agg[1].x + n][agg[1].y + m] != 1&&map[agg[1].x][agg[1].y] != 5){if(map[agg[1].x + n][agg[1].y + m] == 4){map[agg[1].x][agg[1].y] = 0;map[agg[1].x + n][agg[1].y + m] = 5;agg[1].y = agg[1].y + m;agg[1].x = agg[1].x + n;}else{t = map[agg[1].x + n][agg[1].y + m];map[agg[1].x + n][agg[1].y + m] = map[agg[1].x][agg[1].y];map[agg[1].x][agg[1].y] = t;agg[1].y = agg[1].y + m;agg[1].x = agg[1].x + n;}}//第二个箱子的判断条件if(agg[0].x + n == agg[2].x&&(agg[0].y + m) == agg[2].y&&map[agg[2].x + n][agg[2].y + m] != 1&&map[agg[2].x][agg[2].y] != 5){if(map[agg[2].x + n][agg[2].y + m] == 4){map[agg[2].x][agg[2].y] = 0;map[agg[2].x + n][agg[2].y + m] = 5;agg[2].y = agg[2].y + m;agg[2].x = agg[2].x + n;}else{t = map[agg[2].x + n][agg[2].y + m];map[agg[2].x + n][agg[2].y + m] = map[agg[2].x][agg[2].y];map[agg[2].x][agg[2].y] = t;agg[2].y = agg[2].y + m;agg[2].x = agg[2].x + n;}}if(map[agg[0].x + n][agg[0].y + m] == 0){t = map[agg[0].x + n][agg[0].y + m];map[agg[0].x + n][agg[0].y + m] = map[agg[0].x][agg[0].y];map[agg[0].x][agg[0].y] = t;agg[0].y = agg[0].y + m;agg[0].x = agg[0].x + n;steps++;}m = 0;n = 0;}}//输出,寻找1,2,3void control_output(){printf("关卡%d:\n",gk);printf("步数:%d\n",steps);for(i = 0;i < 8;i++){for(j = 0;j < 11;j++){if(map[i][j] == 1)printf("■");if(map[i][j] == 0)printf("");if(map[i][j] == 2){printf("□");agg[1].x = i;agg[1].y = j;}if(map[i][j] == 3){printf("⊙");agg[0].x = i;agg[0].y = j;}if(map[i][j] == 4)printf("☆");if(map[i][j] == 5)printf("★");if(map[i][j] == 6){printf("□");agg[2].x = i;agg[2].y = j;}}printf("\n");}printf("w,a,s,d控制方向,t退出游戏,f重新开始!\n");}//输入指令void input_command(){while(1){scanf("%c",&command);if(command == 'w'||command == 'a'||command == 's'||command == 'd'||command == 't'||command == 'f')break;}}main(){for(i = 0;i < 8;i++){for(j = 0;j < 12;j++){map_1[i][j] = map[i][j];}}while(1){control_output();input_command();control_direction(command);control_reset(command);control_end(command);control_adopt();system("cls");clearance();}}【下载本文档,可以自由复制内容或自由编辑修改内容,更多精彩文章,期待你的好评和关注,我将一如既往为您服务】。

推箱子-C语言源码

推箱子-C语言源码

#include <stdio.h>#include <stdlib.h>//用到system()#include <conio.h>//因为需要用到getch()函数//推箱子_C语言版//写了一下午,第一个C语言游戏终于写好了,想想有点小激动....//蔚蓝之链编写//分享给大家参考下(在看得懂的情况下...),代码写得很凌乱.....,勿喷#define X 20#define Y 20#define REN 2 //表示人#define XIANGZI 3 //箱子#define WEIZHI 4 //箱子要推到的位置#define KONG 0 //空位#define QIANG 1 //墙#define RW 6 //人在箱子要推到的位置上#define XW 5 //箱子在箱子要推到的位置上#define bool short#define false 0#define true 1//数组内部数值表示内容:0表示空位,1表示墙体,2表示人,3表示箱子,4表示箱子要推到的位置,5表示既有箱子又有又位置,6表示既有位置又有人//01,2,3,4符号表示://0 :" " 1:■2:♀3:回4:◎void create_map_arr(int _x,int _y,int (*map_arr)[Y]);//创建地图数组void creata_map(int _x,int _y, int (*map)[Y]);//创建地图,重绘游戏画面void amend_map(int _x,int _y,char key , int (* arr_map)[Y]);//接收输入,修改游戏画面bool is_finish(int _x, int _y,int _total ,int (*arr_map)[Y]);//判断是否完成推箱子(地图x,地图y,总箱子数,地图数组)int main(void){int map_arr[X][Y];//存储地图的二维数组create_map_arr(X,Y,map_arr);//创建数组printf("C语言_推箱子v1.0 蔚蓝之链编写\n\n");printf("控制键:W↑,A←,S↓,D→\n");printf("提示:玩本游戏前请先关闭输入法\n");system("pause");system("cls");while(1){creata_map(X,Y,map_arr);//创建地图,重绘游戏画面if(is_finish(X,Y,2,map_arr) == true){printf("恭喜哦,推箱子完成!\n");system("pause");return 0;}amend_map(X,Y,getch(),map_arr);//接收输入,修改画面system("cls");//清空画面}getchar();return 0;}void create_map_arr (int _x,int _y,int (*map_arr)[Y])//创建地图数组{int x,y;for(x=0;x<_x;x++){for (y=0;y<_y;y++){if(y==0 || x==0 || x==_x-1 || y==_y-1){*(*(map_arr+x)+y) = QIANG;//printf ("■");}else{*(*(map_arr+x)+y) = KONG;//printf (" ");}}//printf("\n");}//这里可以修改箱子创建的位置,人创建的位置及箱子要达到的位置,默认两个箱子*(*(map_arr+3)+7)=REN;//人*(*(map_arr+5)+6)=XIANGZI;//箱子*(*(map_arr+5)+7)=XIANGZI;//箱子*(*(map_arr+1)+8)=WEIZHI;//箱子要推到的位置*(*(map_arr+2)+8)=WEIZHI;//箱子要推到的位置}void creata_map(int _x,int _y, int (*map)[Y])//创建地图(地图数组[][]){int x,y;for(x = 0; x < _x; x++){for (y = 0; y < _y; y++){if (*(*(map + x) + y) == QIANG){printf ("■");}else if(*(*(map + x) + y) == KONG){printf (" ");}else if(*(*(map + x) + y)==REN || *(*(map + x) + y)==RW){printf ("♀");}else if (* (*(map + x) + y)==XIANGZI){printf("□");}else if (*(*(map + x) + y)==WEIZHI){printf("◎");}else if(*(*(map + x) + y) == XW){printf("回");}}printf ("\n");}}//这个函数写的太乱了,自己看着也头大,不过还好,总算完成了^_^void amend_map(int _x,int _y,char key , int (* arr_map)[Y])//移动(地图x,地图y,按键值,地图数组){int x,y;for(x = 0; x < _x; x++){for (y = 0; y < _y; y++){if(*( * (arr_map + x ) + y) == REN || *( * (arr_map + x ) + y) == RW){if(key == 'a' || key == 'A')//←{if (y > 0){if(*(*(arr_map + x) +(y)) == RW)//人在最终位置{if(*( * (arr_map + x ) + (y-1)) == WEIZHI)//人左边还是最终位置{*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + y) = WEIZHI;}else if(*( * (arr_map + x ) + (y-1)) == XIANGZI || *( * (arr_map + x ) + (y-1)) == XW){if(*( * (arr_map + x ) + (y-1)) == XW){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + x) + (y - 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + x) + (y - 1)) == KONG){*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + x ) + (y-1)) == WEIZHI)//人左边是最终位置{*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + x ) + (y-1)) == XIANGZI || *( * (arr_map + x ) + (y-1)) == XW){if(*( * (arr_map + x ) + (y-1)) == XW){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y - 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + x) + (y - 1)) == KONG){*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + x ) + (y-1)) == XW || *( * (arr_map + x ) + (y-1)) == XIANGZI)//人左边有箱子或箱子在最终位置{if(*( * (arr_map + x ) + (y-1)) == XIANGZI || *( * (arr_map + x ) + (y-1)) == XW){if(*( * (arr_map + x ) + (y-1)) == XW){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y - 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + x) + (y-1)) == WEIZHI){*(*(arr_map + x) + (y-1)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + x) + (y-1)) == KONG){*(*(arr_map + x) + (y-1)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//←else if(key == 'd' || key == 'D')//→{if (y < _y){if(*(*(arr_map + x) +(y)) == RW)//人在最终位置{if(*( * (arr_map + x ) + (y+1)) == WEIZHI)//人右边还是最终位置{*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + y) = WEIZHI;}else if(*( * (arr_map + x ) + (y+1)) == XIANGZI || *( * (arr_map + x ) + (y+1)) == XW){if(*( * (arr_map + x ) + (y+1)) == XW){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + x) + (y + 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + x) + (y + 1)) == KONG){*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + x ) + (y+1)) == WEIZHI)//人右边是最终位置{*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + x ) + (y+1)) == XIANGZI || *( * (arr_map + x ) + (y+1)) == XW){if(*( * (arr_map + x ) + (y+1)) == XW){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y + 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + x) + (y + 1)) == KONG){*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + x ) + (y+1)) == XW || *( * (arr_map + x ) + (y+1)) == XIANGZI)//人右边有箱子或箱子在最终位置{if(*( * (arr_map + x ) + (y+1)) == XIANGZI || *( * (arr_map + x ) + (y+1)) == XW){if(*( * (arr_map + x ) + (y+1)) == XW){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y + 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + x) + (y+1)) == WEIZHI){*(*(arr_map + x) + (y+1)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + x) + (y+1)) == KONG){*(*(arr_map + x) + (y+1)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//→else if(key == 'w' || key == 'W')//↑{if (x-1 > 0){if(*(*(arr_map + (x)) +(y)) == RW)//人在最终位置{if(*( * (arr_map + (x-1) ) + (y)) == WEIZHI)//人上边还是最终位置{*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + (x) ) + y) = WEIZHI;}else if(*( * (arr_map + (x-1) ) + (y)) == XIANGZI || *( * (arr_map + (x-1) ) + (y)) == XW){if(*( * (arr_map + (x-1) ) + (y)) == XW){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + (x) ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + (x-1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + (x-1)) + (y)) == KONG){*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + (x-1) ) + (y)) == WEIZHI)//人上边是最终位置{*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + (x-1) ) + (y)) == XIANGZI || *( * (arr_map + (x-1) ) + (y)) == XW)//人上边是箱子或在最终位置的箱子{if(*( * (arr_map + (x-1) ) + (y)) == XW){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x-1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + (x-1)) + (y)) == KONG){*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + (x-1) ) + (y)) == XW || *( * (arr_map + (x-1) ) + (y)) == XIANGZI)//人上边有箱子或箱子在最终位置{if(*( * (arr_map + (x-1) ) + (y)) == XIANGZI || *( * (arr_map + (x-1) ) + (y)) == XW){if(*( * (arr_map + (x-1) ) + (y)) == XW){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x-1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + (x-1)) + (y)) == WEIZHI){*(*(arr_map + (x-1)) + (y)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + (x-1)) + (y)) == KONG){*(*(arr_map + (x-1)) + (y)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//↑else if(key == 's' || key == 'S')//↓{if (x < _x+1 > 0){if(*(*(arr_map + (x)) +(y)) == RW)//人在最终位置{if(*( * (arr_map + (x+1) ) + (y)) == WEIZHI)//人下边还是最终位置{*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + (x) ) + y) = WEIZHI;}else if(*( * (arr_map + (x+1) ) + (y)) == XIANGZI || *( * (arr_map + (x+1) ) + (y)) == XW){if(*( * (arr_map + (x+1) ) + (y)) == XW){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + (x) ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + (x+1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + (x+1)) + (y)) == KONG){*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + (x+1) ) + (y)) == WEIZHI)//人下边是最终位置{*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + (x+1) ) + (y)) == XIANGZI || *( * (arr_map + (x+1) ) + (y)) == XW){if(*( * (arr_map + (x+1) ) + (y)) == XW){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x+1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + (x+1)) + (y)) == KONG){*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + (x+1) ) + (y)) == XW || *( * (arr_map + (x+1) ) + (y)) == XIANGZI)//人下边有箱子或箱子在最终位置{if(*( * (arr_map + (x+1) ) + (y)) == XIANGZI || *( * (arr_map + (x+1) ) + (y)) == XW){if(*( * (arr_map + (x+1) ) + (y)) == XW){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x+1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + (x+1)) + (y)) == WEIZHI){*(*(arr_map + (x+1)) + (y)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + (x+1)) + (y)) == KONG){*(*(arr_map + (x+1)) + (y)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//↓return;}}}}bool is_finish(int _x, int _y,int _total ,int (*arr_map)[Y])//判断是否完成推箱子(地图x,地图y,总箱子数,地图数组){int x,y,temp = 0;for (x = 0; x < _x; x++){for (y = 0; y < _y; y++){if (*(*(arr_map+x)+y) == XW){temp++;}}}if (_total == temp){return true;}return false;}。

贪吃蛇俄罗斯方块五子棋推箱子的C语言源代码

贪吃蛇俄罗斯方块五子棋推箱子的C语言源代码

贪吃蛇/俄罗斯方块/五子棋/推箱子/的C语言源代码贪吃蛇。

#defineN200#include<graphics.h>#include<stdlib.h>#include<dos.h>#defineLEFT0x4b00#defineRIGHT0x4d00#defineDOWN0x5000#defineUP0x4800#defineESC0x011binti,key;intscore=0;/*得分*/intgamespeed=50000;/*游戏速度自己调整*/ structFood{intx;/*食物的横坐标*/inty;/*食物的纵坐标*/intyes;/*判断是否要出现食物的变量*/}food;/*食物的结构体*/structSnake{intx[N];inty[N];intnode;/*蛇的节数*/intdirection;/*蛇移动方向*/intlife;/*蛇的生命,0活着,1死亡*/}snake;voidInit(void);/*图形驱动*/voidClose(void);/*图形结束*/voidDrawK(void);/*开始画面*/voidGameOver(void);/*结束游戏*/voidGamePlay(void);/*玩游戏具体过程*/ voidPrScore(void);/*输出成绩*//*主函数*/voidmain(void){Init();/*图形驱动*/DrawK();/*开始画面*/GamePlay();/*玩游戏具体过程*/Close();/*图形结束*/}/*图形驱动*/voidInit(void){intgd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/ voidDrawK(void){/*setbkcolor(LIGHTGREEN);*/setcolor(11);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/for(i=50;i<=600;i+=10)/*画围墙*/{rectangle(i,40,i+10,49);/*上边*/rectangle(i,451,i+10,460);/*下边*/}for(i=40;i<=450;i+=10){rectangle(50,i,59,i+10);/*左边*/rectangle(601,i,610,i+10);/*右边*/}}/*玩游戏具体过程*/voidGamePlay(void){randomize();/*随机数发生器*/food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/snake.life=0;/*活着*/snake.direction=1;/*方向往右*/snake.x[0]=100;snake.y[0]=100;/*蛇头*/snake.x[1]=110;snake.y[1]=100;snake.node=2;/*节数*/PrScore();/*输出得分*/while(1)/*可以重复玩游戏,压ESC键结束*/{while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/{if(food.yes==1)/*需要出现新食物*/{food.x=rand()%400+60;food.y=rand()%350+60;while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/ food.x++;while(food.y%10!=0)food.y++;food.yes=0;/*画面上有食物了*/}if(food.yes==0)/*画面上有食物了就要显示*/{setcolor(GREEN);rectangle(food.x,food.y,food.x+10,food.y-10);}for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/{snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];}/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/switch(snake.direction){case1:snake.x[0]+=10;break;case2:snake.x[0]-=10;break;case3:snake.y[0]-=10;break;case4:snake.y[0]+=10;break;}for(i=3;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/ {if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){GameOver();/*显示失败*/snake.life=1;break;}}if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)/*蛇是否撞到墙壁*/{GameOver();/*本次游戏结束*/snake.life=1;/*蛇死*/}if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/break;if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/{setcolor(0);/*把画面上的食物东西去掉*/rectangle(food.x,food.y,food.x+10,food.y-10);snake.x[snake.node]=-20;snake.y[snake.node]=-20;/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/snake.node++;/*蛇的身体长一节*/food.yes=1;/*画面上需要出现新的食物*/score+=10;PrScore();/*输出新得分*/}setcolor(4);/*画出蛇*/for(i=0;i<snake.node;i++)rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);delay(gamespeed);setcolor(0);/*用黑色去除蛇的的最后一节*/rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);}/*endwhile(!kbhit)*/if(snake.life==1)/*如果蛇死就跳出循环*/break;key=bioskey(0);/*接收按键*/if(key==ESC)/*按ESC键退出*/break;elseif(key==UP&&snake.direction!=4)/*判断是否往相反的方向移动*/snake.direction=3;elseif(key==RIGHT&&snake.direction!=2)snake.direction=1;elseif(key==LEFT&&snake.direction!=1)snake.direction=2;elseif(key==DOWN&&snake.direction!=3)snake.direction=4;}/*endwhile(1)*/}/*游戏结束*/voidGameOver(void){cleardevice();PrScore();setcolor(RED);settextstyle(0,0,4);outtextxy(200,200,"GAMEOVER");getch();}/*输出成绩*/voidPrScore(void){charstr[10];setfillstyle(SOLID_FILL,YELLOW);bar(50,15,220,35);setcolor(6);settextstyle(0,0,2);sprintf(str,"score:%d",score);outtextxy(55,20,str);}/*图形结束*/voidClose(void){getch();closegraph();}-----------------------------------------------------------------------------一下是俄罗斯方块的。

C++实现推箱子游戏源代码

C++实现推箱子游戏源代码

#define qiang 1#define ren 2#define xiangzi 3#define jia 6#define men 8#define space 9int map[13][17] = {0};int fuben[13][17];int num_guan; //现在的关数int num_xiangzi; //箱子的个数int move_num = 0; //人行走的次数int push_num = 0; //人推箱子的次数int people[2]; //M[0] 是人所在位置的横坐标M[1]是人所在位置的纵坐标int door[2]; //门所在的坐标int tem = 0;int cc;void play();void chushi2();void guan1();void guan2();void guan3();void guan4();void guan5();void guan6();void guan7();void guan8();void guan9();void guan10();void guan11();void guan12();void guan13();void guan14();void dayin();int control();int finish();/****************************************************** ****主方法******************************************************* ******************/int main(){void jiemian();void shuoming();void xuanguan();void chushi(int nn);void chushi1();int i;char c;chushi1();for(i = 0;i<=999;i++){jiemian();c = getche();if(c == '1')play();else if(c == '2')xuanguan();else if(c == '3')shuoming();else if(c == '4')break;else system("cls");}}/*************************游戏主界面******************************/void jiemian(){printf("\n\n\n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY | FOREGROUND_BLUE);printf(" ╭╮■■□□◆◆◆◆◆◆◆◆\n");printf(" ││■■□□□□□□□□◆ \n");printf(" ╭─┘└╮■■■■■□■□■◆◆ \n");printf(" ╰─┐┌■■■□┏━━━┓◆◆ \n");printf(" ││■■■■□□□□┃┏━┓┃◆◆ \n");printf(" ╭─┘└╮■■□┃┏━┓┃┏━━━━◆━━┓\n");printf(" ╰─┐┌╯■■■■□□┃┏━┓┃┗━━━━◆━━┛\n");printf(" ╭─┘│■■□□□┃┗━┛┃◆ \n");printf(" ╰──╯■■■■■□□□┗━━━┛◆◆◆◆◆◆ \n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf(" >>1 开始游戏\n\n");printf(" >>2 选择关卡\n\n");printf(" >>3 游戏帮助\n\n");printf(" >>4 退出游戏\n\n");}void chushi1(){int i,j;for(i = 0;i<=12;i++)for (j = 0;j<=16;j++)fuben[i][j] = 0;}void chushi2(){int i,j;move_num = 0;push_num=0;for(i = 0;i<=12;i++)for(j = 0;j<=16;j++){map[i][j] = 0;fuben[i][j] = 0;}}/*************************游戏规则及说明******************************/void shuoming(){int i;char c;for(i = 0;i<=999;i++){system("cls");LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("\t\t\t╭───────────╮\n");printf("\t\t\t│游戏规则及说明│\n");printf("\t\t\t╰───────────╯\n\n ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),BACKGROUND_INTENSITY|FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN |BACKGROUND_BLUE|FOREGROUND_RED|FOREGROUND_RED);printf("囧");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf(" ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("别看了,这就是你了,还挺cool的吧,嘿嘿\n\n\n");LE),FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);printf(" █ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这个是墙,放心,不是豆腐渣工程,要不你可以去撞撞看,\n");printf(" 你会懂得......\n\n\n");printf(" ■ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这个是箱子,就是要你去推的那个,不要试图一下推两个箱\n");printf(" 子,更不要想推着箱子把墙撞开,没用的...不要沮丧,再免\n");printf(" 费给你一句忠告,要是把箱子推到了死角,呵呵,直接下一\n");printf(" 关吧\n\n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_RED);printf(" ★ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这里就是箱子们的最终归宿了,箱子要是回不到家,哼哼,\n");printf(" 你自己掂量着办吧。

推箱子(C语言小游戏代码)

推箱子(C语言小游戏代码)

#include<stdio.h> #include<stdlib.h>#include<windows.h>#include<conio.h>//打印地图void DrawMap();//设置颜色void SetColor(int nColor);//获取玩家坐标POINT GetGamerPosition();//上void Up();//下void Down();//左void Left();//右void Right();//获取空箱子个数int GetSpareBox();//地图//0:空地;3箱子的目的地4箱子// 6人7箱子与目的地的集合//1:墙壁;9人在箱子的目的地int g_map[2][10][12]={{{0,0,0,0,1,1,1,0,0,0,0,0 },{0,0,0,0,1,3,1,0,0,0,0,0 },{0,0,0,0,1,0,1,1,1,1,1,1 },{1,1,1,1,1,4,0,4,0,0,3,1 },{1,3,0,0,0,4,6,1,1,1,1,1 },{1,1,1,1,1,1,4,1,0,0,0,0 },{0,0,0,0,0,1,0,1,0,0,0,0 },{0,0,0,0,0,1,0,1,0,0,0,0 },{0,0,0,0,0,1,3,1,0,0,0,0 },{0,0,0,0,0,1,1,1,0,0,0,0 },},{{1,1,1,1,1,0,0,0,0,0,0,0 },{1,0,0,0,1,0,1,1,1,0,0,0 },{1,0,4,0,1,0,1,1,1,1,1,1 },{1,0,4,6,1,0,1,0,0,0,3,1 },{1,1,1,4,1,1,1,0,0,0,3,1 },{1,1,1,0,0,0,0,0,0,0,3,1 },{0,1,0,0,0,1,0,0,0,0,0,1 },{0,1,0,0,0,1,0,0,0,0,0,1 },{0,1,0,0,0,1,1,1,1,1,1,1 },{1,1,1,1,1,1,0,0,0,0,0,0 },}};int g_nCurrentLevel = 0;//当前管卡int main(){//设置窗口大小system("mode con cols=26 lines=11");//设置标题//SetConsoLeTitle("推箱子");char nInput = 0;//输入字符while(1){//如果箱子推完了过关if (0==GetSpareBox())g_nCurrentLevel++;//清屏system("cls");//如果没有关卡,那就恭喜你成功通关。

c语言推箱子代码

c语言推箱子代码

c语言推箱子代码推箱子是一款经典的益智游戏,也是计算机科学中常见的算法案例。

在这篇文章中,我们将通过C语言来实现推箱子游戏的代码。

推箱子游戏中,玩家需要控制一个人物,将箱子推到指定位置。

箱子只能被推,不能被拉,也不能被穿过其他箱子或墙壁。

游戏的目标是将所有的箱子推到指定位置,完成所有的关卡。

我们需要定义游戏中的元素,包括墙壁、箱子、玩家和目标位置。

我们可以使用二维数组来表示游戏地图,其中0表示空地,1表示墙壁,2表示箱子,3表示玩家,4表示目标位置。

例如,下面是一个简单的游戏地图:int map[5][5] = {{1, 1, 1, 1, 1},{1, 0, 0, 0, 1},{1, 0, 2, 4, 1},{1, 0, 3, 0, 1},{1, 1, 1, 1, 1}};接下来,我们需要实现玩家的移动。

玩家可以向上、下、左、右四个方向移动,但是需要避免撞到墙壁或箱子。

我们可以使用循环和条件语句来实现玩家的移动,例如:int x = 2; // 玩家的初始位置int y = 2;char input; // 读取玩家的输入while (1) { // 游戏循环// 打印游戏地图for (int i = 0; i < 5; i++) {for (int j = 0; j < 5; j++) {if (map[i][j] == 0) printf(" "); if (map[i][j] == 1) printf("#"); if (map[i][j] == 2) printf("$"); if (map[i][j] == 3) printf("@"); if (map[i][j] == 4) printf("."); }printf("\n");}// 读取玩家的输入scanf("%c", &input);// 根据玩家的输入移动玩家int new_x = x, new_y = y;if (input == 'w') new_x--;if (input == 's') new_x++;if (input == 'a') new_y--;if (input == 'd') new_y++;// 判断新位置是否可行if (map[new_x][new_y] == 0 || map[new_x][new_y] == 4) { map[x][y] = 0;map[new_x][new_y] = 3;x = new_x;y = new_y;} else if (map[new_x][new_y] == 2) {// 判断箱子的移动int new_box_x = new_x, new_box_y = new_y;if (input == 'w') new_box_x--;if (input == 's') new_box_x++;if (input == 'a') new_box_y--;if (input == 'd') new_box_y++;if (map[new_box_x][new_box_y] == 0 || map[new_box_x][new_box_y] == 4) {map[x][y] = 0;map[new_x][new_y] = 3;map[new_box_x][new_box_y] = 2;x = new_x;y = new_y;}}// 判断游戏是否结束int count = 0;for (int i = 0; i < 5; i++) {for (int j = 0; j < 5; j++) {if (map[i][j] == 2) count++;}}if (count == 0) {printf("Congratulations! You win!\n");break;}}我们需要添加一些辅助功能,例如读取游戏地图和保存游戏进度。

推箱子c语言程序代码

推箱子c语言程序代码

推箱子c语言程序代码推箱子游戏是一款益智类游戏,在游戏中需要利用角色推动箱子到指定位置,并通过通关来获得胜利。

在这里,我将为您介绍一个简单的推箱子的C语言程序代码。

首先,我们需要定义一些变量和常量来描述游戏中的元素和状态:```c#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <windows.h>#define ROW 10 //地图行数#define COL 15 //地图列数//定义角色、箱子和目标的符号#define PLAYER 'P'#define BOX '$'#define TARGET '*'#define WALL '#'#define SPACE ' '接下来,我们需要定义一个结构体来表示地图上的每一个位置,包含如下元素:- `type`:表示该位置的类型,可以是空格、墙、箱子、目标或者角色。

- `hasBox`: 表示该位置上是否有箱子,是一个布尔值。

```cstruct Position {char type;bool hasBox;};```- `map`:表示地图,是一个二维数组,每个元素都是一个 `Position` 结构体。

- `playerRow` 和 `playerCol`:表示角色当前的位置。

- `boxRow` 和 `boxCol`:表示当前推动的箱子的位置。

```cint playerRow, playerCol, boxRow, boxCol;Position map[ROW][COL];```接下来,我们需要编写地图的初始化函数。

在这个函数中,我们先将地图上所有的位置都标记为空格,并将所有的箱子标记为 `false`。

然后,我们可以将墙、箱子、目标和角色放置到正确的位置上。

c语言推箱子代码

c语言推箱子代码

#i n c l u d e<s t d i o.h> #include <stdlib.h>void map1();void map2();void map3();void show();void move();void main(int argc,char*argv[]){time_t a,b;time(&a);system("color 1E");loop1: {system("cls");//刷屏printf("\n\n\n\n\n\n\t\t\t\t欢迎进入推箱子游戏\n\n\t\t\t\t 请按a b c选择游戏\n\n\t\t\t\t 按 d 键结束");int t=getch();if(t=='d'){printf("\n\t\t\t "); }else{if(t=='a'){map1();goto loop1;}if(t=='b'){map2();goto loop1;}if(t=='c'){map3();goto loop1;}else{printf("\n\n\t\t\t\t 请重新输入:");goto loop1;}}}time(&b);printf("\n\n\n\n\t\t\t\t 游戏总共用时%.3d秒\n\t\t\t ",b-a);getch();//等待读取回车符以结束程序}void map1(){time_t p,q,o;time(&p);int i=4,j=4,m=6,n=2,t=1,k=3; system("cls");//刷屏system("color 2E");int x=1,y=7;char z,a[9][9]={ /*为背景设置初始资料*/ {'#','#','#','#','#','#','#','#','#'},{'#','#','#', 3, 0 , 0 , 0 , 1 ,'#'},{'#','#', 0 , 0 , 0 ,'#', 0 ,'#','#'},{'#', 0 , 0 ,'#', 0 , 0 , 5 , 0 ,'#'},{'#', 0 , 5 , 5 , 3 , 0 , 0 , 0 ,'#'},{'#','#', 0 ,'#', 0 , 0 ,'#','#','#'},{'#','#', 3 , 0 , 0 , 0 ,'#','#','#'},{'#','#','#','#','#','#','#','#','#'},{'#','#','#','#','#','#','#','#','#'}};show(a);//调用输出函数move(a,&x,&y,i,j,m,n,t,k,&z);if(a[i][j]=='!'&&a[m][n]=='!'&&a[t][k]=='!'){system("cls");//刷屏show(a);printf("\t\t\t\t YOU ARE WIN!\n");time(&q);printf("\t\t\t\t\t 用时%.3d秒\n",q-p);printf("\t\t\t\t*******回车重新选择*******\n\t\t\t\t");getch();}else{time(&o);if(z=='d'){return;}if(o-p>30){loop1: system("cls");//刷屏printf("\t\t\t\t时间已过请重新回车选关!");int g=getch();if(g==13)//判断是否为回车符{return;}else{goto loop1;}}system("cls");//刷屏show(a);//调用输出函数goto loop;}}void map2(){time_t p,q,o;time(&p);int i=2,j=1,m=1,n=3,t=4,k=6;system("cls");//刷屏system("color 2E");int x=4,y=1;char z,b[9][9]={ /*为背景设置初始资料*/ {'#','#','#','#','#','#','#','#','#'},{'#','#','#', 3 , 0 ,'#','#','#','#'},{'#', 3 ,'#', 0 , 0 , 0 , 0 ,'#','#'},{'#', 0 , 5 , 0 ,'#','#', 0 , 0 ,'#'},{'#', 1 , 0 , 5 , 0 , 0 , 3 , 0 ,'#'},{'#', 0 , 0 , 0 , 0 , 0 , 0 ,'#','#'},{'#','#','#', 0 , 0 , 5 ,'#','#','#'},{'#','#','#','#', 0 , 0 ,'#','#','#'},{'#','#','#','#','#','#','#','#','#'}};show(b);//调用输出函数move(b,&x,&y,i,j,m,n,t,k,&z);if(b[i][j]=='!'&&b[m][n]=='!'&&b[t][k]=='!'){system("cls");//刷屏show(b);printf("\t\t\t\t YOU ARE WIN!\n");time(&q);printf("\t\t\t\t\t 用时%.3d秒\n",q-p);printf("\t\t\t\t*******回车重新选择*******\n\t\t\t\t");getch();}else{time(&o);if(z=='d'){return;}if(o-p>30){loop1: system("cls");//刷屏printf("\t\t\t\t时间已过请重新回车选关!");int g=getch();if(g==13)//判断是否为回车符{return;}else{goto loop1;}}system("cls");//刷屏show(b);//调用输出函数goto loop;}}void map3(){time_t p,q,o;time(&p);int i=2,j=1,m=6,n=2,t=7,k=6;system("cls");//刷屏system("color 2E");int x=3,y=6;char z,c[9][9]={ /*为背景设置初始资料*/ {'#','#','#','#','#','#','#','#','#'},{'#','#','#','#','#', 0 , 0 , 0 ,'#'},{'#', 3 ,'#', 0 , 0 , 0 , 0 , 0 ,'#'},{'#', 0 , 5 , 0 , 5 ,'#', 1 ,'#','#'},{'#', 0 , 0 , 0 , 0 , 0 , 5 , 0 ,'#'},{'#','#', 0 , 0 , 0 , 0 , 0 , 0 ,'#'},{'#','#', 3 , 0 ,'#', 0 ,'#', 0 ,'#'},{'#','#','#', 0 , 0 , 0 , 3 ,'#','#'},{'#','#','#','#','#','#','#','#','#'} };show(c);//调用输出函数move(c,&x,&y,i,j,m,n,t,k,&z);if(c[i][j]=='!'&&c[m][n]=='!'&&c[t][k]=='!'){system("cls");//刷屏show(c);printf("\t\t\t\t YOU ARE WIN!\n");time(&q);printf("\t\t\t\t\t 用时%.3d秒\n",q-p);printf("\t\t\t\t*******回车重新选择*******\n\t\t\t\t");getch();}else{time(&o);if(z=='d'){return;}if(o-p>30){loop1: system("cls");//刷屏printf("\t\t\t\t时间已过请重新回车选关!");int g=getch();if(g==13)//判断是否为回车符{return;}else{goto loop1;}}system("cls");//刷屏show(c);//调用输出函数goto loop;}}void show(char b[9][9]){printf("\t\t\t\t***************************\n");printf("\t\t\t\t 推箱子游戏");printf("\n\t\t\t\t***************************\n");printf("\t\t\t\t 人:★墙:■\n\t\t\t\t 箱子:◎目的地:□\n\t\t\t\t 按d 键退出游戏\n\t\t\t\t 限时30秒!");printf("\n\t\t\t\t***************************\n");int i;int k;for(i=0;i<9;i++) //使用循环将地图转换输出{printf("\t\t\t\t ");for(k=0;k<9;k++){if(b[i][k]=='#'){printf("■");}else{if(b[i][k]==3){printf("□");}else{if(b[i][k]==5){printf("◎");}else{if(b[i][k]==1){printf("★");}else{if(b[i][k]==0){printf(" ");}else{if(b[i][k]=='!'){printf("⊙");}elseprintf("%c ",b[i][k]);}}}}}}printf("\n");}printf("\t\t\t\t***************************\n");}void move(char c[9][9],int *x,int *y,int X,int Y,int A,int B,int N,int M,char *t){*t=getch();//用getch()得到上下左右键,要调用二次getch(),上键72,下键80,左键75,右键77.if(*t=='d'){}//遇到d返回if(*t==75)//左键{if(c[*x][*y-1]=='#'||c[*x][*y-1]==5&&c[*x][*y-2]==5||c[*x][*y-1]==5&&c[*x][*y-2]=='!'){}//遇到墙或推两个箱子else{if(c[*x][*y-1]=='!')//把箱子推出目的地{if(c[*x][*y-2]!=0){}else{c[*x][*y]=0;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}}else{if(c[*x][*y-1]==5&&c[*x][*y-2]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //推着箱子离开目的地{c[*x][*y]=3;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}else{c[*x][*y]=0;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}}else{if(c[*x][*y-1]==5&&c[*x][*y-2]=='#'){}//推不动箱子else{if(c[*x][*y-1]==c[X][Y]||c[*x][*y-1]==c[A][B]||c[*x][*y-1]==c[N][M])//进到目的地{c[*x][*y]=0;*y-=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //离开目的地{c[*x][*y]=3;*y-=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*y-=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x][*y-1]='!';}}if(*t==77)//右键{if(c[*x][*y+1]=='#'||c[*x][*y+1]==5&&c[*x][*y+2]==5||c[*x][*y+1]==5&&c[*x][*y+2]=='!'){}//遇到墙或推两个箱子else{if(c[*x][*y+1]=='!')//把箱子推出目的地{if(c[*x][*y+2]!=0){}else{c[*x][*y]=0;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}}else{if(c[*x][*y+1]==5&&c[*x][*y+2]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //推着箱子离开目的地{c[*x][*y]=3;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}else{c[*x][*y]=0;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}}else{if(c[*x][*y+1]==5&&c[*x][*y+2]=='#'){}//推不动箱子else{if(c[*x][*y+1]==c[X][Y]||c[*x][*y+1]==c[A][B]||c[*x][*y+1]==c[N][M])//进到目的地{c[*x][*y]=0;*y+=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //离开目的地{c[*x][*y]=3;*y+=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*y+=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x][*y+1]='!';}}if(*t==72)//上键{if(c[*x-1][*y]=='#'||c[*x-1][*y]==5&&c[*x-2][*y]==5||c[*x-1][*y]==5&&c[*x-2][*y]=='!'){}//遇到墙或推两个箱子else{if(c[*x-1][*y]=='!')//把箱子推出目的地{if(c[*x-2][*y]!=0){}else{c[*x][*y]=0;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}}else{if(c[*x-1][*y]==5&&c[*x-2][*y]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //推着箱子离开目的地{c[*x][*y]=3;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}else{c[*x][*y]=0;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}}else{if(c[*x-1][*y]==5&&c[*x-2][*y]=='#'){}//推不动箱子else{if(c[*x-1][*y]==c[X][Y]||c[*x-1][*y]==c[A][B]||c[*x-1][*y]==c[N][M])//进到目的地{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //离开目的地{c[*x][*y]=3;*x-=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x-1][*y]='!';}}if(*t==80)//下键{if(c[*x+1][*y]=='#'||c[*x+1][*y]==5&&c[*x+2][*y]==5||c[*x+1][*y]==5&&c[*x+2][*y]=='!'){}//遇到墙或推两个箱子else{if(c[*x+1][*y]=='!')//把箱子推出目的地{if(c[*x+2][*y]!=0){}else{c[*x][*y]=0;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}}else{if(c[*x+1][*y]==5&&c[*x+2][*y]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //推着箱子离开目的地{c[*x][*y]=3;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}else{c[*x][*y]=0;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}}else{if(c[*x+1][*y]==5&&c[*x+2][*y]=='#'){}//推不动箱子else{if(c[*x+1][*y]==c[X][Y]||c[*x+1][*y]==c[A][B]||c[*x+1][*y]==c[N][M])//进到目的地{c[*x][*y]=0;*x+=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //离开目的地{c[*x][*y]=3;*x+=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*x+=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x+1][*y]='!';}}}。

推箱子源代码

推箱子源代码

#include<stdio.h>#include<stdlib.h>#define X 6#define Y 1//输出函数void show(char b[9][9]){printf(" 推箱子游戏");printf("\n***************************\n\n");char a1=1,a2=5,a3=3,a4='#';printf(" 人:%c 墙:%c\n 箱子:%c 目的地:%c\n",a1,a4,a2,a3);printf("\n***************************\n\n");for(int i=0;i<9;i++){for(int k=0;k<9;k++){printf(" %c ",b[i][k]);}printf("\n");}printf("\n***************************\n");}//移动函数void move(char c[9][9],int *x,int *y){char t;scanf("%c",&t);////////////////////////////////////////////////////////////////////////////////// if(t=='a'){if(c[*x][*y-1]=='#')//遇到墙{}else{if(c[*x][*y-1]==5&&c[*x][*y-2]==0)//推箱子{c[*x][*y]=0;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}else{if(c[*x][*y-1]==5&&c[*x][*y-2]=='#')//推不动箱子{}else{if(c[*x][*y-1]==c[X][Y])//进到目的地{c[*x][*y]=0;*y-=1;c[*x][*y]=1;}else{if(c[*x][*y-1]==c[X][Y])//离开目的地{c[*x][*y]=3;*y-=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*y-=1;c[*x][*y]=1;}}}}}}else{//////////////////////////////////////////////////////////////////////////////////////// if(t=='s'){if(c[*x+1][*y]=='#')//遇到墙{}else{if(c[*x+1][*y]==5&&c[*x+2][*y]==0)//推箱子{c[*x][*y]=0;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}else{if(c[*x+1][*y]==5&&c[*x+2][*y]=='#')//推不动箱子{}else{if(c[*x+1][*y]==c[X][Y])//进到目的地{c[*x][*y]=0;*x+=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y])//走出目的地{c[*x][*y]=3;*x+=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*x+=1;c[*x][*y]=1;}}}}}}else{////////////////////////////////////////////////////////////////////////////////////// if(t=='w'){if(c[*x-1][*y]=='#')//遇到墙{}else{if(c[*x-1][*y]==5&&c[*x-2][*y]!='#')//推箱子{c[*x][*y]=0;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}else{if(c[*x-1][*y]==5&&c[*x-2][*y]=='#')//推不动箱子{}else{if(c[*x-1][*y]==c[X][Y])//进到目的地{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}else{if(c[*x-1][*y]==c[X][Y])//走出目的地{c[*x][*y]=3;*x-=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}}}}}}else{//////////////////////////////////////////////////////////////////////////////////////////// if(t=='d'){if(c[*x][*y+1]=='#')//遇到墙{}else{if(c[*x][*y+1]==5&&c[*x][*y+2]==0)//推箱子{c[*x][*y]=0;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}else{if(c[*x][*y+1]==5&&c[*x][*y+2]=='#')//推不动箱子{}else{if(c[*x][*y+1]==c[X][Y])//进到目的地{c[*x][*y]=0;*y+=1;c[*x][*y]=1;}else{if(c[*x][*y+1]==c[X][Y])//离开目的地{c[*x][*y]=3;*y+=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*y+=1;c[*x][*y]=1;}}}}}}}}}system("cls");//刷屏}void main(){int x=1,y=7;//设置地图char a[9][9]={{'#','#','#','#','#','#','#','#','#'},{'#','#','#',0,0,'#',0,1,'#'},{'#',0,5,0,0,0,0,0,'#'},{'#',0,0,0,0,'#',0,0,'#'},{'#',0,'#','#',0,0,0,0,'#'},{'#','#',0,0,'#',0,'#','#','#'},{'#',3,0,0,0,0,'#','#','#'},{'#','#','#',0,0,'#','#','#','#'},{'#','#','#','#','#','#','#','#','#'}};show(a);loop: move(a,&x,&y);show(a);goto loop;}。

c语言推箱子代码

c语言推箱子代码

# i n c l#include <>void map1();void map2();void map3();void show();void move();void main(int argc,char*argv[]){time_t a,b;time(&a);system("color 1E");loop1: {system("cls");d 秒\n\t\t\t getch();d 秒\n",q-p);printf("\t\t\t\t******* 回车u d e < >",b-a);新选择*******\n\t\t\t\t");getch();else{time(&o);if(z=='d'){return;} if(o-p>30){loop1: system("cls");dprintf("\t\t\t\t*******getch();}elsetime(&o);秒\n",q-p);回车重新选择*******\n\t\t\t\t");if(z=='d'){return;}if(o-p>30){loop1: system("cls");d 秒\n",q-p);printf("\t\t\t\t******* 回车重新选择*******\n\t\t\t\t"); getch();}else{time(&o);if(z=='d'){return;if(o-p>30){}loop1: system("cls"); if(*t=='d'){}// 遇到d 返回if(*t==75)// 左键{if(c[*x][*y-1]=='#'||c[*x][*y-1]==5&&c[*x][*y-2]==5||c[*x][*y-1]==5&&c[*x][*y-2]=='!'){}// 遇到墙或推两个箱子else{if(c[*x][*y-1]=='!')// 把箱子推出目的地{if(c[*x][*y-2]!=0){}else{c[*x][*y]=0;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}}else{if(c[*x][*y-1]==5&&c[*x][*y-2]!='#')//{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B] ||c[*x][*y]==c[N][M]) //{c[*x][*y]=3;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}elsec[*x][*y]=0;推箱子推着箱子离开目的地{*y-=1;c[*x][*y]=1; *y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}}else{if(c[*x][*y-1]==5&&c[*x][*y-2]=='#'){}//else{if(c[*x][*y-1]==c[X][Y]||c[*x][*y-1]==c[A][B] ||c[*x][*y-1]==c[N][M])// 进到目的地{c[*x][*y]=0;推不动箱子c[*x][*y]=1; } else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //{c[*x][*y]=3;*y-=1;c[*x][*y]=1;}else // 走到空地{c[*x][*y]=0;*y-=1;}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)// 箱子推到目的地离开目的地{c[*x][*y-1]='!';}}if(*t==77)// 右键{if(c[*x][*y+1]=='#'||c[*x][*y+1]==5&&c[*x][*y+2]==5||c[*x][*y+1]==5&&c[*x][*y+2]=='!'){}// 遇到墙或推两个箱子else{if(c[*x][*y+1]=='!')// 把箱子推出目的地{if(c[*x][*y+2]!=0){} else{c[*x][*y]=0;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}}else{if(c[*x][*y+1]==5&&c[*x][*y+2]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]c[*x][*y]=3;||c[*x][*y]==c[N][M]) // 推着箱子离开目的地*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}else{c[*x][*y]=0;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}}else{elseif(c[*x][*y+1]==5&&c[*x][*y+2]=='#'){}// 推不动箱子{c[*x][*y]=1;else // 走到空地if(c[*x][*y+1]==c[X][Y]||c[*x][*y+1]==c[A][B] ||c[*x][*y+1]==c[N][M])//{c[*x][*y]=0; *y+=1; c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B] ||c[*x][*y]==c[N][M]) // {c[*x][*y]=3; *y+=1;}进到目的地离开目的地{{c[*x][*y]=0;*y+=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)// 箱子推到目的地{c[*x][*y+1]='!';}}if(*t==72)// 上键if(c[*x-1][*y]=='#'||c[*x-1][*y]==5&&c[*x-2][*y]==5||c[*x-1][*y]==5&&c[*x-2][*y]=='!'){}//遇到墙或推两个箱子else{if(c[*x-1][*y]=='!')// 把箱子推出目的地{if(c[*x-2][*y]!=0){}else{c[*x][*y]=0;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}}else{c[*x-1][*y]=5;}if(c[*x-1][*y]==5&&c[*x-2][*y]!='#')//{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B] ||c[*x][*y]==c[N][M]) //{c[*x][*y]=3; *x-=1; c[*x][*y]=1; c[*x-1][*y]=5; } else {c[*x][*y]=0; *x-=1; c[*x][*y]=1;推箱子推着箱子离开目的地else{if(c[*x-1][*y]==5&&c[*x-2][*y]=='#'){}//推不动箱子else{if(c[*x-1][*y]==c[X][Y]||c[*x-1][*y]==c[A][B]||c[*x-1][*y]==c[N][M])// 进到目的地{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}else{||c[*x][*y]==c[N][M]) // 离开目的地if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]{}c[*x][*y]=3;*x-=1;c[*x][*y]=1;}else //{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}}}}}走到空地if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)// 箱子推到目的地c[*x][*y]=0;*x+=1;c[*x-1][*y]='!'; }}if(*t==80)// 下键{if(c[*x+1][*y]=='#'||c[*x+1][*y]==5&&c[*x+2][*y]==5||c[*x+1][*y]==5&&c[*x+2][*y]=='!'){}// else{if(c[*x+1][*y]=='!')// 把箱子推出目的地{if(c[*x+2][*y]!=0){} else{遇到墙或推两个箱子c[*x][*y]=1;c[*x+1][*y]=5;}}else{if(c[*x+1][*y]==5&&c[*x+2][*y]!='#')// {if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B] ||c[*x][*y]==c[N][M]) //{c[*x][*y]=3;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;} else推箱子推着箱子离开目的地{c[*x][*y]=0;*x+=1;c[*x][*y]=0; *x+=1; c[*x][*y]=1; c[*x+1][*y]=5; }}else{if(c[*x+1][*y]==5&&c[*x+2][*y]=='#'){}// else {if(c[*x+1][*y]==c[X][Y]||c[*x+1][*y]==c[A][B] ||c[*x+1][*y]==c[N][M])// 进到目的地 {推不动箱子c[*x][*y]=1;c[*x][*y]=1;}}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B] ||c[*x][*y]==c[N][M]) // {c[*x][*y]=3; *x+=1; c[*x][*y]=1; }else // 走到空地 {c[*x][*y]=0; *x+=1;离开目的地}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x+1][*y]='!';}}}。

c推箱子

c推箱子

# include "conio.h"# include "graphics.h"char map[12][19]={{'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'}, {'.','.','.','.','#','#','#','#','#','#','#','#','#','#','#','.','.','.','.'},{'.','.','.','.','#',' ',' ',' ',' ','#',' ',' ',' ',' ','#','.','.','.','.'},{'.','.','.','.','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','.','.','.','.'},{'.','.','.','.','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','.','.','.','.'},{'.','.','.','.','#','#','#','#','#',' ','#','#','#','#','#','.','.','.','.'},{'.','.','.','.','.','.','.','#',' ',' ',' ','#','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','#',' ',' ',' ','#','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','#',' ',' ',' ','#','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','#',' ',' ',' ','#','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','#','#','#','#','#','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'}};struct move_point{int x,y;}man;int num;int step=0;int xi=0,re=0;void draw(){gotoxy(man.x,man.y);printf("%c\b",2);}void clr(){gotoxy(man.x,man.y);printf(" \b");}void drawmap(){int i,j;gotoxy(0,0);for(i=0;i<12;i++){for(j=0;j<19;j++)printf("%c",map[i][j]);printf("\n");}}int BI(int x,int y){if(map[y-1][x-1]=='#'||map[y-1][x-1]==15) return 1;else return 0;}int XI_W(int x,int y){if(map[y-1][x-1]==15&&BI(x,y-1)==0) {map[y-1][x-1]=' '; map[y-2][x-1]=15;xi=1;gotoxy(x,y-1);printf("%c",15);return 0;}if(map[y-1][x-1]==' ') {xi=2;return 0;}return 1;}int XI_A(int x,int y){if(map[y-1][x-1]==15&&BI(x-1,y)==0) {map[y-1][x-1]=' '; map[y-1][x-2]=15;xi=1;gotoxy(x-1,y);printf("%c",15);return 0;}if(map[y-1][x-1]==' ') {xi=2;return 0;}return 1;}int XI_S(int x,int y){if(map[y-1][x-1]==15&&BI(x,y+1)==0) {map[y-1][x-1]=' '; map[y][x-1]=15;xi=1;gotoxy(x,y+1);printf("%c",15);return 0;}if(map[y-1][x-1]==' ') {xi=2;return 0;}return 1;}int XI_D(int x,int y){if(map[y-1][x-1]==15&&BI(x+1,y)==0) {map[y-1][x-1]=' ';map[y-1][x]=15;xi=1;gotoxy(x+1,y);printf("%c",15);return 0;}if(map[y-1][x-1]==' ') {xi=2;return 0;}return 1;}void draw_des(){num=6;if(map[6][10]==' ') gotoxy(11,7),printf("+");else num--;if(map[7][10]==' ') gotoxy(11,8),printf("+");else num--;if(map[8][10]==' ') gotoxy(11,9),printf("+");else num--;if(map[9][10]==' ') gotoxy(11,10),printf("+");else num--;if(map[8][8]==' ') gotoxy(9,9),printf("+");else num--;if(map[8][9]==' ') gotoxy(10,9),printf("+");else num--;if(num==0) gotoxy(23,12),printf("You are very clever!"),getch(),exit(0); }void draw_step(){gotoxy(38,6);printf("%d",step);}void draw_biaoge(){line(170,0,170,200);line(0,200,340,200);line(170,60,340,60);line(340,0,340,200);line(0,205,345,205);line(345,0,345,205);line(165,0,165,200);line(260,60,260,160);line(170,5,340,5);line(170,160,340,160);}void FAN(){switch(re){case 1:clr();man.y++;draw();if(xi==1) {map[man.y-3][man.x-1]=' ';map[man.y-2][man.x-1]=15;gotoxy(man.x,man.y-2);printf(" ");gotoxy(man.x,man.y-1);printf("%c\b",15);}break;case 2:clr();man.x++;draw();if(xi==1) {map[man.y-1][man.x-3]=' ';map[man.y-1][man.x-2]=15;gotoxy(man.x-2,man.y);printf(" ");gotoxy(man.x-1,man.y);printf("%c\b",15);}break;case 3:clr();man.y--;draw();if(xi==1) {map[man.y+1][man.x-1]=' ';map[man.y][man.x-1]=15;gotoxy(man.x,man.y+2);printf(" ");gotoxy(man.x,man.y+1);printf("%c\b",15);}break;case 4:clr();man.x--;draw();if(xi==1) {map[man.y-1][man.x+1]=' ';map[man.y-1][man.x]=15;gotoxy(man.x+2,man.y);printf(" ");gotoxy(man.x+1,man.y);printf("%c\b",15);}break;}re=0;xi=0;}main(){int driver=DETECT,mode;char ch;man.x=12;man.y=4;map[3][6]=15;map[3][7]=15;map[3][8]=15;map[3][9]=15;map[3][10]=15;map[3][12]=15;clr();registerbgidriver(EGAVGA_driver); initgraph(&driver,&mode,""); setbkcolor(GREEN);drawmap();draw_des();draw_step();draw();draw_biaoge();gotoxy(25,5);printf("w:UP");gotoxy(25,6);printf("a:LEFT");gotoxy(25,7);printf("s:DOWN");gotoxy(25,8);printf("d:RIGHT");gotoxy(25,9);printf("r:QUIT");gotoxy(25,10);printf("f:REGRET");gotoxy(35,5);printf("step:");gotoxy(27,3);printf("BAN YUN GONG");for(;;){if(kbhit()){ch=getch();if(ch=='r') break;switch(ch){case 'w':if(XI_W(man.x,man.y-1)==1) break; clr();step++;man.y--;re=1;draw_des();draw_step();draw();break;case 'a':if(XI_A(man.x-1,man.y)==1) break; clr();step++;man.x--;re=2;draw_des();draw_step();draw();break;case 's':if(XI_S(man.x,man.y+1)==1) break; clr();step++;man.y++;re=3;draw_des();draw_step();draw();break;case 'd':if(XI_D(man.x+1,man.y)==1) break; clr();step++;man.x++;re=4;draw_des();draw_step();draw();break;case 'f':if(re==0) break;else FAN();break;default:break;}}}}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/* 一个C语言编写的推箱子游戏源代码*//* 本游戏是字符模式的,请不要在中文dos下运行。

本游戏在TURBO C下调试通过*/程序预处理部分包括加载头文件、定义全局变量和定义数据结构,并对它们进行初始化工作。

#include <dos.h>#include <stdio.h>#include <ctype.h>#include <conio.h>#include <bios.h>#include <alloc.h>/* 定义二维数组ghouse来记录屏幕上各点的状态,其中:0表示什么都没有,'b'表示箱子,'w'表示墙壁,'m'表示目的地,'i'表示箱子在目的地。

*/ char ghouse[20][20];/* 以下函数为直接写屏函数,很酷的函数哦!是我朋友告诉我的。

*/定义全局变量char far *screen=(char far* )0xb8000000; 用于在屏幕上输出字符。

彩色显示器的字符缓冲区首地址为0xB8000000,每一个字符占2个字节(第一个字节为ASCII值,第二个字节为颜色值),字符模式下屏幕宽80像素,高25像素,一屏可以写80*25个字符。

void putchxy(int y,int x,char ch,char fc,char bc)Putchxy()函数在屏幕上的指定位置输出指定的字符。

其中,x、y指明输出的位置,ch表示输出的字符,fc表示输出的字符颜色,bc 表示背景色。

{screen[(x*160)+(y<<1)+0]=ch; /*屏幕输出字符*/screen[(x*160)+(y<<1)+1]=(bc*16)+fc; /*指定字符颜色fc,背景色bc*/}/* 定义判断是否胜利的数据结构*/typedef struct winer {int x,y;struct winer *p;}winer;定义结构体struct winer用于判断每一关是否已完成。

其中x用于存放目的地的横坐标,y用于存放目的地的纵坐标。

如果所有表示目的地坐标对应的状态都为“i”,即箱子在目的地,则表示已经过关,可以进入下一关。

该结构体的初始化在每一关的初始化时进行。

/* 箱子位置的数据结构*/typedef struct boxs {int x,y;struct boxs *next;}boxs;/* 在特定的坐标上画墙壁并用数组记录状态的函数*/void printwall(int x,int y)printwall()用于画墙传入参数x、y指明位置。

该函数调用putoutChar()进行输出,以黑色为背景画绿色墙,用小方块表示墙(ASCII值为219)。

{ /*以黑色为背景画绿色墙,用小方块表示*/putchxy(y-1,x-1,219,GREEN,BLACK);/*记录状态为墙*/ghouse[x][y]='w';}/* 在特定的坐标上画箱子并用数组记录状态的函数*/void printbox(int x,int y) 用于在非目的地画箱子printBox()函数用于在非目的地画箱子,传入参数x、y指明位置。

该函数调用putchxy()进行输出,以黑色为背景白色箱子,用ASCII值为10的字符表示箱子。

{putchxy(y-1,x-1,10,WHITE,BLACK);ghouse[x][y]='b';}/* 在特定的坐标上画目的地并用数组记录状态的函数*/void printwhither1(int x,int y,winer **win,winer **pw) 画目的地函数,并记录每个目的地的位置。

void printwhither1()函数与printwhither1()函数功能基本相同,都是画目的地函数,但是printDestination1()增加了记录每一个目的地位置的功能。

其中x、y指明目的地的位置,每一关的所有目的地位置存放在结构体struct winer中,形成一条链表,**winer返回链表的头,**pw则指向链表的尾部。

{winer *qw;putchxy(y-1,x-1,'*',YELLOW,BLACK); 。

ghouse[x][y]='m';if(*win==NULL){*win=*pw=qw=(winer* )malloc(sizeof(winer));(*pw)->x=x;(*pw)->y=y;(*pw)->p=NULL;}else{qw=(winer* )malloc(sizeof(winer));qw->x=x;qw->y=y;(*pw)->p=qw;(*pw)=qw;qw->p=NULL;}}/* 在特定的坐标上画目的地并用数组记录状态的函数*/void printwhither(int x,int y) 画目的地函数Printwhither()函数用于画目的地,传入参数x、y指明位置。

该函数调用putoutChar()进行输出,以黑色为背景画黄色目的地,用‘*’型表示(ASCII值为003)。

{putchxy(y-1,x-1,'*',YELLOW,BLACK);ghouse[x][y]='m';}/* 在特定的坐标上画人的函数*/void printman(int x,int y)printman()函数用于画小人。

X、y指明画的位置。

该函数通过软中断来实现,首先设置寄存器AX的高位和低位,设置高位0xa表示在光标位置显示字符;设置低位02(ASCII值),表示输出的字符;然后设置寄存器CX为01,表示重复输出的次数,这里只输出一次;最后产生类型为0x10的中断,表示显示器输出。

{gotoxy(y,x);_AL=02;_CX=01;_AH=0xa;geninterrupt(0x10);}/* 在特定的坐标上画箱子在目的地上并用数组记录状态的函数*/void printboxin(int x,int y) 在目的地画箱子printboxin()函数用于在目的地画箱子,传入参数x、y指明位置。

该函数调用putchxy()进行输出,以黑色为背景画黄色箱子,仍用ASCII值为10的字符表示箱子。

{putchxy(y-1,x-1,10,YELLOW,BLACK);ghouse[x][y]='i';}/* 初始化函数,初始化数组和屏幕*/void init(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++) /*屏幕20*20范围内状态初始化为0,表示什么都没有*/ghouse[i][j]=0;_AL=3; /*设置寄存器AX低位,80*25彩色方式显示*/_AH=0; /*设置寄存器AX高位*/geninterrupt(0x10);/*移动光标到指定位置输出屏幕信息*/gotoxy(40,4);printf("Welcome to come box world!"); 欢迎参加推箱子游戏gotoxy(40,6);printf("Press up,down,left,right to play.");/*按上下左右玩游戏*/gotoxy(40,8);printf("Press Esc to quit it."); /*按Esc退出游戏*/gotoxy(40,10);printf("Press space to reset the game.");/*按空格键重新开始游戏*/gotoxy(40,12);printf("Producer : wangdehao."); /*玩家玩的好*/gotoxy(40,14);printf("Mar. 30th 2003."); /*日期2003年3月30日*/}/* 第一关的图象初始化*/winer *inithouse1(){int x,y;winer *win=NULL,*pw;for(x=1,y=5;y<=9;y++)printwall(x+4,y+10);for(y=5,x=2;x<=5;x++)printwall(x+4,y+10);for(y=9,x=2;x<=5;x++)printwall(x+4,y+10);for(y=1,x=3;x<=8;x++)printwall(x+4,y+10);for(x=3,y=3;x<=5;x++)printwall(x+4,y+10);for(x=5,y=8;x<=9;x++)printwall(x+4,y+10);for(x=7,y=4;x<=9;x++)printwall(x+4,y+10);for(x=9,y=5;y<=7;y++)printwall(x+4,y+10);for(x=8,y=2;y<=3;y++)printwall(x+4,y+10); printwall(5+4,4+10);printwall(5+4,7+10);printwall(3+4,2+10);printbox(3+4,6+10);printbox(3+4,7+10);printbox(4+4,7+10);printwhither1(4+4,2+10,&win,&pw); printwhither1(5+4,2+10,&win,&pw); printwhither1(6+4,2+10,&win,&pw); printman(2+4,8+10);return win;}/* 第三关的图象初始化*/winer *inithouse3(){int x,y;winer *win=NULL,*pw;for(x=1,y=2;y<=8;y++)printwall(x+4,y+10);for(x=2,y=2;x<=4;x++)printwall(x+4,y+10);for(x=4,y=1;y<=3;y++)printwall(x+4,y+10);for(x=5,y=1;x<=8;x++)printwall(x+4,y+10);for(x=8,y=2;y<=5;y++)printwall(x+4,y+10);for(x=5,y=5;x<=7;x++)printwall(x+4,y+10);for(x=7,y=6;y<=9;y++)printwall(x+4,y+10);for(x=3,y=9;x<=6;x++)printwall(x+4,y+10);for(x=3,y=6;y<=8;y++)printwall(x+4,y+10);printwall(2+4,8+10);printwall(5+4,7+10);printbox(6+4,3+10);printbox(4+4,4+10);printbox(5+4,6+10);printwhither1(2+4,5+10,&win,&pw); printwhither1(2+4,6+10,&win,&pw); printwhither1(2+4,7+10,&win,&pw); printman(2+4,4+10);return win;}/* 第二关的图象初始化*/winer *inithouse2(){int x,y;winer *win=NULL,*pw;for(x=1,y=4;y<=7;y++)printwall(x+4,y+10);for(x=2,y=2;y<=4;y++)printwall(x+4,y+10);for(x=2,y=7;x<=4;x++)printwall(x+4,y+10);for(x=4,y=1;x<=8;x++)printwall(x+4,y+10);for(x=8,y=2;y<=8;y++)printwall(x+4,y+10);for(x=4,y=8;x<=8;x++)printwall(x+4,y+10);for(x=4,y=6;x<=5;x++)printwall(x+4,y+10);for(x=3,y=2;x<=4;x++)printwall(x+4,y+10);for(x=4,y=4;x<=5;x++)printwall(x+4,y+10);printwall(6+4,3+10);printbox(3+4,5+10);printbox(6+4,6+10);printbox(7+4,3+10);printwhither1(5+4,7+10,&win,&pw);printwhither1(6+4,7+10,&win,&pw);printwhither1(7+4,7+10,&win,&pw);printman(2+4,6+10);return win;}/* 第四关的图象初始化*/winer *inithouse4(){int x,y;winer *win=NULL,*pw;for(x=1,y=1;y<=6;y++)printwall(x+4,y+10);for(x=2,y=7;y<=8;y++)printwall(x+4,y+10);for(x=2,y=1;x<=7;x++)printwall(x+4,y+10);for(x=7,y=2;y<=4;y++)printwall(x+4,y+10);for(x=6,y=4;y<=9;y++)printwall(x+4,y+10);for(x=3,y=9;x<=5;x++)printwall(x+4,y+10);for(x=3,y=3;y<=4;y++)printwall(x+4,y+10);printwall(3+4,8+10);printbox(3+4,5+10);printbox(4+4,4+10);printbox(4+4,6+10);printbox(5+4,5+10);printbox(5+4,3+10);printwhither1(3+4,7+10,&win,&pw);printwhither1(4+4,7+10,&win,&pw);printwhither1(5+4,7+10,&win,&pw);printwhither1(4+4,8+10,&win,&pw);printwhither1(5+4,8+10,&win,&pw);printman(2+4,2+10);return win;}/* 移动在空地上的箱子到空地上*/movebox(int x,int y,char a) 把箱子从空地移动到空地。

相关文档
最新文档