推箱子游戏的代码)

合集下载

推箱子代码

推箱子代码
arr[sr][sc]=2;
for(i=0;i<N;i++){
for(j=0;j<N;j++)
printf("%d",arr[i][j]);
printf("\n");
}
if(sr==0&&sc==N-1) {
printf("恭喜你成功了!\n");
break;
{ for(j=0;j<N;j++)
printf("%d",arr[i][j]);
printf("\n");
}
//人移动
int fx;
for(;;)
{ arr[pr][pc]=0;
arr[sr][sc]=0;
for(;;){ //输入移动的方向 定义 1左移,2右移,3上移,4下移
if(pr!=sr&&pc!=sc){ //不在同行且同列
if(fx==1){
if(pc>0) pc--;
else ;
}
if(fx==2){
if(pc<N-1) pc++;
else ;
}
sr=rand()%8+1;//箱子的随机位置
sc=rand()%8+1;
if(pr!=sr||pc!=sc) break;
}
arr[pr][pc]=1;//标示人的位置
arr[sr][sc]=2;//标示箱子的位置
intN;i++) //打印初始位置图

推箱子代码

推箱子代码

推箱子代码#include&lt;iostream&gt;#include&lt;string&gt;#include&lt;iomanip&gt;using namespace std;/*r 目的地O 箱子I 人X 墙路(空格)W 上A 左D 右S 下*/void menu();int level1();int level2();void swap();int opera();void dituprint();void menu(){cout&lt;&lt;&quot; 推箱子&quot;&lt;&lt;endl;cout&lt;&lt;&quot; 1.新游戏 2.选关&quot;&lt;&lt;endl;cout&lt;&lt;&quot; 0.退出&quot;&lt;&lt;endl;}void xuanguan(){int choice_level;while(1){cout&lt;&lt;&quot;请选择关卡:&quot;&lt;&lt;endl;cin&gt;&gt;choice_level;switch(choice_level){case 1:level1();break;case 2:level2();break;default:cout&lt;&lt;&quot;输入有误请重新选择关卡!&quot;;}}}void swap(char *a,char *b){system(&quot;cls&quot;);char ch;ch=*a;*a=*b;*b=ch;}void dituprint(char *p){//system(&quot;cls&quot;);for(int i=0;i&lt;9;i++){for(int j=0;j&lt;9;j++)cout&lt;&lt;setw(2)&lt;&lt;p[i*9+j];cout&lt;&lt;endl;}}int opera(char *p,int *ren,int xzs) //xzs 箱子数ren 存有人与箱子目标的位置{dituprint(p);while(1){while(1){for(int i=1,panduan=0;i&lt;=xzs;i++){if(p[ren[i]]==&#39;O&#39;){panduan+=1;cout&lt;&lt;&quot;panduan=&quot;&lt;&lt;panduan&lt;&lt;endl;} //判断当前有几个已经推到目的地了if(panduan==xzs){cout&lt;&lt;&quot;恭喜你过关啦!&quot;;getchar();return 0;}}char ch;cout&lt;&lt;&quot;你的按键为:&quot;;cin&gt;&gt;ch;switch(ch){case &#39;w&#39;:{if(p[ren[0]]==&#39;r&#39;) //当前人所在的位置为箱子最终要到的目的地的话{if(p[ren[0]-9]==&#39;r&#39;) //要移到的位置是箱子最终目的地{ren[0]-=9;p[ren[0]]=&#39;I&#39;;p[ren[0]+9]=&#39;r&#39;;break;}if(p[ren[0]-9]==&#39;X&#39;) //碰到墙了{cout&lt;&lt;&quot;人碰到墙了换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;; break;}if(p[ren[0]-9]==&#39; &#39;) //可以通过{ren[0]-=9;p[ren[0]]=&#39;I&#39;;p[ren[0]-9]=&#39;r&#39;;break;}if(p[ren[0]-9]==&#39;O&#39;) //碰到箱子{if(p[ren[0]-18]==&#39;X&#39;||&#39;O&#39;)cout&lt;&lt;&quot;箱子碰到墙了换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;;break;if(p[ren[0]-18]==&#39;r&#39;)p[ren[0]-18]=&#39;O&#39;;p[ren[0]-9]=&#39;I&#39;;p[ren[0]]=&#39;r&#39;;ren[0]-=9;break;}if(p[ren[0]-18]==&#39;O&#39;)cout&lt;&lt;&quot;推不动哎换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;;break; if(p[ren[0]-18]==&#39; &#39;){swap(p[ren[0]-9],p[ren[0]-18]);swap(p[ren[0]],p[ren[0]-9]);p[ren[0]]=&#39;r&#39;;ren[0]-=9;break;}}}else{if(p[ren[0]-9]==&#39;X&#39;){cout&lt;&lt;&quot;人碰到墙了换其他按键哇2&gt;&gt;&gt;&gt;&gt;&quot;;break;}if(p[ren[0]-9]==&#39; &#39;){swap(p[ren[0]],p[ren[0]-9]);ren[0]-=9;break;}if(p[ren[0]-9]==&#39;O&#39;){//遇到箱子代码if(p[ren[0]-18]==&#39;O&#39;||p[ren[0]-18]==&#39;X&#39;){cout&lt;&lt;&quot;走不动了换按其他键哇.......&quot;&lt;&lt;endl;break;if(p[ren[0]-18]==&#39;r&#39;){p[ren[0]-18]=&#39;O&#39;;p[ren[0]-9]=&#39;I&#39;;p[ren[0]]=&#39; &#39;;ren[0]-=9;break;}if(p[ren[0]-18]==&#39; &#39;){p[ren[0]-18]=&#39;O&#39;;p[ren[0]-9]=&#39;I&#39;;p[ren[0]]=&#39; &#39;;ren[0]-=9;break;}}}}break;case &#39;a&#39;:{if(p[ren[0]]==&#39;r&#39;) //当前人所在的位置为箱子最终要到的目的地的话{if(p[ren[0]-1]==&#39;r&#39;) //要移到的位置是箱子最终目的地{ren[0]-=1;p[ren[0]]=&#39;I&#39;;p[ren[0]+1]=&#39;r&#39;;break;}if(p[ren[0]-1]==&#39;X&#39;) //碰到墙了{cout&lt;&lt;&quot;人碰到墙了换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;; break;}if(p[ren[0]-1]==&#39; &#39;) //可以通过{p[ren[0]]=&#39;I&#39;;p[ren[0]+9]=&#39;r&#39;;break;}if(p[ren[0]-1]==&#39;O&#39;) //碰到箱子{if(p[ren[0]-2]==&#39;X&#39;||&#39;O&#39;)cout&lt;&lt;&quot;箱子碰到墙了换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;;break;if(p[ren[0]-2]==&#39;r&#39;){p[ren[0]-2]=&#39;O&#39;;p[ren[0]-1]=&#39;I&#39;;p[ren[0]]=&#39;r&#39;;ren[0]-=1;break;}if(p[ren[0]-2]==&#39;O&#39;)cout&lt;&lt;&quot;推不动哎换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;;break; if(p[ren[0]-2]==&#39; &#39;){swap(p[ren[0]-1],p[ren[0]-2]);swap(p[ren[0]],p[ren[0]-1]);p[ren[0]]=&#39;r&#39;;ren[0]-=1;break;}}}else{if(p[ren[0]-1]==&#39;X&#39;){cout&lt;&lt;&quot;人碰到墙了换其他按键哇2&gt;&gt;&gt;&gt;&gt;&quot;; break;}if(p[ren[0]-1]==&#39; &#39;){swap(p[ren[0]],p[ren[0]-1]);break;}if(p[ren[0]-1]==&#39;O&#39;){//遇到箱子代码if(p[ren[0]-2]==&#39;O&#39;||p[ren[0]-2]==&#39;X&#39;){cout&lt;&lt;&quot;走不动了换按其他键哇.......&quot;&lt;&lt;endl;break;}if(p[ren[0]-2]==&#39;r&#39;){p[ren[0]-2]=&#39;O&#39;;p[ren[0]-1]=&#39;I&#39;;p[ren[0]]=&#39; &#39;;ren[0]-=1;break;}if(p[ren[0]-2]==&#39; &#39;){p[ren[0]-2]=&#39;O&#39;;p[ren[0]-1]=&#39;I&#39;;p[ren[0]]=&#39; &#39;;ren[0]-=1;break;}}}}break;case &#39;s&#39;:{if(p[ren[0]]==&#39;r&#39;) //当前人所在的位置为箱子最终要到的目的地的话{if(p[ren[0]+9]==&#39;r&#39;) //要移到的位置是箱子最终目的地{ren[0]+=9;p[ren[0]]=&#39;I&#39;;p[ren[0]-9]=&#39;r&#39;;break;}if(p[ren[0]+9]==&#39;X&#39;) //碰到墙了{cout&lt;&lt;&quot;人碰到墙了换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;; break;}if(p[ren[0]+9]==&#39; &#39;) //可以通过{ren[0]+=9;p[ren[0]]=&#39;I&#39;;p[ren[0]+9]=&#39;r&#39;;break;}if(p[ren[0]+9]==&#39;O&#39;) //碰到箱子{if(p[ren[0]+18]==&#39;X&#39;||&#39;O&#39;)cout&lt;&lt;&quot;箱子碰到墙了换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;;break;if(p[ren[0]+18]==&#39;r&#39;){p[ren[0]+18]=&#39;O&#39;;p[ren[0]+9]=&#39;I&#39;;p[ren[0]]=&#39;r&#39;;ren[0]+=9;break;}if(p[ren[0]+18]==&#39;O&#39;)cout&lt;&lt;&quot;推不动哎换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;;break; if(p[ren[0]+18]==&#39; &#39;){swap(p[ren[0]+9],p[ren[0]+18]);swap(p[ren[0]],p[ren[0]-9]);p[ren[0]]=&#39;r&#39;;ren[0]+=9;break;}}else{if(p[ren[0]+9]==&#39;X&#39;){cout&lt;&lt;&quot;人碰到墙了换其他按键哇2&gt;&gt;&gt;&gt;&gt;&quot;; break;}if(p[ren[0]+9]==&#39; &#39;){swap(p[ren[0]],p[ren[0]+9]);ren[0]+=9;break;}if(p[ren[0]+9]==&#39;O&#39;){//遇到箱子代码if(p[ren[0]+18]==&#39;O&#39;||p[ren[0]+18]==&#39;X&#39;){cout&lt;&lt;&quot;走不动了换按其他键哇.......&quot;&lt;&lt;endl; break;}if(p[ren[0]+18]==&#39;r&#39;){p[ren[0]+18]=&#39;O&#39;;p[ren[0]+9]=&#39;I&#39;;p[ren[0]]=&#39; &#39;;ren[0]+=9;break;}if(p[ren[0]+18]==&#39; &#39;){p[ren[0]+18]=&#39;O&#39;;p[ren[0]+9]=&#39;I&#39;;p[ren[0]]=&#39; &#39;;ren[0]+=9;break;}}}}break;case &#39;d&#39;:{if(p[ren[0]]==&#39;r&#39;) //当前人所在的位置为箱子最终要到的目的地的话{if(p[ren[0]+1]==&#39;r&#39;) //要移到的位置是箱子最终目的地{ren[0]+=1;p[ren[0]]=&#39;I&#39;;p[ren[0]-1]=&#39;r&#39;;break;}if(p[ren[0]+1]==&#39;X&#39;) //碰到墙了{cout&lt;&lt;&quot;人碰到墙了换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;; break;}if(p[ren[0]+1]==&#39; &#39;) //可以通过{ren[0]+=1;p[ren[0]]=&#39;I&#39;;p[ren[0]+1]=&#39;r&#39;;break;}if(p[ren[0]+1]==&#39;O&#39;) //碰到箱子{if(p[ren[0]+2]==&#39;X&#39;||&#39;O&#39;)cout&lt;&lt;&quot;箱子碰到墙了换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;;break;if(p[ren[0]+2]==&#39;r&#39;){p[ren[0]+2]=&#39;O&#39;;p[ren[0]+1]=&#39;I&#39;;p[ren[0]]=&#39;r&#39;;ren[0]+=1;break;}if(p[ren[0]+2]==&#39;O&#39;)cout&lt;&lt;&quot;推不动哎换其他按键1哇&gt;&gt;&gt;&gt;&gt;&quot;;break; if(p[ren[0]+2]==&#39; &#39;){swap(p[ren[0]+1],p[ren[0]+2]);swap(p[ren[0]],p[ren[0]-1]);p[ren[0]]=&#39;r&#39;;ren[0]+=1;break;}}}else{if(p[ren[0]+1]==&#39;X&#39;){cout&lt;&lt;&quot;人碰到墙了换其他按键哇2&gt;&gt;&gt;&gt;&gt;&quot;; break;}if(p[ren[0]+1]==&#39; &#39;){swap(p[ren[0]],p[ren[0]+1]);ren[0]+=1;break;}if(p[ren[0]+1]==&#39;O&#39;){//遇到箱子代码if(p[ren[0]+2]==&#39;O&#39;||p[ren[0]+2]==&#39;X&#39;){cout&lt;&lt;&quot;走不动了换按其他键哇.......&quot;&lt;&lt;endl; break;}if(p[ren[0]+2]==&#39;r&#39;){p[ren[0]+2]=&#39;O&#39;;p[ren[0]+1]=&#39;I&#39;;p[ren[0]]=&#39; &#39;;ren[0]+=1;break;}if(p[ren[0]+2]==&#39; &#39;){p[ren[0]+2]=&#39;O&#39;;p[ren[0]+1]=&#39;I&#39;;p[ren[0]]=&#39; &#39;;ren[0]+=1;break;}}}}break;default:cout&lt;&lt;&quot;操作错误!请确认后再重新操作.......&quot;&lt;&lt;endl; }dituprint(p);}/*int panduan=0;for(int i=1;i&lt;xzs;i++){if(p[ren[i]]==&#39;O&#39;)panduan++;}if(panduan=(xzs-1)){getchar();break;}*/}return 0;}int level1(){cout&lt;&lt;&quot;第一关&quot;&lt;&lt;endl;char ditu1[9*9]={&#39;0&#39;,&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;, &#39;6&#39;,&#39;7&#39;,&#39;8&#39;,&#39;1&#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39; &#39;,&#39; &#39;,&#39; &#39;,&#39;2&#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39;r&#39;,&#39;X&#39;,&#39; &#39;,&#39; &#39;,&#39; &#39;,&#39;3&#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39; &#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;4&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;O&#39;,&#39;&#39;,&#39;O&#39;,&#39;r&#39;,&#39;X&#39;,&#39;5&#39;,&#39;X&#39;,&#39;r&#39;,&#39;&#39;,&#39;O&#39;,&#39;I&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;6&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;O&#39;,&#39;X&#39;,&#39; &#39;,&#39; &#39;,&#39;7&#39;,&#39; &#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39;r&#39;,&#39;X&#39;,&#39; &#39;,&#39; &#39;,&#39;8&#39;,&#39; &#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39; &#39;,&#39; &#39;};int ren1[5]={50,22,47,43,68};opera(ditu1,ren1,4);level2();return 0;}int level2(){cout&lt;&lt;&quot; 欢迎来到第二关&quot;&lt;&lt;endl;char ditu1[9*9]={&#39;0&#39;,&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;, &#39;6&#39;,&#39;7&#39;,&#39;8&#39;,&#39;1&#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39; &#39;,&#39; &#39;,&#39; &#39;,&#39;2&#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39;r&#39;,&#39;X&#39;,&#39; &#39;,&#39; &#39;,&#39; &#39;,&#39;3&#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39; &#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;4&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;O&#39;,&#39;&#39;,&#39;O&#39;,&#39;r&#39;,&#39;X&#39;,&#39;5&#39;,&#39;X&#39;,&#39;r&#39;,&#39;&#39;,&#39;O&#39;,&#39;I&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;6&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39;O&#39;, &#39;X&#39;,&#39; &#39;,&#39; &#39;,&#39;7&#39;,&#39; &#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39;r&#39;,&#39;X&#39;,&#39; &#39;,&#39; &#39;,&#39;8&#39;,&#39; &#39;,&#39; &#39;,&#39; &#39;,&#39;X&#39;,&#39;X&#39;,&#39;X&#39;,&#39; &#39;,&#39; &#39;};int ren1[5]={50,22,47,43,68};opera(ditu1,ren1,4);level1();return 0;}int main(){int choice;menu();while(1){cin&gt;&gt;choice;switch(choice){case 0:exit(0);case 1:{level1();break;}case 2:{xuanguan();break;}default:{cout&lt;&lt;&quot;操作错误!请确认正确后操作.......&quot;&lt;&lt;endl; }}}getchar();return 0;}。

推箱子游戏的代码)

推箱子游戏的代码)

#include <dos.h>#include <stdio.h>#include <ctype.h>#include <conio.h>#include <bios.h>#include <alloc.h>typedef struct winer{int x,y;struct winer *p;}winer;char status [20][20];char far *printScreen=(char far* )0xB8000000;void putoutChar(int y,int x,char ch,char fc,char bc);void printWall(int x, int y);void printBox(int x, int y);void printBoxDes(int x, int y);void printDestination(int x, int y);void printDestination1(int x,int y,winer **win,winer **pw); void printMan(int x, int y);void init();winer *initStep1();winer *initStep2();winer *initStep3();winer *initStep4();void moveBoxSpacetoSpace(int x ,int y, char a);void moveBoxDestoSpace(int x ,int y, char a) ;void moveBoxSpacetoDes(int x, int y, char a);void moveBoxDestoDes(int x, int y, char a);int judge(int x, int y);void move(int x, int y, char a);void reset(int i);void putoutChar(int y,int x,char ch,char fc,char bc){printScreen[(x*160)+(y<<1)+0]=ch;printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc;}void printWall(int x,int y){putoutChar(y-1,x-1,219,GREEN,BLACK);status[x][y]='w';}void printBox(int x,int y){putoutChar(y-1,x-1,10,WHITE,BLACK);status[x][y]='b';}void printDestination1(int x,int y,winer **win,winer **pw) {winer *qw;putoutChar(y-1,x-1,003,YELLOW,BLACK);status[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 printDestination(int x,int y){putoutChar(y-1,x-1,003,YELLOW,BLACK);status[x][y]='m';}void printMan(int x,int y){gotoxy(y,x);_AL=02;_CX=01;_AH=0xa;geninterrupt(0x10);}void printBoxDes(int x,int y){putoutChar(y-1,x-1,10,YELLOW,BLACK);status[x][y]='i';}void init(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)status[i][j]=0;_AL=3;_AH=0;geninterrupt(0x10);gotoxy(40,4);printf("Welcome to the box world!");gotoxy(40,6);printf("You can use up, down, left,");gotoxy(40,8);printf("right key to control it, or");gotoxy(40,10);printf("you can press Esc to quit it."); gotoxy(40,12);printf("Press space to reset the game."); gotoxy(40,14);printf("Wish you have a good time !");gotoxy(40,16);printf("April , 2007");}winer *initStep1(){int x;int y;winer *win=NULL;winer *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);printDestination1(4+4,2+10,&win,&pw); printDestination1(5+4,2+10,&win,&pw); printDestination1(6+4,2+10,&win,&pw); printMan(2+4,8+10);return win;}winer *initStep2(){int x;int y;winer *win=NULL;winer *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++)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);printDestination1(5+4,7+10,&win,&pw); printDestination1(6+4,7+10,&win,&pw); printDestination1(7+4,7+10,&win,&pw); printMan(2+4,6+10);return win;}winer *initStep3(){int x;int y;winer *win=NULL;winer *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(5+4,6+10);printDestination1(2+4,5+10,&win,&pw); printDestination1(2+4,6+10,&win,&pw); printDestination1(2+4,7+10,&win,&pw); printMan(2+4,4+10);return win;}winer *initStep4(){int x;int y;winer *win=NULL;winer *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);printDestination1(3+4,7+10,&win,&pw); printDestination1(4+4,7+10,&win,&pw); printDestination1(5+4,7+10,&win,&pw); printDestination1(4+4,8+10,&win,&pw); printDestination1(5+4,8+10,&win,&pw); printMan(2+4,2+10);return win;}void moveBoxSpacetoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]=0;printf(" ");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]=0;printf(" ");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]=0;printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxDestoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]='m';printf(" ");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]='m';printf(" ");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]='m';printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxSpacetoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBoxDes(x-2,y);status[x-2][y]='i';break;case 'd':status[x+1][y]=0;printf(" ");printBoxDes(x+2,y);printMan(x+1,y);status[x+2][y]='i';break;case 'l':status[x][y-1]=0;printf(" ");printBoxDes(x,y-2);printMan(x,y-1);status[x][y-2]='i';break;case 'r':status[x][y+1]=0;printf(" ");printBoxDes(x,y+2);printMan(x,y+1);status[x][y+2]='i';break;default:break;}}void moveBoxDestoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBoxDes(x-2,y);printMan(x-1,y);status[x-2][y]='i';break;case 'd':status[x+1][y]='m';printf(" ");printMan(x+1,y);status[x+2][y]='i'; break;case 'l':status[x][y-1]='m'; printf(" ");printBoxDes(x,y-2); printMan(x,y-1);status[x][y-2]='i'; break;case 'r':status[x][y+1]='m'; printf(" ");printBoxDes(x,y+2); printMan(x,y+1);status[x][y+2]='i'; break;default:break;}}int judge(int x,int y) {int i;switch(status[x][y]) {case 0:i=1;break;case 'w':i=0;break;case 'b':i=2;break;case 'i':i=4;break;case 'm':i=3;break;default:break;}return i;}void move(int x,int y,char a){switch(a){case 'u':if(!judge(x-1,y)){gotoxy(y,x);break;}else if(judge(x-1,y)==1||judge(x-1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x-1,y);break;}else{printf(" ");printMan(x-1,y);break;}}else if(judge(x-1,y)==2){if(judge(x-2,y)==1){moveBoxSpacetoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxSpacetoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}else if(judge(x-1,y)==4){if(judge(x-2,y)==1){moveBoxDestoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxDestoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}case 'd':if(!judge(x+1,y)){gotoxy(y,x);break;}else if(judge(x+1,y)==1||judge(x+1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x+1,y);break;}else{printf(" ");printMan(x+1,y);break;}}else if(judge(x+1,y)==2){if(judge(x+2,y)==1){moveBoxSpacetoSpace(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxSpacetoDes(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}else if(judge(x+1,y)==4){if(judge(x+2,y)==1){moveBoxDestoSpace(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxDestoDes(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}case 'l':if(!judge(x,y-1)){gotoxy(y,x);break;}else if(judge(x,y-1)==1||judge(x,y-1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y-1);break;}else{printf(" ");printMan(x,y-1);break;}}else if(judge(x,y-1)==2){if(judge(x,y-2)==1){moveBoxSpacetoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxSpacetoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}else if(judge(x,y-1)==4){{moveBoxDestoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxDestoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}case 'r':if(!judge(x,y+1)){gotoxy(y,x);break;}else if(judge(x,y+1)==1||judge(x,y+1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y+1);break;}else{printf(" ");printMan(x,y+1);break;}}else if(judge(x,y+1)==2){if(judge(x,y+2)==1){moveBoxSpacetoSpace(x,y,'r');printDestination(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxSpacetoDes(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}elsegotoxy(y,x);break;}else if(judge(x,y+1)==4){if(judge(x,y+2)==1){moveBoxDestoSpace(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxDestoDes(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}elsegotoxy(y,x);break;}default:break;}}void reset(int i){switch(i){case 0:init();initStep1();break;case 1:init();initStep2();break;case 2:init();initStep3();break;case 3:init();initStep4();break;default:break;}}void main(){int key;int x;int y;int s;int i=0;winer *win;winer *pw;_AL=3;_AH=0;geninterrupt(0x10); init();win=initStep1();do{_AH=3;geninterrupt(0x10); x=_DH+1;y=_DL+1;while(bioskey(1)==0);key=bioskey(0);switch(key){case 0x4800:move(x,y,'u');break;case 0x5000:move(x,y,'d');break;case 0x4b00:move(x,y,'l');break;case 0x4d00:move(x,y,'r');break;case 0x3920:reset(i);break;default:break;}s=0;pw=win;while(pw){if(status[pw->x][pw->y]=='m')s++;pw=pw->p;}if(s==0){free(win);gotoxy(25,2);printf("congratulate! You have done this step!"); getch();i++;switch(i){case 1:init();win=initStep2();break;case 2:init();win=initStep3();break;case 3:init();win=initStep4();break;case 4:gotoxy(15,21);printf("Congratulation! \n");gotoxy(15,23);printf("You have done all the steps, Welcome to play again!"); key=0x011b;getch();break;default:break;}}}while(key!=0x011b);_AL=3;_AH=0;geninterrupt(0x10);}。

推箱子的c语言代码

推箱子的c语言代码

推箱子的c语言代码推箱子(Sokoban)是一种经典的益智游戏,也是计算机程序设计中常用的案例之一。

在这个游戏中,玩家需要移动箱子,将它们推到指定位置上,以完成关卡的目标。

在C语言中,我们可以使用二维数组来表示游戏的地图,其中不同的字符代表不同的元素。

例如,可以使用'#'表示墙壁,'@'表示玩家,'$'表示箱子,'.'表示目标位置。

玩家可以通过控制输入来移动,将箱子推到目标位置上。

为了实现这个游戏,我们可以先定义一个二维数组来表示地图,然后通过循环读取用户的输入,根据输入来移动玩家和箱子,最后判断是否完成了关卡的目标。

我们需要定义一个二维数组来表示地图。

假设地图的大小是10x10,我们可以这样定义:```c#define MAP_SIZE 10char map[MAP_SIZE][MAP_SIZE] = {"##########","# #","# $ #","# @ #","# #","# #","# #","# #","# #","##########"};```其中,'#'表示墙壁,'@'表示玩家,'$'表示箱子,'.'表示目标位置。

空格表示可通行的空地。

接下来,我们可以定义一个函数来打印地图,以便在每一次移动后显示最新的状态:```cvoid printMap() {for (int i = 0; i < MAP_SIZE; i++) {printf("%s\n", map[i]);}}```然后,我们可以定义一个循环,接收用户的输入,并根据输入来移动玩家和箱子。

java推箱子游戏源代码(含推箱子的判断)

java推箱子游戏源代码(含推箱子的判断)

第一个Java 文件:package ziaoA; import import import import j ava•awt•Color;java•awt•ReadiessException; j ava•awt•event•KeyEvent; j ava•awt•event•KeyListener ;import import importimport import j avaz•swing•Imageicon; j avax•swing•JFrame; j avax•swing•JLabel; j avax•swing•JOptionPane; j avax•swing•JPanel; public JPanel〃匸人 JLabel class GameFrame extends JFrame { zhuobu = new JPanel(); worker = null;//箱r JLabelbox = null; //目的地 JLabel goal = null; JLabel[] walls = null; public static final int SPEED = 12; //设s 图片人小 int imgSize = 48; public void setImgSize (int imgSize){ this •imgSize = imgSize; public GameFrame(String title) throws HeadlessException { super {title); //构造方法中调用本类的其它方法 this •initContentPane(); this •addKeyListener (new KeyListener() { //键盘按下爭件 public void keyPressed(KeyEvent e) { //E2.5]使匸人可以移动 int 次Speed = 0, ySpeed = 0; switch (e•getKeyCode()){case KeyEvent• VK LEFT :xSpeed = -SPEED;worker . seticon (new Imageicon {'* image / workerUp • gif'*)); break;case KeyEvent• VK」工GHT :xSpeed = SPEED;worker.seticon(new Imageicon("image/workerUp•gif")); break;case KeyEvent•VK_UP :ySpeed = -SPEED;worker . seticon (new Imageicon (image/workerUp . gif ")); break;case KeyEvent•VK_DOWN :ySpeed = SPEED;worker . seticon (new Imageicon {'* image/workerUp • gif'*)); break;default:return;worker • setBounds (x-zorker • getX () + KSpeed, worker • getY () + ySpeed, worker•getWidth(), worker•getHeight());//[2.7]判断匸人是否撞到墙壁for (int i = 0; i < walls.length; i++) {if (worker•getBounds{).intersects(walls[i].getBounds())) { worker • setBounds (x-zorker • getX () - :龙Speed, worker • getY () - ySpeed, worker•getWidth(), worker•getHeight());//E3.2]使I:人可以推动箱rif (worker•getBounds()•intersects(box•getBounds())){box•setBounds(box•getX() + xSpeed, box•getY () + ySpeed, box•getWidth(), box•getHeight());//[3.3]判断箱r是否撞到墙壁for (int i = 0; i < walls.length; i++) {if (box•getBounds()•intersects(walls[i].getBounds())) {worker•setBounds(worker•getX () - xSpeed, worker•getY () ySpeed, worker•getWidth(), worker•getHeight());box•setBounds(box•getX() - xSpeed, box•getY() - ySpeed, box•getWidth(), box•getHeight());//[3.4]判断是否胜利if (box•getX{)==goal•getX() && box•getY()==goal•getY()){JOptionPane > showMessageDialog(null, ”您贏啦!;public void keyReleased(KeyEvent e) {public void keyTyped(KeyEvent e) { }});*设置内容面板public void initContentPane() {zhuobu •setBackground(Color•red); zhuobu •setLayout (null);//调用父类的厲性和方法super •setcontentPane(zhuobu);public void addComponent (int tag. String imgPath, int x, int y) { Imageicon img = new Imageicon{imgPath); //创建JLabel^把工mage 工oon 通过构造方法传参传入〃把食物放到盘J"里JLabel componet = new JLabel(img);//设置盘f 在桌布上的位置和人小componet.setBounds(z, y, imgSize, imgSize);//把盘r 放到桌布上zhuobu •add(componet);switch (tag) {case 1:box = componet; break;case 2:*把某个图片以组件的力式加入窗体图片路径* @parain* Oparam* @param imgPath * Oparam 、* @param : * fireturn y width height y 宽度 拓度添加完的组件int index = 0;分别设置各个图片位置r(int i = 0; i < 14; i++) {//左边墙walls [index].setBounds(0, izhuobu •add (walls [index]); index++; //右边墙walls [index].setBounds(20 *for * imgSize , imgSize , imgSize );imgSize , i * imgSize , imgSize ,imgSize);zhuobu .add (walls [index]); index+-s-; for (int i = 0; i < 19; i++) {//上边墙walls [index].setBounds((i +1) * imgSize , 0, imgSize ,imgSize);zhuobu .add (walls [index]);index-5-+; //下边墙walls [index]•setBounds((i + 1) imgSize);zhuobu •add (walls [index]);inde:<++; * imgSize, 13 * imgSize, imgSize,goal = componet; break;case 3:worker = componet; break;for (int i = 0; i < walls .length; i++) {//创建没每■个闱墙,他们使用的是同 个图片walls [i] = new JLabel(walllmg);for (int i = 0; i < walls .length; i++) {//创建没每■个鬧墙,他们使用的是同■个图片walls [i] = new JLabel(walllmg);//添加中间障碍耦介解耦for (int i = 0; i < loactions.length; i++) {public void addWall(String Imageicon walllmg = new walls = new JLabel[66 +imgPath, int [][] loactions) {Imageicon(imgPath); loactions•length];walls[index]•setBounds(loactions[i][0]* imgSize, loactions[i][1]* imgSize, imgSize, imgSize);zhuobu.add(walls[index]);inde:<++;第二个Java文件:pubUc class Run {pubUc static void niain(String[] args) {GameFrame gameFramc = new GameFrame("推箱子游戏…”); 〃设置大小gameFrame.seiBoundsdOO. 50. 21 *48 + 5, 14*48 + 25); 〃窗体大小不可变ganieFrame.selRcsizable(false); ganieFrame5ClInigSize(48);ganieFrame.addComponenU3. '*workerUp.png"\ 4{M). 100):ganieFrame.addComponentf U "box,png*\ 160,60): ganieFrame.addComponenU2,"goal.png". 80.520); int[][] wallLocalions ={{4.5}.{5.5b(6.5|,{7.5},{8-5}.{9.51,(10,5}.{6-8}.{7-8}.{&8}・{9.8}.{10,8b{11.5}ganieFrame.addWall("wall.png"\ wallLocations);ganieFrame.setVisibleCtrue);。

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("用↑.↓.←.→控制人物的移动。

推箱子游戏代码

推箱子游戏代码

import java.awt.Color;import java.awt.HeadlessException;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;@SuppressWarnings("serial")public class GameFrame extends JFrame {private JPanel panel; //面板private JLabel[] box; //箱子private JLabel[] goal; //目标private JLabel worker; //工人private JLabel component; //元件private JLabel[] wall; //围墙private int levalNum = 1; //关卡数private int sum = 0; //箱子与目标重合的个数private int[][] direction;private String[] image;private int dirIndex;private int flag;private int flag1;//窗体初始化public GameFrame(String title) throws HeadlessException {super(title); //调用父类的构造方法super.setResizable(false); //设置窗体伸缩性super.setDefaultCloseOperation(EXIT_ON_CLOSE); //设置关闭窗口时结束程序initPanel(); //初始化面板}//加载控件public void loadLeval(Leval leval) {sum = 0;direction = new int[10][2];image = new String[10];// dirIndex = 0;flag = 1;panel.removeAll(); //清除面板上的元件super.setTitle("推箱子游戏--" + "第" + levalNum + "关"); //设置窗体的标题super.setBounds(400, 200, leval.getLength() * leval.getImgSize() + 6, leval.getHeight() * leval.getImgSize() + 25); //设置窗体的的大小和位置box = new JLabel[leval.getBoxNum()]; //设置和创建boxfor(int i = 0; i < box.length; i++) {int[] location = leval.getBoxLocation()[i];box[i] = initComponent(leval.getImgBox(), location[0], location[1], leval.getImgSize());}int[] location = leval.getWorkerLocaton(); //设置和创建workerworker = initComponent(leval.getImgWorker()[3], location[0], location[1], leval.getImgSize());initWalls(leval.getImgWalls(), leval.getWallsLocation(), leval.getImgSize()); //初始化围墙goal = new JLabel[leval.getBoxNum()]; //设置和创建goalfor(int i = 0; i < goal.length; i++) {int[] location1 = leval.getGoalLocation()[i];goal[i] = initComponent(leval.getImgGoal(), location1[0], location1[1], leval.getImgSize());}panel.repaint(); // 重画面板}// 面板初始化方法private void initPanel() {panel = new JPanel();panel.setBackground(Color.GREEN);panel.setLayout(null);super.setContentPane(panel);}//元件初始化方法private JLabel initComponent(String componentPath, int left, int top, int imgSize) {ImageIcon imgComponent = new ImageIcon("image/" + componentPath);component = new JLabel(imgComponent);component.setBounds(left * imgSize, top * imgSize, imgSize, imgSize);panel.add(component);return component;}// 围墙初始化方法private void initWalls(String wallsPath, int[][] wallsLocation, int imgSize) { ImageIcon Imgwall = new ImageIcon("image/" + wallsPath);wall = new JLabel[wallsLocation.length];for (int i = 0; i < wallsLocation.length; i++) {wall[i] = new JLabel(Imgwall);}int index = 0;for (int i = 0; i < wallsLocation.length; i++) {int[] location = wallsLocation[i];wall[index].setBounds(location[0] * imgSize, location[1] * imgSize, imgSize, imgSize);panel.add(wall[index]);index++;}}// 键盘处理事件方法public void addEvent(final Leval leval) {super.addKeyListener(new KeyListener() {@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {int keyCode = e.getKeyCode();int step = 24;int x = 0, y = 0;flag1 = 1;String img = leval.getImgWorker()[3];if(keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {y -= step;img = leval.getImgWorker()[0];} else if(keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) {y += step;img = leval.getImgWorker()[1];} else if(keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {x -= step;img = leval.getImgWorker()[2];} else if(keyCode == KeyEvent.VK_RIGHT || keyCode ==KeyEvent.VK_D) {x += step;img = leval.getImgWorker()[3];} else if(keyCode == KeyEvent.VK_ESCAPE) {Leval leval = LevalManager.getLeval(levalNum);loadLeval(leval); // 按ESC键重新玩这一关}// else if(keyCode == KeyEvent.VK_F1) {// flag1 = 0;// worker.setIcon(new ImageIcon("image/" +image[dirIndex])); //设置工人图片// worker.setLocation(worker.getBounds().x - direction[dirIndex][0], worker.getBounds().y - direction[dirIndex][1]); //设置工人位置// }worker.setIcon(new ImageIcon("image/" +img)); //设置工人图片worker.setLocation(worker.getBounds().x + x, worker.getBounds().y + y); //设置工人位置if(flag1 == 1) {direction[dirIndex][0] = x;direction[dirIndex][1] = y;image[dirIndex] = img;dirIndex++;}//阻止工人穿墙for(int i = 0; i < wall.length; i++) {if(wall[i].getBounds().intersects(worker.getBounds())) {worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);flag = 0;break;}}// 阻止工人穿过箱子for(int j = 0; j < box.length; j++) {if(worker.getBounds().intersects(box[j].getBounds())) {box[j].setLocation(box[j].getBounds().x + x, box[j].getBounds().y + y);// 阻住箱子穿过箱子for(int k = 0; k < box.length; k++) {if(box[j].getBounds().intersects(box[k].getBounds())&& k != j) {box[j].setLocation(box[j].getBounds().x - x, box[j].getBounds().y - y);worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);flag = 0;break;}}}for(int i = 0; i < wall.length; i++) {if(wall[i].getBounds().intersects(box[j].getBounds())) {box[j].setLocation(box[j].getBounds().x - x, box[j].getBounds().y - y);worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);flag = 0;break;}}// 阻止箱子穿过围墙for(int i = 0; i < wall.length; i++) {if(wall[i].getBounds().intersects(box[j].getBounds())) {for(int k = 0; k < box.length; k++) {if(box[j].getBounds().intersects(box[k].getBounds()) && k != j) {box[j].setLocation(box[j].getBounds().x - x, box[j].getBounds().y - y);worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);flag = 0;break;}}break;}}}// 判断箱子与目标重合for(int i = 0; i < box.length; i++) {for(int j = 0; j < goal.length; j++) {if(goal[j].getBounds().contains(box[i].getBounds())) {sum++;}}}// 判断箱子与目标是否都已经重合if(sum == box.length) {levalNum++;Leval leval = LevalManager.getLeval(levalNum);if(leval == null) {JOptionPane.showMessageDialog(null, "恭喜您闯关成功!");System.exit(0);} else {JOptionPane.showMessageDialog(null, "恭喜您成功闯过第" + (levalNum - 1) + "关!");loadLeval(leval);}}sum = 0;if(dirIndex == 10) {dirIndex = 0;}if(flag == 0) {dirIndex--;flag = 1;}}});}}public class Leval {private int imgSize; //图片的大小private String imgBox; //box图片private String imgGoal; //goal图片private String[] imgWorker; //工人图片private String imgWalls; //围墙图片private int[][] boxLocation; //box的位置private int[][] goalLocation; //goal的位置private int[] workerLocaton; //工人的位置private int[][] wallsLocation; //围墙的位置private int boxNum; //box的个数private int wallNum; //围墙的个数private int length; //窗体的宽可容纳围墙的个数private int height; //窗体的高可容纳围墙的个数public Leval() {}public int getImgSize() {return imgSize;}public void setImgSize(int imgSize) {this.imgSize = imgSize;}public String getImgBox() {return imgBox;}public class LevalManager {public static Leval getLeval(int index) {Leval leval = new Leval();int[][] boxLocation;int[][] goalLocation;int[][] wallsLocation;int[] workerLocation;int wallIndex = 0, goalIndex = 0, boxIndex = 0;// 地图信息:0代表空,1代表围墙,2代表箱子,3代表目标,4,代表工人int[][][] map = new int[][][]{{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},{1, 1, 1, 4, 0, 1, 1, 1, 1, 1},{1, 1, 1, 0, 2, 0, 0, 1, 1, 1},{1, 1, 1, 1, 0, 1, 0, 1, 1, 1},{1, 1, 3, 1, 0, 1, 0, 0, 1, 1},{1, 1, 3, 2, 0, 0, 1, 0, 1, 1},{1, 1, 3, 0, 0, 0, 2, 0, 1, 1},{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}},{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},{1, 1, 0, 0, 0, 0, 0, 1, 1, 1},{1, 1, 2, 1, 1, 1, 0, 0, 0, 1},{1, 4, 0, 0, 2, 0, 0, 2, 0, 1},{1, 0, 3, 3, 1, 0, 2, 0, 1, 1},{1, 1, 3, 3, 1, 0, 0, 0, 1, 1},{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}},{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},{1, 1, 1, 1, 0, 0, 1, 0, 4, 1},{1, 1, 1, 0, 0, 0, 1, 0, 0, 1},{1, 1, 1, 2, 0, 2, 0, 2, 0, 1},{1, 1, 1, 0, 2, 1, 1, 0, 0, 1},{1, 1, 1, 0, 2, 0, 1, 0, 1, 1},{1, 3, 3, 3, 3, 3, 0, 0, 1, 1},{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}},{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},{1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1},{1, 1, 0, 0, 0, 2, 0, 0, 0, 1, 1},{1, 1, 2, 0, 1, 1, 1, 0, 2, 1, 1},{1, 1, 0, 1, 3, 3, 3, 1, 0, 1, 1},{1, 1, 0, 1, 3, 3, 3, 1, 0, 1, 1},{1, 0, 2, 0, 0, 2, 0, 0, 2, 0, 1},{1, 0, 0, 0, 0, 0, 1, 4, 0, 0, 1},{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}},{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},{1, 1, 3, 3, 3, 3, 0, 1, 1, 1},{1, 1, 1, 3, 3, 3, 2, 1, 1, 1},{1, 0, 0, 2, 1, 2, 0, 2, 0, 1},{1, 0, 2, 2, 0, 0, 1, 2, 0, 1},{1, 0, 0, 0, 0, 1, 0, 0, 0, 1},{1, 1, 1, 1, 4, 0, 0, 1, 1, 1},{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}};switch (index) {case 1:leval.setWallNum(57);leval.setBoxNum(3);leval.setImgSize(48);leval.setImgBox("box.png");leval.setImgGoal("goal.png");leval.setImgWorker(new String[] {"workerUp2.png", "workerDown2.png", "workerLeft2.png", "workerRight2.png"});leval.setImgWalls("wall2.PNG");break;case 2:leval.setWallNum(52);leval.setBoxNum(4);leval.setImgSize(48);leval.setImgBox("box2.PNG");leval.setImgGoal("goal2.png");leval.setImgWorker(new String[] {"workerUp2.png", "workerDown2.png", "workerLeft2.png", "workerRight2.png"});leval.setImgWalls("wall2.PNG");break;case 3:leval.setWallNum(52);leval.setBoxNum(5);leval.setImgSize(48);leval.setImgBox("box.png");leval.setImgGoal("goal.png");leval.setImgWorker(new String[] {"workerUp2.png", "workerDown2.png", "workerLeft2.png", "workerRight2.png"});leval.setImgWalls("wall2.PNG");break;case 4:leval.setWallNum(57);leval.setBoxNum(6);leval.setImgSize(48);leval.setImgBox("box2.PNG");leval.setImgGoal("goal.png");leval.setImgWorker(new String[] {"workerUp2.png", "workerDown2.png", "workerLeft2.png", "workerRight2.png"});leval.setImgWalls("wall2.PNG");break;case 5:leval.setWallNum(47);leval.setBoxNum(7);leval.setImgSize(48);leval.setImgBox("box.png");leval.setImgGoal("goal.png");leval.setImgWorker(new String[] {"workerUp2.png", "workerDown2.png", "workerLeft2.png", "workerRight2.png"});leval.setImgWalls("wall2.PNG");break;default:return null;}leval.setHeight(map[index - 1].length);leval.setLength(map[index - 1][0].length);wallsLocation = new int[leval.getWallNum()][2];boxLocation = new int[leval.getBoxNum()][2];goalLocation = new int[leval.getBoxNum()][2];workerLocation = new int[2];// 求出每关围墙、箱子、目标和工人的位置信息for(int i = 0; i < map[index - 1].length; i++) {for(int j = 0; j < map[index - 1][i].length; j++) {if(map[index - 1][i][j] == 1) {wallsLocation[wallIndex][0] = j;wallsLocation[wallIndex][1] = i;wallIndex++;} else if(map[index - 1][i][j] == 2) {boxLocation[boxIndex][0] = j;boxLocation[boxIndex][1] = i;boxIndex++;} else if(map[index - 1][i][j] == 3) {goalLocation[goalIndex][0] = j;goalLocation[goalIndex][1] = i;goalIndex++;} else if(map[index - 1][i][j] == 4) {workerLocation[0] = j;workerLocation[1] = i;}}}leval.setBoxLocation(boxLocation);leval.setGoalLocation(goalLocation);leval.setWorkerLocaton(workerLocation);leval.setWallsLocation(wallsLocation);return leval;}}public class Run {public static void main(String[] args) {Leval leval = LevalManager.getLeval(1); //传入第一关信息GameFrame gameFrame = new GameFrame("推箱子游戏--第一关"); //创建一个窗体gameFrame.loadLeval(leval); //加载控件gameFrame.addEvent(leval); //加载键盘处理事件gameFrame.setVisible(true); //设置窗体为可见}}。

Java面向对象推箱子源代码(可扩展)

Java面向对象推箱子源代码(可扩展)

Java面向对象实现推箱子的源代码目录一、首先: (1)二、以下为工程中各个类的源代码: (1)1、Box (1)2、GameMainTest (3)3、Man (4)4、Map (6)5、MovingException (7)6、Out (8)一、首先:在eclipse中新建一个工程,包名和类名(工程结构)如下:二、以下为工程中各个类的源代码:源代码按对应的类名复制粘贴进去即可。

1、Boxpackage tuixiangzi;import java.util.Random;public class Box {private static Random ran = new Random();private static int x = ran.nextInt(10); //箱子所在的位置(随机) private static int y = ran.nextInt(10); //箱子所在的位置(随机) private int [][]map = Map.getArray();private int h = map.length - 1;private int l = map[h].length - 1;/*** 箱子左移*/public void boxLMove()throws MovingException{if(y-1 < 0) {throw new MovingException("You Can't Moving Left!");}if(Man.getX() == x && Man.getY() == y) {y=(y-1);}/*** 箱子右移*/public void boxRMove()throws MovingException{if(y+1 > l) {throw new MovingException("You Can't Moving Right!");}if(Man.getX() == x && Man.getY() == y) {y=(y+1);}}/*** 箱子上移*/public void boxUMove()throws MovingException{if(x-1 < 0) {throw new MovingException("You Can't Moving Up!");}if(Man.getX() == x && Man.getY() == y) {x=(x-1);}}/*** 箱子下移*/public void boxDMove()throws MovingException{if(x+1 > h) {throw new MovingException("You Can't Moving Down!");}if(Man.getX() == x && Man.getY() == y) {x=(x+1);}}/*** 判断箱子是否能移动(死亡)或者是否通关* @return*/public String judgOver() {String msg = null;if(x == Out.getX() && y == Out.getY()) {msg = "You Win!";return msg;if(x == 0 && y == 0 || x == h && y == l || x == 0 && y == l || x == h && y == 0) { msg = "Game Over!";return msg;}else {msg = " ";return msg;}}public static int getX() {return x;}public static void setX(int x) {Box.x = x;}public static int getY() {return y;}public static void setY(int y) {Box.y = y;}}2、GameMainTestpackage tuixiangzi;import java.util.Scanner;public class GameMainTest {private static Scanner sc;public static void main(String[] args){Map map = new Map();Man man = new Man();Box box = new Box();sc = new Scanner(System.in);map.printMap();//游戏开始,打印地图System.out.println("推箱子游戏开始!");do {System.out.println("w:↑ s:↓ a:← d:→ 回车确认移动");String key = sc.next();switch (key){case"a": //左移try {man.leftMove(key);box.boxLMove();} catch (MovingException e) {System.out.println("走不下去啦!");}map.cleanManAfter(Man.getX(), Man.getY()+1);break;case"d": //右移try {man.rightMove(key);box.boxRMove();} catch (MovingException e) {System.out.println("走不下去啦!");}map.cleanManAfter(Man.getX(), Man.getY()-1);break;case"s": //下移try {man.downMove(key);box.boxDMove();} catch (MovingException e) {System.out.println("走不下去啦!");}map.cleanManAfter(Man.getX()-1, Man.getY());break;case"w": //上移try {man.upMove(key);box.boxUMove();} catch (MovingException e) {System.out.println("走不下去啦!");}map.cleanManAfter(Man.getX()+1, Man.getY());break;}System.out.println("\n\n\n\n");System.out.println(box.judgOver());map.printMap();}while(true);}}3、Manpackage tuixiangzi;public class Man {private static int x = 0; //人所在的行位置private static int y = 0; //人所在的列位置private int [][]map = Map.getArray();private int h = map.length-1;private int l = map[h].length-1;/*** 向左移动* @param key 移动按键* @throws MovingException 无法移动异常*/public void leftMove(String key)throws MovingException{if(y-1 < 0) {throw new MovingException("You Can't Moving Left!");}if(key.equals("a")) {y=(y-1);}}/*** 向右移动* @param key 移动按键* @throws MovingException 无法移动异常*/public void rightMove(String key)throws MovingException{ if(y+1 > l) {throw new MovingException("You Can't Moving Right!");}if(key.equals("d")) {y=(y+1);}}/*** 向上移动* @param key 移动按键* @throws MovingException 无法移动异常*/public void upMove(String key)throws MovingException{if(x-1 < 0) {throw new MovingException("You Can't Moving Up!");}if(key.equals("w")) {x=(x-1);}}/*** 向下移动* @param key 移动按键* @throws MovingException 无法移动异常*/public void downMove(String key)throws MovingException{ if(x+1 > h) {throw new MovingException("You Can't Moving Down!");}if(key.equals("s")) {x=(x+1);}}public static int getX() {return x;}public static void setX(int x) {Man.x = x;}public static int getY() {return y;}public static void setY(int y) {Man.y = y;}}4、Mappackage tuixiangzi;public class Map {private static int array[][] = new int[10][10];/*** 打印地图布局*/public void printMap() {array[Man.getX()][Man.getY()] = 1; //初始化人array[Box.getX()][Box.getY()] = 3; //初始化箱子array[Out.getX()][Out.getY()] = 2; //初始化出口for(int i = 0; i < array.length; i++) {for(int j = 0; j < array[i].length;j++) {if(array[i][j] == array[Man.getX()][Man.getY()]) {System.out.print("♀ ");} else if(array[i][j] == 0) {System.out.print("□ ");} else if(array[i][j] == array[Box.getX()][Box.getY()]) {System.out.print("■ ");} else if(array[i][j] == array[Out.getX()][Out.getY()]) {System.out.print("→ ");}}System.out.println();}}/*** 清除人和箱子移动后上一步的位置* @param x* @param y* @return*/public int cleanManAfter(int x,int y) {return array[x][y]=0;}public static int[][] getArray() {return array;}public static void setArray(int[][] array) {Map.array = array;}}5、MovingExceptionpackage tuixiangzi;public class MovingException extends Exception{private static final long serialVersionUID = 1L;public MovingException() {super();}public MovingException(String message) {super(message);}public MovingException(String message, Throwable cause) {super(message, cause);}public MovingException(Throwable cause) {super(cause);}}6、Outpackage tuixiangzi;public class Out {//后期可扩展为出口出现的位置随机private static int x = 9; //初始化出口的位置private static int y = 9; //初始化出口的位置public static int getX() {return x;}public static void setX(int x) {Out.x = x;}public static int getY() {return y;}public static void setY(int y) {Out.y = y;}}。

推箱子游戏代码,非界面版

推箱子游戏代码,非界面版

import java.util.Random;import java.util.Scanner;public class 推箱子 {public static void main(String[] args) {//推箱子int map[][]=new int[10][10]; //固定箱子,人,目的地的位置// int rx=0;// int ry=0;// map[rx][ry]=1;// int xx=5;// int xy=1;// map[xx][xy]=2;// int mx=6;// int my=4;// map[mx][my]=3;//随机生成人,箱子,目的地的位置Random r=new Random();int rx=r.nextInt(10);int ry=r.nextInt(10);map[rx][ry]=1;int xx=r.nextInt(10);int xy=r.nextInt(10);map[xx][xy]=2;int mx=r.nextInt(10);int my=r.nextInt(10);map[mx][my]=3;while(true){for(int i=0;i<map.length;i++){for(int j=0;j<map[i].length;j++){if(map[i][j]==1){System.out.print(" 人 ");}if(map[i][j]==2){//△代表箱子System.out.print(" △ ");}if(map[i][j]==3){//★代表箱子System.out.print(" ★ ");}if(map[i][j]==0){//◇代表箱子System.out.print(" ◇ ");}}System.out.println();}if(xx==mx&&xy==my){break;}if(xx==0&&mx!=0||xy==0&&my!=0||xx==9&&mx!=9||xy==9&&my!=9){// 判断箱子到边上而目的地不在边上的情况System.out.println("永远也到不了了~~~~(>_<)~~~~ ");}Scanner s = new Scanner(System.in);System.out.println("请输入要操作的动作:");System.out.println("w↑,s↓,a←,d→");String t=s.next();if(t.equals("d")){if(ry==9){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else if(t.equals("d")){ry++;if(map[rx][ry]==0){map[rx][ry]=1;map[rx][ry-1]=0;}else if(map[rx][ry]==2){xy++;map[xx][xy]=2;map[rx][ry]=1;map[rx][ry-1]=0;}}}if(t.equals("a")){if(ry==0){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else {ry--;if(map[rx][ry]==0){map[rx][ry]=1;map[rx][ry+1]=0;}else if(map[rx][ry]==2){xy--;map[xx][xy]=2;map[rx][ry+1]=0;map[rx][ry]=1;}}}if(t.equals("s")){if(rx==9){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else {rx++;if(map[rx][ry]==0){map[rx][ry]=1;map[rx-1][ry]=0;}else if(map[rx][ry]==2){xx++;map[xx][xy]=2;map[rx-1][ry]=0;map[rx][ry]=1;}}}if(t.equals("w")){if(rx==0){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else{rx--;if(map[rx][ry]==0){map[rx][ry]=1;map[rx+1][ry]=0;}else if(map[rx][ry]==2){xx--;map[xx][xy]=2;map[rx+1][ry]=0;map[rx][ry]=1;}}}}System.out.println("游戏结束!");}}。

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`。

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

推箱子源小游戏代码

推箱子源小游戏代码

<!DOCTYPE html ><html xmlns="#" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>推箱子</title><style type="text/css">*{margin:0px; padding:0px;}.bg{margin:0px;width:32px;height:32px;background-image: url(gameImg/01.jpg);float:left;} /*背景*/.box{margin:0px;width:32px;height:32px;background-image: url(gameImg/05.jpg);float:left;} /*箱子*/.box2{margin:0px;width:32px;height:32px;background-image: url(gameImg/06.jpg);float:left;} /*箱子2(放目标上)*/.wall{margin:0px;width:32px;height:32px;background-image: url(gameImg/03.jpg);float:left;} /*墙*/.goal{margin:0px;width:32px;height:32px;background-image: url(gameImg/02.jpg);float:left;} /*目标*/.role{margin:0px;width:32px;height:32px;background-image: url(gameImg/04.jpg);float:left;} /*角色*/</style><script type="text/javascript">var maxX = 10;var maxY = 10;var mapAry =[ //地图状态数组0:墙 1:路 2:目标[1,1,0,0,0,0,0,0,1,1],[0,0,0,1,1,2,1,0,0,0],[0,1,1,2,1,1,1,1,1,0],[0,1,0,1,0,2,1,0,1,0],[0,1,1,2,1,2,1,0,1,0],[0,0,0,1,1,1,1,1,1,0],[1,1,0,1,1,2,0,0,0,0],[1,1,0,1,1,1,0,1,1,1],[1,1,0,1,1,1,0,1,1,1],[1,1,0,0,0,0,0,1,1,1]];var imgAry = new Array(); //地图图片数组var role = new Point(4,2); //创建角色坐标var boxs = [new Point(2,2),new Point(2,4),new Point(2,5),new Point(2,6),new Point(6,4),new Point(5,5)]; //创建箱子坐标数组//定义坐标类function Point(x,y){this.x = x;this.y = y;}//创建单个方块function CreateDiv(obj,css){var d = document.createElement(obj); //创建一个DIV 元素d.className = css; //设置样式document.getElementById("main").appendChild(d);return d;}//初始化界面function Show(){//显示地图for (var i=0; i<maxX; i++) {imgAry[i] = new Array();for (var j=0; j<maxY; j++) {if(mapAry[i][j]==0){imgAry[i][j]= CreateDiv("div","wall");}else if(mapAry[i][j]==1){imgAry[i][j]= CreateDiv("div","bg");}else if(mapAry[i][j]==2){imgAry[i][j]= CreateDiv("div","goal");}}}//显示角色imgAry[role.x][role.y].className = "role";//显示箱子for (var i=0; i<boxs.length; i++) {imgAry[boxs[i].x][boxs[i].y].className = "box";}}//控制角色移动function RoleMove(k){var p = new Point(role.x,role.y); //保存角色移动前的坐标//改变角色坐标if(k==37){ //向左role.y--;}else if(k==38){ //向上role.x--;}else if(k==39){ //向右role.y++;}else if(k==40){ //向下role.x++;}//还原角色所在地图背景if(mapAry[role.x][role.y]==0){ //如果是墙,则不能移动role = p;}else{ //否则还原地图背景if(mapAry[p.x][p.y]==1){imgAry[p.x][p.y].className = "bg";}else if(mapAry[p.x][p.y]==2){imgAry[p.x][p.y].className = "goal";}//判断是否推到箱子for (var i=0; i<boxs.length; i++) {if(role.x==boxs[i].x &&role.y==boxs[i].y){//如果角色和箱子重叠,调用箱子移动方法,根据返回值来判断角色坐标是否还原if(BoxMove(role,k)==false){role = p;}break;}}}//显示角色imgAry[role.x][role.y].className = "role";}//箱子移动方法,p表示和角色重叠的箱子坐标,k表示键盘按钮function BoxMove(p,k){var bl = true; //箱子是否可以移动:true可以移动,false不能移动var tmpP; //保存箱子另一侧的坐标//获取箱子另一侧坐标if(k==37){ //向左tmpP = new Point(p.x,p.y-1);}else if(k==38){ //向上tmpP = new Point(p.x-1,p.y);}else if(k==39){ //向右tmpP = new Point(p.x,p.y+1);}else if(k==40){ //向下tmpP = new Point(p.x+1,p.y);}//判断该坐标是否是墙if(mapAry[tmpP.x][tmpP.y] == 0){bl = false;}//判断该坐标是否有箱子for (var i=0; i<boxs.length; i++) {if(tmpP.x==boxs[i].x && tmpP.y==boxs[i].y){bl = false;break;}}//如果箱子可以移动,则改变箱子坐标并在新坐标显示箱子if(bl == true){for (var i=0; i<boxs.length; i++) {if(boxs[i].x==p.x && boxs[i].y==p.y){boxs[i] = tmpP;break;}}imgAry[tmpP.x][tmpP.y].className = "box";//如果新坐标是目标,则显示成箱子2的样式if(mapAry[tmpP.x][tmpP.y] == 2){imgAry[tmpP.x][tmpP.y].className = "box2";}}return bl;}//按键控制移动function Move(){var k = event.keyCode;var count = 0; //与目标点重叠的箱子个数RoleMove(k);//判断游戏是否结束判断所有箱子的坐标点的状态,如果状态都是2则游戏结束for (var i=0; i<boxs.length; i++) {if(mapAry[boxs[i].x][boxs[i].y]==2){count++;}}if(count==6){alert("恭喜过关,游戏结束!");}}document.onkeydown = Move;</script></head><body onload="Show();"><div id="main" style="margin:50px auto; border:solid1px blue; width:320px; height:320px;"></div></body> </html>。

推箱子源代码

推箱子源代码

#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;}。

推箱子游戏编程实现

推箱子游戏编程实现

推箱子游戏编程实现推箱子游戏是一种经典的逻辑思维游戏,通过移动箱子将其推到指定位置,完成关卡的过程。

在编程实现推箱子游戏时,需要考虑游戏界面的搭建、玩家操作的实现以及游戏逻辑的设计。

本文将介绍如何使用Python编程语言实现推箱子游戏,并给出相应的代码示例。

一、游戏界面的搭建推箱子游戏的界面通常由一个二维的网格组成,每个格子可以表示不同的元素,如墙壁、箱子、目标点、玩家等。

为了实现游戏界面的搭建,可以使用Python的图形界面库Tkinter或Pygame。

下面是使用Tkinter库搭建推箱子游戏界面的示例代码:```pythonimport tkinter as tk# 创建游戏界面root = ()root.title("推箱子游戏")root.geometry("400x400")# 绘制游戏网格canvas = tk.Canvas(root, width=400, height=400, bg='white')canvas.pack()# 绘制墙壁、箱子、目标点、玩家等元素# ...# 游戏主循环root.mainloop()```在这段代码中,我们首先导入了Tkinter库,然后创建了一个窗口对象`root`,并设置了窗口的标题和大小。

接着,使用`Canvas`类创建了一个画布对象`canvas`,并将其放置在窗口中。

最后,在游戏主循环中,通过调用`root.mainloop()`方法来启动游戏界面的事件循环,使窗口一直显示。

二、玩家操作的实现在推箱子游戏中,玩家可以通过键盘输入来移动箱子或玩家角色。

为了实现玩家的操作,我们可以使用Tkinter库中的事件绑定功能对键盘事件进行监听,并在相应的事件处理函数中更新游戏界面。

下面是使用Tkinter库实现玩家操作的示例代码:```pythondef move_left(event):# 处理向左移动的逻辑# ...def move_right(event):# 处理向右移动的逻辑# ...def move_up(event):# 处理向上移动的逻辑# ...def move_down(event):# 处理向下移动的逻辑# ...# 绑定键盘事件root.bind('<Left>', move_left)root.bind('<Right>', move_right)root.bind('<Up>', move_up)root.bind('<Down>', move_down)```在这段代码中,我们定义了四个函数分别处理向左、向右、向上、向下移动的逻辑。

C语言推箱子程序

C语言推箱子程序
m[x+a][y+b]=8;
m[x-a][y-b]=0;}
else if(m[x][y]==3) //人在成功点
{
x+=a;
y+=b;
m[x][y]=3;
m[x+a][y+b]=8;
m[x-a][y-b]=4;
}
}
else if(m[x+a][y+b]==5&&m[x+2*a][y+2*b]==4) //人物前面是成功箱子箱子前面是成功点
printf("⊙");
else if(m[i][j]==4)
printf("□");
else if(m[i][j]==5)
{
count++; //每扫描到一个成功值计数加1
printf("★");}
}
printf("\n");
}
return count;
}
void key() //按键操作
{
char z;
{
x+=a;
y+=b;
m[x][y]=3;
m[x+a][y+b]=5;
m[x-a][y-b]=0;
}
}
void game()
{
m[2][1]=2; //初始化人物模型点
for(;;)
{
prnmap(); //地图打印
key(); //操作指令
move(); //移动判断函数
if(prnmap()==4) //通关条件
{x+=a;
y+=b;

推箱子游戏源代码

推箱子游戏源代码

#include<stdio.h>#include<windows.h>#include<conio.h>int i=0;//记录所走步数//人的结构体struct person{int x;int y;}per={3,3};//定义光标移动函数void gotoxy(int x,int y){COORD coord={x,y};SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); }//初始化图形界面int arr_set(char arr[9][9]){int i=0;int j=0;for(i=0;i<9;i++)for(j=0;j<9;j++)arr[i][j]='*';arr[1][1]='';arr[1][2]='';arr[1][3]='';arr[2][1]='';arr[2][2]='';arr[2][3]='';arr[3][1]='';arr[3][2]='';arr[3][3]='';arr[3][7]='';arr[4][3]='';arr[4][7]='';arr[5][3]='';arr[5][4]='';arr[5][5]='';arr[5][6]='';arr[5][7]='';arr[6][2]='';arr[6][3]='';arr[6][4]='';arr[6][6]='';arr[6][7]='';arr[7][2]='';arr[7][3]='';arr[7][4]='';arr[2][2]=2;arr[3][2]=2;arr[4][3]=2;arr[3][3]=12;arr[3][7]=4;arr[4][7]=4;arr[5][7]=4;return0;}//打印图形界面int prin(char arr[9][9]){int i=0;int j=0;for(i=0;i<9;i++){for(j=0;j<9;j++){if('*'==arr[i][j])printf("▓");if(2==arr[i][j])printf("□");if(12==arr[i][j])printf("♀");if(4==arr[i][j])printf("○");if(''==arr[i][j])printf("");}printf("\n");}//操作提示gotoxy(30,2);printf("WSAD分别操作上移、下移、左移、右移");gotoxy(30,4);printf("□代表箱子、♀代表人、○代表目的地");gotoxy(30,6);printf("把所有箱子推到目的地即完成任务");gotoxy(2*per.x,per.y);return0;}//向上移动int move_up(char arr[9][9]){//如果上面没有东西if(''==arr[per.y-1][per.x]||4==arr[per.y-1][per.x]){gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x,per.y-1);printf("♀");gotoxy(2*per.x,per.y-1);arr[per.y-1][per.x]=12;per.y--;i++;}//如果上面是箱子并且箱子上面是空格则箱子和人同时上移光标回到人处if(2==arr[per.y-1][per.x]&&(''==arr[per.y-2][per.x]||4==arr[per.y-2][per.x])) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x,per.y-1);printf("♀");arr[per.y-1][per.x]=12;gotoxy(2*per.x,per.y-2);printf("□");arr[per.y-2][per.x]=2;per.y--;gotoxy(2*per.x,per.y);i++;}if(arr[3][7]==''){gotoxy(14,3);printf("○");arr[3][7]=4;}if(arr[4][7]==''){gotoxy(14,4);printf("○");arr[4][7]=4;}if(arr[5][7]==''){gotoxy(14,5);printf("○");arr[5][7]=4;}return0;}//向下移动int move_down(char arr[9][9]){//如果下面没有东西if(''==arr[per.y+1][per.x]||4==arr[per.y+1][per.x]){gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x,per.y+1);printf("♀");gotoxy(2*per.x,per.y+1);arr[per.y+1][per.x]=12;per.y++;i++;}//如果下面是箱子并且箱子下面是空格则箱子和人同时下移光标回到人处不用boxz if(2==arr[per.y+1][per.x]&&(''==arr[per.y+2][per.x]||''==arr[per.y+2][per.x])) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x,per.y+1);printf("♀");arr[per.y+1][per.x]=12;gotoxy(2*per.x,per.y+2);printf("□");arr[per.y+2][per.x]=2;per.y++;gotoxy(2*per.x,per.y);i++;}if(arr[3][7]==''){gotoxy(14,3);printf("○");arr[3][7]=4;}if(arr[4][7]==''){gotoxy(14,4);printf("○");arr[4][7]=4;}if(arr[5][7]==''){gotoxy(14,5);printf("○");arr[5][7]=4;}return0;}//向左移动int move_left(char arr[9][9]){//如果左边没有东西if(''==arr[per.y][per.x-1]||4==arr[per.y][per.x-1]){gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x-2,per.y);printf("♀");gotoxy(2*per.x-2,per.y);arr[per.y][per.x-1]=12;per.x--;i++;}//如果左面是箱子并且箱子左面是空格则箱子和人同时左移光标回到人处if(2==arr[per.y][per.x-1]&&(''==arr[per.y][per.x-2]||4==arr[per.y][per.x-2])) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x-2,per.y);printf("♀");arr[per.y][per.x-1]=12;gotoxy(2*per.x-4,per.y);printf("□");arr[per.y][per.x-2]=2;per.x--;gotoxy(2*per.x,per.y);i++;}if(arr[3][7]==''){gotoxy(14,3);printf("○");arr[3][7]=4;}if(arr[4][7]==''){gotoxy(14,4);printf("○");arr[4][7]=4;}if(arr[5][7]==''){gotoxy(14,5);printf("○");arr[5][7]=4;}return0;}//向右移动int move_right(char arr[9][9]){//如果右边没有东西if(''==arr[per.y][per.x+1]||4==arr[per.y][per.x+1]) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x+2,per.y);printf("♀");gotoxy(2*per.x+2,per.y);arr[per.y][per.x+1]=12;per.x++;i++;}//如果右面是箱子并且箱子右面是空格则箱子和人同时右移光标回到人处if(2==arr[per.y][per.x+1]&&(''==arr[per.y][per.x+2]||4==arr[per.y][per.x+2])) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x+2,per.y);printf("♀");arr[per.y][per.x+1]=12;gotoxy(2*per.x+4,per.y);printf("□");arr[per.y][per.x+2]=2;per.x++;gotoxy(2*per.x,per.y);i++;}if(arr[3][7]==''){gotoxy(14,3);printf("○");arr[3][7]=4;}if(arr[4][7]==''){gotoxy(14,4);printf("○");arr[4][7]=4;}if(arr[5][7]==''){gotoxy(14,5);printf("○");arr[5][7]=4;}return0;}//主函数int main(void){char ch=0;char arr[9][9]={0};//构建界面arr_set(arr);prin(arr);//游戏运行while(1){ch=_getch();switch(ch){case'w':move_up(arr);break;case's':move_down(arr);break;case'a':move_left(arr);break;case'd':move_right(arr);break;}if(arr[3][7]==2&&arr[4][7]==2&&arr[5][7]==2){gotoxy(15,15);printf("恭喜过关!共%d步",i);goto_out;}}_out:return0;}。

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

#include <dos.h>#include <stdio.h>#include <ctype.h>#include <conio.h>#include <bios.h>#include <alloc.h>typedef struct winer{int x,y;struct winer *p;}winer;char status [20][20];char far *printScreen=(char far* )0xB8000000;void putoutChar(int y,int x,char ch,char fc,char bc);void printWall(int x, int y);void printBox(int x, int y);void printBoxDes(int x, int y);void printDestination(int x, int y);void printDestination1(int x,int y,winer **win,winer **pw); void printMan(int x, int y);void init();winer *initStep1();winer *initStep2();winer *initStep3();winer *initStep4();void moveBoxSpacetoSpace(int x ,int y, char a);void moveBoxDestoSpace(int x ,int y, char a) ;void moveBoxSpacetoDes(int x, int y, char a);void moveBoxDestoDes(int x, int y, char a);int judge(int x, int y);void move(int x, int y, char a);void reset(int i);void putoutChar(int y,int x,char ch,char fc,char bc){printScreen[(x*160)+(y<<1)+0]=ch;printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc;}void printWall(int x,int y){putoutChar(y-1,x-1,219,GREEN,BLACK);status[x][y]='w';}void printBox(int x,int y){putoutChar(y-1,x-1,10,WHITE,BLACK);status[x][y]='b';}void printDestination1(int x,int y,winer **win,winer **pw) {winer *qw;putoutChar(y-1,x-1,003,YELLOW,BLACK);status[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 printDestination(int x,int y){putoutChar(y-1,x-1,003,YELLOW,BLACK);status[x][y]='m';}void printMan(int x,int y){gotoxy(y,x);_AL=02;_CX=01;_AH=0xa;geninterrupt(0x10);}void printBoxDes(int x,int y){putoutChar(y-1,x-1,10,YELLOW,BLACK);status[x][y]='i';}void init(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)status[i][j]=0;_AL=3;_AH=0;geninterrupt(0x10);gotoxy(40,4);printf("Welcome to the box world!");gotoxy(40,6);printf("You can use up, down, left,");gotoxy(40,8);printf("right key to control it, or");gotoxy(40,10);printf("you can press Esc to quit it."); gotoxy(40,12);printf("Press space to reset the game."); gotoxy(40,14);printf("Wish you have a good time !");gotoxy(40,16);printf("April , 2007");}winer *initStep1(){int x;int y;winer *win=NULL;winer *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);printDestination1(4+4,2+10,&win,&pw); printDestination1(5+4,2+10,&win,&pw); printDestination1(6+4,2+10,&win,&pw); printMan(2+4,8+10);return win;}winer *initStep2(){int x;int y;winer *win=NULL;winer *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++)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);printDestination1(5+4,7+10,&win,&pw); printDestination1(6+4,7+10,&win,&pw); printDestination1(7+4,7+10,&win,&pw); printMan(2+4,6+10);return win;}winer *initStep3(){int x;int y;winer *win=NULL;winer *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(5+4,6+10);printDestination1(2+4,5+10,&win,&pw); printDestination1(2+4,6+10,&win,&pw); printDestination1(2+4,7+10,&win,&pw); printMan(2+4,4+10);return win;}winer *initStep4(){int x;int y;winer *win=NULL;winer *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);printDestination1(3+4,7+10,&win,&pw); printDestination1(4+4,7+10,&win,&pw); printDestination1(5+4,7+10,&win,&pw); printDestination1(4+4,8+10,&win,&pw); printDestination1(5+4,8+10,&win,&pw); printMan(2+4,2+10);return win;}void moveBoxSpacetoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]=0;printf(" ");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]=0;printf(" ");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]=0;printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxDestoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]='m';printf(" ");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]='m';printf(" ");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]='m';printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxSpacetoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBoxDes(x-2,y);status[x-2][y]='i';break;case 'd':status[x+1][y]=0;printf(" ");printBoxDes(x+2,y);printMan(x+1,y);status[x+2][y]='i';break;case 'l':status[x][y-1]=0;printf(" ");printBoxDes(x,y-2);printMan(x,y-1);status[x][y-2]='i';break;case 'r':status[x][y+1]=0;printf(" ");printBoxDes(x,y+2);printMan(x,y+1);status[x][y+2]='i';break;default:break;}}void moveBoxDestoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBoxDes(x-2,y);printMan(x-1,y);status[x-2][y]='i';break;case 'd':status[x+1][y]='m';printf(" ");printMan(x+1,y);status[x+2][y]='i'; break;case 'l':status[x][y-1]='m'; printf(" ");printBoxDes(x,y-2); printMan(x,y-1);status[x][y-2]='i'; break;case 'r':status[x][y+1]='m'; printf(" ");printBoxDes(x,y+2); printMan(x,y+1);status[x][y+2]='i'; break;default:break;}}int judge(int x,int y) {int i;switch(status[x][y]) {case 0:i=1;break;case 'w':i=0;break;case 'b':i=2;break;case 'i':i=4;break;case 'm':i=3;break;default:break;}return i;}void move(int x,int y,char a){switch(a){case 'u':if(!judge(x-1,y)){gotoxy(y,x);break;}else if(judge(x-1,y)==1||judge(x-1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x-1,y);break;}else{printf(" ");printMan(x-1,y);break;}}else if(judge(x-1,y)==2){if(judge(x-2,y)==1){moveBoxSpacetoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxSpacetoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}else if(judge(x-1,y)==4){if(judge(x-2,y)==1){moveBoxDestoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxDestoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}case 'd':if(!judge(x+1,y)){gotoxy(y,x);break;}else if(judge(x+1,y)==1||judge(x+1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x+1,y);break;}else{printf(" ");printMan(x+1,y);break;}}else if(judge(x+1,y)==2){if(judge(x+2,y)==1){moveBoxSpacetoSpace(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxSpacetoDes(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}else if(judge(x+1,y)==4){if(judge(x+2,y)==1){moveBoxDestoSpace(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxDestoDes(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}case 'l':if(!judge(x,y-1)){gotoxy(y,x);break;}else if(judge(x,y-1)==1||judge(x,y-1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y-1);break;}else{printf(" ");printMan(x,y-1);break;}}else if(judge(x,y-1)==2){if(judge(x,y-2)==1){moveBoxSpacetoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxSpacetoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}else if(judge(x,y-1)==4){{moveBoxDestoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxDestoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}case 'r':if(!judge(x,y+1)){gotoxy(y,x);break;}else if(judge(x,y+1)==1||judge(x,y+1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y+1);break;}else{printf(" ");printMan(x,y+1);break;}}else if(judge(x,y+1)==2){if(judge(x,y+2)==1){moveBoxSpacetoSpace(x,y,'r');printDestination(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxSpacetoDes(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}elsegotoxy(y,x);break;}else if(judge(x,y+1)==4){if(judge(x,y+2)==1){moveBoxDestoSpace(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxDestoDes(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}elsegotoxy(y,x);break;}default:break;}}void reset(int i){switch(i){case 0:init();initStep1();break;case 1:init();initStep2();break;case 2:init();initStep3();break;case 3:init();initStep4();break;default:break;}}void main(){int key;int x;int y;int s;int i=0;winer *win;winer *pw;_AL=3;_AH=0;geninterrupt(0x10); init();win=initStep1();do{_AH=3;geninterrupt(0x10); x=_DH+1;y=_DL+1;while(bioskey(1)==0);key=bioskey(0);switch(key){case 0x4800:move(x,y,'u');break;case 0x5000:move(x,y,'d');break;case 0x4b00:move(x,y,'l');break;case 0x4d00:move(x,y,'r');break;case 0x3920:reset(i);break;default:break;}s=0;pw=win;while(pw){if(status[pw->x][pw->y]=='m')s++;pw=pw->p;}if(s==0){free(win);gotoxy(25,2);printf("congratulate! You have done this step!"); getch();i++;switch(i){case 1:init();win=initStep2();break;case 2:init();win=initStep3();break;case 3:init();win=initStep4();break;case 4:gotoxy(15,21);printf("Congratulation! \n");gotoxy(15,23);printf("You have done all the steps, Welcome to play again!"); key=0x011b;getch();break;default:break;}}}while(key!=0x011b);_AL=3;_AH=0;geninterrupt(0x10);}。

相关文档
最新文档