推箱子游戏源代码

合集下载

推箱子游戏代码

推箱子游戏代码

#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) {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);gotoxy(40,12);printf("Press space to reset the game."); gotoxy(40,14);printf("Wish you have a good time !");gotoxy(40,16);printf("December , 2015");}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++)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);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(4+4,4+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;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);printMan(x-1,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("");printBoxDes(x+2,y);printMan(x+1,y);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);}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;}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);}gotoxy(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){if(judge(x,y-2)==1){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');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');if(judge(x,y)==3)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);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();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++实现推箱子游戏源代码

#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>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语言推箱子代码

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

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

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

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语言小游戏代码)

#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语言程序代码。

首先,我们需要定义一些变量和常量来描述游戏中的元素和状态:```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++源代码

//typedef enum object////nclude<stdio.h>//#include<conio.h>//#include <windows.h>////接下来是几个宏定义,////以后用到那些被定义的数字或字符的候,直接用你想好的名称就可以了////这样只要改动宏定义,后面的数据都会变//对程序修改拓展等都很方便!//#define H 15//#define W 20//#define UP 'w'//#define DOWN 's'//#define LEFT 'a'//#define RIGHT 'd'////kbhit_remember是一个全局变量////用来记录用户的按键////因为许多函数都要知道用户按键是什么////所以我们把它设置为全局变量!//char kbhit_remember;////二维数组是用来储存数字的,不同的数字//代表的对象也不同,我们后边的绘图函数//就是根据这些数字来判断要输出什么样////的图形的//char map[H][W];////下面是枚举,像START等其实就代表着////一个数字,记忆这些数字代表了什么////显然不是一个程序员应该做的事////这些事就交给我们的枚举朋友了!////下面是游戏状态的枚举//typedef enum game//{// START,// RUNNING,// OVER//} gs;////下面是物体(对象)的枚举{// BOX_NULL_E,// BOX_SUCCESS_E,// PERSON_E,// OBSTACLE_E,// TARGET_E,// WALL_E,// NULL_E//} obj;////这下小伙伴们应该明白了吧!//typedef enum direction//{// UP_E,// DOWN_E,// LEFT_E,// RIGHT_E//} direction;//typedef struct//{// int y;// int x;// int sign;//} person_t;//typedef struct//{// int y;// int x;// int sign;//} box_t;//typedef struct//{// int y;// int x;// int sign;//} obstacle_t;//typedef struct//{// int y;// int x;// int sign;//} target_t;//person_t person;////这个数组里边都是box_t类型的元素!//box_t box[4];//obstacle_t obstacle[4];//target_t target[4];//void set_person(person_t * person, int x, int y) //{// person->x = x;// person->y = y;// //看看看!我们不需要知道人是哪个// //数字代表,只需要这样赋值就可以了// //sign,传说中的第三个成员!!// person->sign = PERSON_E;//}////箱子//void set_box(box_t box[], int n, int x, int y)//{// box[n].x = x;// box[n].y = y;// box[n].sign = BOX_NULL_E;//}////这个单词是。

推箱子游戏源代码

推箱子游戏源代码

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

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

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

第一个Java文件:package xiaoA;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;public class GameFrame extends JFrame {JPanel zhuobu = new JPanel();//工人JLabel worker = null;//箱子JLabel box = null;//目的地JLabel goal = null;JLabel[] walls = null;public static final int SPEED = 12;//设置图片大小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) {//[2.5] 使工人可以移动int xSpeed = 0, ySpeed = 0;switch (e.getKeyCode()) {case KeyEvent.VK_LEFT :xSpeed = -SPEED;worker.setIcon(new ImageIcon("image/workerUp.gif"));break;case KeyEvent.VK_RIGHT :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(worker.getX() + xSpeed, 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(worker.getX() - xSpeed, worker.getY() - ySpeed, worker.getWidth(), worker.getHeight());}}//[3.2] 使工人可以推动箱子if (worker.getBounds().intersects(box.getBounds())) {box.setBounds(box.getX() + xSpeed, box.getY() + ySpeed, box.getWidth(), box.getHeight());//[3.3] 判断箱子是否撞到墙壁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);}/***把某个图片以组件的方式加入窗体*@param imgPath图片路径*@param x x*@param y y*@param width宽度*@param height高度*@return添加完的组件*/public void addComponent(int tag, String imgPath, int x, int y) { ImageIcon img = new ImageIcon(imgPath);//创建JLabel并把ImageIcon通过构造方法传参传入//把食物放到盘子里JLabel componet = new JLabel(img);//设置盘子在桌布上的位置和大小componet.setBounds(x, y, imgSize, imgSize);//把盘子放到桌布上zhuobu.add(componet);switch (tag) {case 1:box = componet;break;case 2:goal = componet;break;case 3:worker = componet;break;}}public void addWall(String imgPath, int[][] loactions) { ImageIcon wallImg = new ImageIcon(imgPath);walls = new JLabel[66 + loactions.length];for (int i = 0; i < walls.length; i++) {//创建没每一个围墙,他们使用的是同一个图片walls[i] = new JLabel(wallImg);}for (int i = 0; i < walls.length; i++) {//创建没每一个围墙,他们使用的是同一个图片walls[i] = new JLabel(wallImg);}int index = 0;/*分别设置各个图片位置*/for (int i = 0; i < 14; i++) {//左边墙walls[index].setBounds(0, i * imgSize, imgSize, imgSize);zhuobu.add(walls[index]);index++;//右边墙walls[index].setBounds(20 * imgSize, i * imgSize, imgSize, imgSize);zhuobu.add(walls[index]);index++;}for (int i = 0; i < 19; i++) {//上边墙walls[index].setBounds((i + 1) * imgSize, 0, imgSize, imgSize);zhuobu.add(walls[index]);index++;//下边墙walls[index].setBounds((i + 1) * imgSize, 13 * imgSize, imgSize, imgSize);zhuobu.add(walls[index]);index++;}//添加中间障碍耦合解耦for (int i = 0; i < loactions.length; i++) {walls[index].setBounds(loactions[i][0]* imgSize, loactions[i][1]* imgSize, imgSize, imgSize);zhuobu.add(walls[index]);index++;}}}第二个Java文件:public class Run {public static void main(String[] args) {GameFrame gameFrame = new GameFrame("推箱子游戏…");//设置大小gameFrame.setBounds(100, 50, 21 * 48 + 5, 14 * 48 + 25);//窗体大小不可变gameFrame.setResizable(false);gameFrame.setImgSize(48);gameFrame.addComponent(3, "workerUp.png", 400, 100);gameFrame.addComponent(1, "box.png", 160, 60);gameFrame.addComponent(2, "goal.png", 80, 520);int[][] wallLocations ={{4, 5},{5, 5},{6, 5},{7, 5},{8, 5},{9, 5},{10, 5},{6, 8},{7, 8},{8, 8},{9, 8},{10, 8},{11, 5}};gameFrame.addWall("wall.png", wallLocations);gameFrame.setVisible(true);}}。

c语言推箱子源代码及实验报告

c语言推箱子源代码及实验报告

《C语言推箱子源代码及实验报告》一、概述随着计算机科学技术的迅速发展,编程语言成为了计算机科学领域的重要组成部分。

C语言作为一种通用的编程语言,被广泛应用于系统软件,应用软件,驱动程序等领域。

本次实验将用C语言实现一个推箱子的小游戏,并编写实验报告。

二、实验目的1. 掌握C语言的基本语法和编程技巧;2. 学习如何利用C语言实现简单的游戏功能;3. 提高对C语言的理解和运用能力。

三、实验内容1. 设计并编写一个推箱子的小游戏;2. 实现游戏中的推箱子逻辑,包括箱子的移动、目标检测等功能;3. 编写实验报告,记录游戏设计思路、源代码分析和遇到的问题及解决方法。

四、实验步骤1. 设计游戏地图- 定义地图大小和布局- 设计箱子、目标点和玩家的初始位置2. 确定游戏规则- 确定箱子和玩家的移动规则- 确定胜利条件和失败条件3. 编写C语言代码- 定义地图的数据结构- 实现游戏初始化和终止的函数- 实现玩家和箱子的移动函数- 实现胜利和失败条件的检测函数4. 调试和测试- 对代码进行调试,确保游戏逻辑正确 - 测试不同情况下的游戏表现五、源代码展示以下展示部分游戏源代码:```#include <stdio.h>#include <stdlib.h>#include <conio.h>int m本人n() {// 游戏初始化initMap();drawMap();while(1) {// 监听玩家输入char input = getch();// 根据玩家输入移动玩家或者箱子 // 绘制移动后的地图// 判断胜利或失败条件// 提示玩家胜利或失败}return 0;}```六、实验结果分析通过本次实验,我们成功实现了一个简单的推箱子游戏。

在编写代码的过程中,我们熟练掌握了C语言的各种语法和逻辑结构,同时也加深了对C语言的理解和应用。

七、实验总结本次实验不仅让我们掌握了C语言的编程技巧,还提高了我们的逻辑思维能力和问题解决能力。

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

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

#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;return 0;}//打印图形界面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);return 0;}//向上移动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;}return 0;}//向下移动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;}return 0;}//向左移动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;}return 0;}//向右移动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;}return 0;}//主函数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:return 0;}。

相关文档
最新文档