📄 -一个c语言编写的箱子游戏.txt
字号:
i,j;<BR>for(i=0;i<20;i++)<BR>for(j=0;j<20;j++)<BR>ghouse[i][j]=0;<BR>_AL=3;<BR>_AH=0;<BR>geninterrupt(0x10);<BR>gotoxy(40,4);<BR>printf("Welcome
to come box
world!");<BR>gotoxy(40,6);<BR>printf("Press
up,down,left,right to
play.");<BR>gotoxy(40,8);<BR>printf("Press Esc
to quit
it.");<BR>gotoxy(40,10);<BR>printf("Press space
to reset the
game.");<BR>gotoxy(40,12);<BR>printf("Producer :
wangdehao.");<BR>gotoxy(40,14);<BR>printf("Mar.
30th 2003.");<BR>}<BR><BR>/* 第一关的图象初始化
*/<BR>winer *inithouse1()<BR>{<BR>int
x,y;<BR>winer
*win=NULL,*pw;<BR>for(x=1,y=5;y<=9;y++)<BR>printwall(x+4,y+10);<BR>for(y=5,x=2;x<=5;x++)<BR>printwall(x+4,y+10);<BR>for(y=9,x=2;x<=5;x++)<BR>printwall(x+4,y+10);<BR>for(y=1,x=3;x<=8;x++)<BR>printwall(x+4,y+10);<BR>for(x=3,y=3;x<=5;x++)<BR>printwall(x+4,y+10);<BR>for(x=5,y=8;x<=9;x++)<BR>printwall(x+4,y+10);<BR>for(x=7,y=4;x<=9;x++)<BR>printwall(x+4,y+10);<BR>for(x=9,y=5;y<=7;y++)<BR>printwall(x+4,y+10);<BR>for(x=8,y=2;y<=3;y++)<BR>printwall(x+4,y+10);<BR>printwall(5+4,4+10);<BR>printwall(5+4,7+10);<BR>printwall(3+4,2+10);<BR>printbox(3+4,6+10);<BR>printbox(3+4,7+10);<BR>printbox(4+4,7+10);<BR>printwhither1(4+4,2+10,&win,&pw);<BR>printwhither1(5+4,2+10,&win,&pw);<BR>printwhither1(6+4,2+10,&win,&pw);<BR>printman(2+4,8+10);<BR>return
win;<BR>}<BR><BR>/* 第三关的图象初始化 */<BR>winer
*inithouse3()<BR>{int x,y;<BR>winer
*win=NULL,*pw;<BR>for(x=1,y=2;y<=8;y++)<BR>printwall(x+4,y+10);<BR>for(x=2,y=2;x<=4;x++)<BR>printwall(x+4,y+10);<BR>for(x=4,y=1;y<=3;y++)<BR>printwall(x+4,y+10);<BR>for(x=5,y=1;x<=8;x++)<BR>printwall(x+4,y+10);<BR>for(x=8,y=2;y<=5;y++)<BR>printwall(x+4,y+10);<BR>for(x=5,y=5;x<=7;x++)<BR>printwall(x+4,y+10);<BR>for(x=7,y=6;y<=9;y++)<BR>printwall(x+4,y+10);<BR>for(x=3,y=9;x<=6;x++)<BR>printwall(x+4,y+10);<BR>for(x=3,y=6;y<=8;y++)<BR>printwall(x+4,y+10);<BR>printwall(2+4,8+10);<BR>printwall(5+4,7+10);<BR>printbox(6+4,3+10);<BR>printbox(4+4,4+10);<BR>printbox(5+4,6+10);<BR>printwhither1(2+4,5+10,&win,&pw);<BR>printwhither1(2+4,6+10,&win,&pw);<BR>printwhither1(2+4,7+10,&win,&pw);<BR>printman(2+4,4+10);<BR>return
win;<BR>}<BR><BR>/* 第二关的图象初始化 */<BR>winer
*inithouse2()<BR>{int x,y;<BR>winer
*win=NULL,*pw;<BR>for(x=1,y=4;y<=7;y++)<BR>printwall(x+4,y+10);<BR>for(x=2,y=2;y<=4;y++)<BR>printwall(x+4,y+10);<BR>for(x=2,y=7;x<=4;x++)<BR>printwall(x+4,y+10);<BR>for(x=4,y=1;x<=8;x++)<BR>printwall(x+4,y+10);<BR>for(x=8,y=2;y<=8;y++)<BR>printwall(x+4,y+10);<BR>for(x=4,y=8;x<=8;x++)<BR>printwall(x+4,y+10);<BR>for(x=4,y=6;x<=5;x++)<BR>printwall(x+4,y+10);<BR>for(x=3,y=2;x<=4;x++)<BR>printwall(x+4,y+10);<BR>for(x=4,y=4;x<=5;x++)<BR>printwall(x+4,y+10);<BR>printwall(6+4,3+10);<BR>printbox(3+4,5+10);<BR>printbox(6+4,6+10);<BR>printbox(7+4,3+10);<BR>printwhither1(5+4,7+10,&win,&pw);<BR>printwhither1(6+4,7+10,&win,&pw);<BR>printwhither1(7+4,7+10,&win,&pw);<BR>printman(2+4,6+10);<BR>return
win;<BR>}<BR><BR>/* 第四关的图象初始化 */<BR>winer
*inithouse4()<BR>{<BR>int x,y;<BR>winer
*win=NULL,*pw;<BR>for(x=1,y=1;y<=6;y++)<BR>printwall(x+4,y+10);<BR>for(x=2,y=7;y<=8;y++)<BR>printwall(x+4,y+10);<BR>for(x=2,y=1;x<=7;x++)<BR>printwall(x+4,y+10);<BR>for(x=7,y=2;y<=4;y++)<BR>printwall(x+4,y+10);<BR>for(x=6,y=4;y<=9;y++)<BR>printwall(x+4,y+10);<BR>for(x=3,y=9;x<=5;x++)<BR>printwall(x+4,y+10);<BR>for(x=3,y=3;y<=4;y++)<BR>printwall(x+4,y+10);<BR>printwall(3+4,8+10);<BR>printbox(3+4,5+10);<BR>printbox(4+4,4+10);<BR>printbox(4+4,6+10);<BR>printbox(5+4,5+10);<BR>printbox(5+4,3+10);<BR>printwhither1(3+4,7+10,&win,&pw);<BR>printwhither1(4+4,7+10,&win,&pw);<BR>printwhither1(5+4,7+10,&win,&pw);<BR>printwhither1(4+4,8+10,&win,&pw);<BR>printwhither1(5+4,8+10,&win,&pw);<BR>printman(2+4,2+10);<BR>return
win;<BR>}<BR><BR>/* 移动在空地上的箱子到空地上
*/<BR>movebox(int x,int y,char
a)<BR>{<BR>switch(a)<BR>{<BR>case
'u':ghouse[x-1][y]=0;printf("
");<BR>printbox(x-2,y);printman(x-1,y);<BR>ghouse[x-2][y]='b';break;<BR>case
'd':ghouse[x+1][y]=0;printf("
");<BR>printbox(x+2,y);printman(x+1,y);<BR>ghouse[x+2][y]='b';break;<BR>case
'l':ghouse[x][y-1]=0;printf("
");<BR>printbox(x,y-2);printman(x,y-1);<BR>ghouse[x][y-2]='b';break;<BR>case
'r':ghouse[x][y+1]=0;printf("
");<BR>printbox(x,y+2);printman(x,y+1);<BR>ghouse[x][y+2]='b';break;<BR>default:
break;<BR>}<BR>}<BR><BR>/* 移动在目的地上的箱子到空地上
*/<BR>moveinbox(int x,int y,char
a)<BR>{<BR>switch(a)<BR>{<BR>case
'u':ghouse[x-1][y]='m';printf("
");<BR>printbox(x-2,y);printman(x-1,y);<BR>ghouse[x-2][y]='b';break;<BR>case
'd':ghouse[x+1][y]='m';printf("
");<BR>printbox(x+2,y);printman(x+1,y);<BR>ghouse[x+2][y]='b';break;<BR>case
'l':ghouse[x][y-1]='m';printf("
");<BR>printbox(x,y-2);printman(x,y-1);<BR>ghouse[x][y-2]='b';break;<BR>case
'r':ghouse[x][y+1]='m';printf("
");<BR>printbox(x,y+2);printman(x,y+1);<BR>ghouse[x][y+2]='b';break;<BR>default:
break;<BR>}<BR>}<BR><BR><BR><BR>/*
移动在空地上的箱子到目的地上 */<BR>moveboxin(int x,int y,char
a)<BR>{<BR>switch(a)<BR>{<BR>case
'u':ghouse[x-1][y]=0;printf("
");<BR>printboxin(x-2,y);printman(x-1,y);<BR>ghouse[x-2][y]='i';break;<BR>case
'd':ghouse[x+1][y]=0;printf("
");<BR>printboxin(x+2,y);printman(x+1,y);<BR>ghouse[x+2][y]='i';break;<BR>case
'l':ghouse[x][y-1]=0;printf("
");<BR>printboxin(x,y-2);printman(x,y-1);<BR>ghouse[x][y-2]='i';break;<BR>case
'r':ghouse[x][y+1]=0;printf("
");<BR>printboxin(x,y+2);printman(x,y+1);<BR>ghouse[x][y+2]='i';break;<BR>default:
break;<BR>}<BR>}<BR><BR>/* 移动在目的地上的箱子到目的地
*/<BR>moveinboxin(int x,int y,char
a)<BR>{<BR>switch(a)<BR>{<BR>case
'u':ghouse[x-1][y]='m';printf("
");<BR>printboxin(x-2,y);printman(x-1,y);<BR>ghouse[x-2][y]='i';break;<BR>case
'd':ghouse[x+1][y]='m';printf("
");<BR>printboxin(x+2,y);printman(x+1,y);<BR>ghouse[x+2][y]='i';break;<BR>case
'l':ghouse[x][y-1]='m';printf("
");<BR>printboxin(x,y-2);printman(x,y-1);<BR>ghouse[x][y-2]='i';break;<BR>case
'r':ghouse[x][y+1]='m';printf("
");<BR>printboxin(x,y+2);printman(x,y+1);<BR>ghouse[x][y+2]='i';break;<BR>default:
break;<BR>}<BR>}<BR><BR>/* 判断特定的坐标上的状态 */<BR>int
judge(int x,int y)<BR>{<BR>int
i;<BR>switch(ghouse[x][y])<BR>{<BR>case 0:
i=1;break;<BR>case 'w': i=0;break;<BR>case 'b':
i=2;break;<BR>case 'i': i=4;break;<BR>case 'm':
i=3;break;<BR>default: break;<BR>}<BR>return
i;<BR>}<BR><BR>/* 处理按下键盘后,人物移动的主函数
*/<BR>move(int x,int y,char
a)<BR>{<BR><BR>switch(a)<BR>{<BR>case
'u':if(!judge(x-1,y))
{gotoxy(y,x);break;}<BR>else
if(judge(x-1,y)==1||judge(x-1,y)==3)<BR>{if(judge(x,y)==3)<BR>{
printwhither(x,y);printman(x-1,y);break;}<BR>else<BR>{printf("
");printman(x-1,y);break;}<BR>}<BR>else
if(judge(x-1,y)==2)<BR>{
if(judge(x-2,y)==1)<BR>{movebox(x,y,'u');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y,x-1);<BR>}<BR>else
if(judge(x-2,y)==3)<BR>{
moveboxin(x,y,'u');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y,x-1);<BR>}<BR>else
gotoxy(y,x);<BR>break;<BR>}<BR>else
if(judge(x-1,y)==4)<BR>{
if(judge(x-2,y)==1)<BR>{moveinbox(x,y,'u');<BR>if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x-1);<BR>}<BR>else
if(judge(x-2,y)==3)<BR>{
moveinboxin(x,y,'u');<BR>if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x-1);<BR>}<BR>else
gotoxy(y,x);<BR>break;<BR>}<BR>case
'd':if(!judge(x+1,y))
{gotoxy(y,x);break;}<BR>else
if(judge(x+1,y)==1||judge(x+1,y)==3)<BR>{if(judge(x,y)==3)<BR>{
printwhither(x,y);printman(x+1,y);break;}<BR>else<BR>{printf("
");printman(x+1,y);break;}<BR>}<BR>else
if(judge(x+1,y)==2)<BR>{
if(judge(x+2,y)==1)<BR>{movebox(x,y,'d');<BR>if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);<BR>}<BR>else
if(judge(x+2,y)==3)<BR>{moveboxin(x,y,'d');<BR>if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);<BR>}<BR>else
gotoxy(y,x);<BR>break;<BR>}<BR>else
if(judge(x+1,y)==4)<BR>{
if(judge(x+2,y)==1)<BR>{moveinbox(x,y,'d');<BR>if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);<BR>}<BR>else
if(judge(x+2,y)==3)<BR>{moveinboxin(x,y,'d');<BR>if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);<BR>}<BR>else
gotoxy(y,x);<BR>break;<BR>}<BR><BR>case
'l':if(!judge(x,y-1))
{gotoxy(y,x);break;}<BR>else
if(judge(x,y-1)==1||judge(x,y-1)==3)<BR>{if(judge(x,y)==3)<BR>{
printwhither(x,y);printman(x,y-1);break;}<BR>else<BR>{printf("
");printman(x,y-1);break;}<BR>}<BR>else
if(judge(x,y-1)==2)<BR>{
if(judge(x,y-2)==1)<BR>{movebox(x,y,'l');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y-1,x);<BR>}<BR>else
if(judge(x,y-2)==3)<BR>{moveboxin(x,y,'l');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y-1,x);<BR>}<BR>else
gotoxy(y,x);<BR>break;<BR>}<BR>else
if(judge(x,y-1)==4)<BR>{
if(judge(x,y-2)==1)<BR>{moveinbox(x,y,'l');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y-1,x);<BR>}<BR>else
if(judge(x,y-2)==3)<BR>{moveinboxin(x,y,'l');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y-1,x);<BR>}<BR>else
gotoxy(y,x);<BR>break;<BR>}<BR>case
'r':if(!judge(x,y+1))
{gotoxy(y,x);break;}<BR>else
if(judge(x,y+1)==1||judge(x,y+1)==3)<BR>{if(judge(x,y)==3)<BR>{printwhither(x,y);printman(x,y+1);break;}<BR>else<BR>{printf("
");printman(x,y+1);break;}<BR>}<BR>else
if(judge(x,y+1)==2)<BR>{
if(judge(x,y+2)==1)<BR>{movebox(x,y,'r');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y+1,x);<BR>}<BR>else
if(judge(x,y+2)==3)<BR>{moveboxin(x,y,'r');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y+1,x);<BR>}<BR>else
gotoxy(y,x);<BR>break;<BR>}<BR>else
if(judge(x,y+1)==4)<BR>{
if(judge(x,y+2)==1)<BR>{moveinbox(x,y,'r');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y+1,x);<BR>}<BR>else
if(judge(x,y+2)==3)<BR>{moveinboxin(x,y,'r');<BR>if(judge(x,y)==3)
printwhither(x,y); gotoxy(y+1,x);<BR>}<BR>else
gotoxy(y,x);<BR>break;<BR>}<BR>default:
break;<BR>}<BR>}<BR><BR>/* 按下空格键后,回到本关开头的函数
*/<BR>void reset(int
i)<BR>{<BR>switch(i)<BR>{<BR>case 0:
init();<BR>inithouse1();break;<BR>case 1:
init();<BR>inithouse2();break;<BR>case 2:
init();<BR>inithouse3();break;<BR>case 3:
init();<BR>inithouse4();break; <BR>default:break;<BR>}<BR>}<BR><BR>/*
主函数main */<BR>void main()<BR>{<BR>int
key,x,y,s,i=0;<BR>winer
*win,*pw;<BR>_AL=3;_AH=0;<BR>geninterrupt(0x10);<BR>init();<BR>win=inithouse1();<BR><BR>do{<BR>_AH=3;<BR>geninterrupt(0x10);<BR>x=_DH+1;y=_DL+1;<BR>while(bioskey(1)==0);<BR>key=bioskey(0);<BR>switch(key)<BR>{<BR>case
0x4800:move(x,y,'u');break; /* 按下向上键后 */<BR>case
0x5000:move(x,y,'d');break; /* 按下向下键后 */<BR>case
0x4b00:move(x,y,'l');break; /* 按下向左键后 */<BR>case
0x4d00:move(x,y,'r');break; /* 按下向右键后 */<BR>case
0x3920:reset(i);break; /* 按下空格键后
*/<BR>default:break;<BR>}<BR>s=0;<BR>pw=win;<BR>while(pw)<BR>{<BR>if(ghouse[pw->x][pw->y]=='m')
s++;<BR>pw=pw->p;<BR>}<BR>if(s==0)<BR>{<BR>free(win);<BR>gotoxy(25,2);<BR>printf("congratulate!
you did a good
job!");<BR>getch();<BR>i++;<BR>switch(i)<BR>{<BR>case
1: init();<BR>win=inithouse2();break;<BR>case 2:
init();<BR>win=inithouse3();break;<BR>case 3:
init();<BR>win=inithouse4();break;<BR>case 4:
gotoxy(15,21);<BR>printf("My dear Friend, How
smart you are! Welcome to play
again!");<BR>key=0x011b;getch();break;<BR>default:
break;<BR>}<BR>}<BR><BR>}while(key!=0x011b);<BR><BR>_AL=3;<BR>_AH=0;<BR>geninterrupt(0x10);<BR>}</FONT></P></TD>
<TD vAlign=top width="5%"
background=编程技巧-一个C语言编写的箱子游戏.files/002.gif
height=520> </TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD width="100%" bgColor=#528feb height=21> </TD></TR>
<TR>
<TD width="100%" bgColor=#528feb height=21>
<P align=center><FONT color=#eaeaea
size=2>Copyright ©wanbo collage
2003-2003版权所有</FONT></P></TD></TR>
<TR>
<TD width="100%" bgColor=#528feb height=21>
<P align=center><FONT color=#eaeaea
size=2>计算机协会</FONT></P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD vAlign=top width=201 bgColor=#528feb
height=636> </TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -