如何用C语言写一个推箱子游戏的程序?!
# include & ltdos.h & gt
# include & ltstdio.h & gt
# include & ltctype.h & gt
# include & ltconio.h & gt
# include & ltbios.h & gt
# include & ltalloc.h & gt
typedef结构winer
{
int x,y;
struct winer * p;
} winer
充电状态[20][20];
char far * print screen =(char far *)0xb 8000000;
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 print destination 1(int x,int y,winer **win,winer * * pw);
void printMan(int x,int y);
void init();
winer * initstep 1();
winer * init step 2();
winer * init step 3();
winer * init step 4();
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)
{
print screen[(x * 160)+(y & lt;& lt1)+0]= ch;
print screen[(x * 160)+(y & lt;& lt1)+1]=(BC * 16)+fc;
}
void printWall(int x,int y)
{
putoutChar(y-1,x-1,219,绿色,黑色);
状态[x][y]= ' w ';
}
void printBox(int x,int y)
{
putoutChar(y-1,x-1,10,白色,黑色);
状态[x][y]= ' b ';
}
void print destination 1(int x,int y,winer **win,winer **pw)
{
winer * qw
putoutChar(y-1,x-1,003,黄色,黑色);
状态[x][y]= ' m ';
if(*win==NULL)
{
* win = * pw = qw =(winer *)malloc(sizeof(winer));
(* pw)-& gt;x = x
(* pw)-& gt;y = y
(* pw)-& gt;p = NULL
}
其他
{
qw =(winer *)malloc(sizeof(winer));
qw-& gt;x = x
qw-& gt;y = y
(* pw)-& gt;p = qw
(* pw)= qw;qw-& gt;p = NULL
}
}
void printDestination(int x,int y)
{
putoutChar(y-1,x-1,003,黄色,黑色);
状态[x][y]= ' m ';
}
void printMan(int x,int y)
{
gotoxy(y,x);
_ AL = 02
_ CX = 01;
_ AH = 0xa
gen interrupt(0x 10);
}
void printBoxDes(int x,int y)
{
putoutChar(y-1,x-1,10,黄色,黑色);
状态[x][y]= ' I ';
}
void初始化()
{
int i,j;
for(I = 0;我& lt20;i++)
for(j = 0;j & lt20;j++)
状态[I][j]= 0;
_ AL = 3;
_ AH = 0;
gen interrupt(0x 10);
gotoxy(40,4);
printf("欢迎来到盒子世界!");
gotoxy(40,6);
printf("可以用上,下,左,");
gotoxy(40,8);
printf("右键控制它,或者");
gotoxy(4010);
printf("可以按Esc退出。");
gotoxy(4012);
printf("按空格键重置游戏。");
gotoxy(4014);
printf("祝你玩得开心!");
gotoxy(4016);
printf(“2007年4月”);
}
winer *initStep1()
{
int x;
int y;
winer * win = NULL
winer * pw
for(x=1,y = 5;y & lt=9;y++)
printWall(x+4,y+10);
for(y=5,x = 2;x & lt=5;x++)
printWall(x+4,y+10);
for(y=9,x = 2;x & lt=5;x++)
printWall(x+4,y+10);
for(y=1,x = 3;x & lt=8;x++)
printWall(x+4,y+10);
for(x=3,y = 3;x & lt=5;x++)
printWall(x+4,y+10);
for(x=5,y = 8;x & lt=9;x++)
printWall(x+4,y+10);
for(x=7,y = 4;x & lt=9;x++)
printWall(x+4,y+10);
for(x=9,y = 5;y & lt=7;y++)
printWall(x+4,y+10);
for(x=8,y = 2;y & lt=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,& amp赢了。pw);
printDestination1(5+4,2+10,& amp赢了。pw);
printDestination1(6+4,2+10,& amp赢了。pw);
printMan(2+4,8+10);
回赢;
}
winer *initStep2()
{
int x;
int y;
winer * win = NULL
winer * pw
for(x=1,y = 4;y & lt=7;y++)
printWall(x+4,y+10);
for(x=2,y = 2;y & lt=4;y++)
printWall(x+4,y+10);
for(x=2,y = 7;x & lt=4;x++)
printWall(x+4,y+10);
for(x=4,y = 1;x & lt=8;x++)
printWall(x+4,y+10);
for(x=8,y = 2;y & lt=8;y++)
printWall(x+4,y+10);
for(x=4,y = 8;x & lt=8;x++)
printWall(x+4,y+10);
for(x=4,y = 6;x & lt=5;x++)
printWall(x+4,y+10);
for(x=3,y = 2;x & lt=4;x++)
printWall(x+4,y+10);
for(x=4,y = 4;x & lt=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,& amp赢了。pw);
printDestination1(6+4,7+10,& amp赢了。pw);
printDestination1(7+4,7+10,& amp赢了。pw);
printMan(2+4,6+10);
回赢;
}
winer *initStep3()
{
int x;
int y;
winer * win = NULL
winer * pw
for(x=1,y = 2;y & lt=8;y++)
printWall(x+4,y+10);
for(x=2,y = 2;x & lt=4;x++)
printWall(x+4,y+10);
for(x=4,y = 1;y & lt=3;y++)
printWall(x+4,y+10);
for(x=5,y = 1;x & lt=8;x++)
printWall(x+4,y+10);
for(x=8,y = 2;y & lt=5;y++)
printWall(x+4,y+10);
for(x=5,y = 5;x & lt=7;x++)
printWall(x+4,y+10);
for(x=7,y = 6;y & lt=9;y++)
printWall(x+4,y+10);
for(x=3,y = 9;x & lt=6;x++)
printWall(x+4,y+10);
for(x=3,y = 6;y & lt=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,& amp赢了。pw);
printDestination1(2+4,6+10,& amp赢了。pw);
printDestination1(2+4,7+10,& amp赢了。pw);
printMan(2+4,4+10);
回赢;
}
winer *initStep4()
{
int x;
int y;
winer * win = NULL
winer * pw
for(x=1,y = 1;y & lt=6;y++)
printWall(x+4,y+10);
for(x=2,y = 7;y & lt=8;y++)
printWall(x+4,y+10);
for(x=2,y = 1;x & lt=7;x++)
printWall(x+4,y+10);
for(x=7,y = 2;y & lt=4;y++)
printWall(x+4,y+10);
for(x=6,y = 4;y & lt=9;y++)
printWall(x+4,y+10);
for(x=3,y = 9;x & lt=5;x++)
printWall(x+4,y+10);
for(x=3,y = 3;y & lt=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,& amp赢了。pw);
printDestination1(4+4,7+10,& amp赢了。pw);
printDestination1(5+4,7+10,& amp赢了。pw);
printDestination1(4+4,8+10,& amp赢了。pw);
printDestination1(5+4,8+10,& amp赢了。pw);
printMan(2+4,2+10);
回赢;
}
void moveBoxSpacetoSpace(int x,int y,char a)
{
开关(a)
{
案例“u”:
状态[x-1][y]= 0;
printf(" ");
printBox(x-2,y);
printMan(x-1,y);
状态[x-2][y]= ' b ';
打破;
案例“d”:
状态[x+1][y]= 0;
printf(" ");
printBox(x+2,y);
printMan(x+1,y);
状态[x+2][y]= ' b ';
打破;
案例“l”:
状态[x][y-1]= 0;
printf(" ");
printBox(x,y-2);
printMan(x,y-1);
状态[x][y-2]= ' b ';
打破;
案例“r”:
状态[x][y+1]= 0;
printf(" ");
printBox(x,y+2);
printMan(x,y+1);
状态[x][y+2]= ' b ';
打破;
默认值:
打破;
}
}
void moveBoxDestoSpace(int x,int y,char a)
{
开关(a)
{
案例“u”:
状态[x-1][y]= ' m ';
printf(" ");
printBox(x-2,y);
printMan(x-1,y);
状态[x-2][y]= ' b ';
打破;
案例“d”:
状态[x+1][y]= ' m ';
printf(" ");
printBox(x+2,y);
printMan(x+1,y);
状态[x+2][y]= ' b ';
打破;
案例“l”:
状态[x][y-1]= ' m ';
printf(" ");
printBox(x,y-2);
printMan(x,y-1);
状态[x][y-2]= ' b ';
打破;
案例“r”:
状态[x][y+1]= ' m ';
printf(" ");
printBox(x,y+2);
printMan(x,y+1);
状态[x][y+2]= ' b ';
打破;
默认值:
打破;
}
}
void moveBoxSpacetoDes(int x,int y,char a)
{
开关(a)
{
案例“u”:
状态[x-1][y]= 0;
printf(" ");
printBoxDes(x-2,y);
printMan(x-1,y);
状态[x-2][y]= ' I ';
打破;
案例“d”:
状态[x+1][y]= 0;
printf(" ");
printBoxDes(x+2,y);
printMan(x+1,y);
状态[x+2][y]= ' I ';
打破;
案例“l”:
状态[x][y-1]= 0;
printf(" ");
printBoxDes(x,y-2);
printMan(x,y-1);
状态[x][y-2]= ' I ';
打破;
案例“r”:
状态[x][y+1]= 0;
printf(" ");
printBoxDes(x,y+2);
printMan(x,y+1);
状态[x][y+2]= ' I ';
打破;
默认值:
打破;
}
}
void moveBoxDestoDes(int x,int y,char a)
{
开关(a)
{
案例“u”:
状态[x-1][y]= ' m ';
printf(" ");
printBoxDes(x-2,y);
printMan(x-1,y);
状态[x-2][y]= ' I ';
打破;
案例“d”:
状态[x+1][y]= ' m ';
printf(" ");
printBoxDes(x+2,y);
printMan(x+1,y);
状态[x+2][y]= ' I ';
打破;
案例“l”:
状态[x][y-1]= ' m ';
printf(" ");
printBoxDes(x,y-2);
printMan(x,y-1);
状态[x][y-2]= ' I ';
打破;
案例“r”:
状态[x][y+1]= ' m ';
printf(" ");
printBoxDes(x,y+2);
printMan(x,y+1);
状态[x][y+2]= ' I ';
打破;
默认值:
打破;
}
}
int judge(int x,int y)
{
int I;
开关(状态[x][y])
{
案例0:
I = 1;
打破;
案例“w”:
I = 0;
打破;
案例“b”:
I = 2;
打破;
案例“I”:
I = 4;
打破;
案例“m”:
I = 3;
打破;
默认值:
打破;
}
返回I;
}
void move(int x,int y,char a)
{
开关(a)
{
案例“u”:
如果(!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);
打破;
}
其他
{
printf(" ");
printMan(x-1,y);
打破;
}
}
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);
}
其他
gotoxy(y,x);
打破;
}
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);
}
其他
gotoxy(y,x);
打破;
}
案例“d”:
如果(!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);
打破;
}
其他
{
printf(" ");
printMan(x+1,y);
打破;
}
}
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);
}
其他
gotoxy(y,x);
打破;
}
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);
打破;
}
案例“l”:
如果(!judge(x,y-1))
{
gotoxy(y,x);
打破;
}
else if(judge(x,y-1)==1||judge(x,y-1)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x,y-1);
打破;
}
其他
{
printf(" ");
printMan(x,y-1);
打破;
}
}
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);
}
其他
gotoxy(y,x);
打破;
}
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 ');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
其他
gotoxy(y,x);
打破;
}
案例“r”:
如果(!judge(x,y+1))
{
gotoxy(y,x);
打破;
}
else if(judge(x,y+1)==1||judge(x,y+1)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x,y+1);
打破;
}
其他
{
printf(" ");
printMan(x,y+1);
打破;
}
}
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);
}
其他
gotoxy(y,x);
打破;
}
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);
}
其他
gotoxy(y,x);
打破;
}
默认值:
打破;
}
}
无效复位(int i)
{
开关(一)
{
案例0:
init();
initstep 1();
打破;
案例1:
init();
init step 2();
打破;
案例二:
init();
init step 3();
打破;
案例三:
init();
init step 4();
打破;
默认值:
打破;
}
}
void main()
{
int键;
int x;
int y;
int s;
int I = 0;
winer * win
winer * pw
_ AL = 3;
_ AH = 0;
gen interrupt(0x 10);
init();
win = init step 1();
做{
_ AH = 3;
gen interrupt(0x 10);
x = _ DH+1;
y = _ DL+1;
while(BIOS key(1)= = 0);
key = BIOS key(0);
开关(钥匙)
{
案例0x4800:
move(x,y,' u ');
打破;
案例0x5000:
move(x,y,' d ');
打破;
案例0x4b00:
move(x,y,' l ');
打破;
案例0x4d00:
move(x,y,' r ');
打破;
案例0x3920:
重置(一);
打破;
默认值:
打破;
}
s = 0;
pw = win
while(pw)
{
if(状态[pw-& gt;x][pw-& gt;y]=='m ')
s++;
pw = pw-& gt;p;
}
如果(s==0)
{
免费(赢);
gotoxy(25,2);
printf("祝贺!你做到了这一步!”);
getch();
i++;
开关(一)
{
案例1:
init();
win = init step 2();
打破;
案例二:
init();
win = init step 3();
打破;
案例三:
init();
win = init step 4();
打破;
案例4:
gotoxy(15,21);
printf("恭喜!\ n ");
gotoxy(15,23);
printf("所有步骤你都做了,欢迎再玩一次!");
key = 0x 011b;
getch();
打破;
默认值:
打破;
}
}
}while(键!= 0x 011b);
_ AL = 3;
_ AH = 0;
gen interrupt(0x 10);
}