📄 c语言编写的推箱子程序.htm
字号:
<TABLE borderColor=#e2ca9f cellSpacing=0 cellPadding=0 width="100%"
border=0>
<TBODY>
<TR>
<TD vAlign=top align=middle width="95%">
<TABLE borderColor=#e2ca9f cellSpacing=0 cellPadding=0 width="100%"
border=1>
<TBODY>
<TR>
<TD vAlign=top borderColor=#e2ca9f align=middle width="69%"
background=C语言编写的推箱子程序_files/002.jpg>
<TABLE cellSpacing=0 cellPadding=0 width="100%" align=center
border=0>
<TBODY>
<TR>
<TD width="100%" height=40></TD></TR>
<TR>
<TD>
<FORM name=form1
action=Readnews.asp?newsid=5500&id2=5500
method=post>
<CENTER><!-- <input type=submit name=aa value="点击关闭浮动图标" width=20 title="点击广告支持本站">--></CENTER></FORM></TD></TR>
<TR>
<TD style="FONT-SIZE: 18px" vAlign=bottom align=middle
width="85%" bgColor=#dddddd height=20><STRONG><FONT
color=#003399 size=4><B>C语言编写的推箱子程序[请高手指点不足之处
</B></FONT></STRONG></TD><BR></TR>
<TR>
<TD align=middle width="100%"><BR></TD></TR>
<TR>
<TD style="FONT-SIZE: 9pt" align=middle
width="100%">发表日期:2005年5月16日 出处:原创 作者:快乐的飞 已经有1651位读者读过此文</TD></TR>
<TR>
<TD align=middle width="100%"><!--下面的这一句是设置阅读文本区的宽度-->
<TABLE style="TABLE-LAYOUT: fixed" cellSpacing=0
cellPadding=0 width="90%" align=center border=0>
<TBODY>
<TR>
<TD align=middle width="100%"></TD></TR>
<TR>
<TD style="WORD-WRAP: break-word"><FONT
class=news><BR>
<P>#include "stdio.h"<BR>#include
"graphics.h"<BR>#include "alloc.h"</P>
<P>#define VK_LEFT 0x4b00<BR>#define VK_RIGHT
0x4d00<BR>#define VK_DOWN 0x5000<BR>#define VK_UP
0x4800<BR>#define VK_HOME 0x4700<BR>#define VK_END
0x4f00<BR>#define VK_SPACE 0x3920<BR>#define
VK_ESC 0x011b<BR>#define VK_ENTER
0x1c0d<BR>#define spread 20<BR>#define VK_R
0x1372</P>
<P>/*定义主数据结构*/<BR>struct Map<BR>{<BR>
int
path[19][19];/*包含每张地图的路径,(0空白(2墙(3箱子(4目的地(5工人(6已完成的箱子<BR>
X轴在前Y在后*/<BR> int
NUM;/*关卡编号*/<BR> int Condition;
/*胜利条件*/<BR> struct Map
*next;/*指向下一个节点*/<BR>};</P>
<P>/*函数说明*/<BR>/*画图函数各有三个参数(宽度(X(Y*/<BR>void
LineWall(int x,int y); /*画墙壁*/<BR>void LineBox(int
x,int y); /*画木箱*/<BR>void LineWorker(int x,int y);
/*画工人*/<BR>void LineEnd(int x,int y);
/*画木箱的目的地*/<BR>void LineHatchway(int x,int y);
/*画地板*/<BR>void LineEndBox(int x,int
y);/*画推好的箱子*/</P>
<P>/*逻辑判断函数*/<BR>int MayMove(int key,struct Map
*head);/*判断是否可以移动,参数是用户按下的键值<BR>返回值(1代表可以,0代表不可以)*/<BR>int
Victoryed(struct Map
*P);/*判断是否获胜返回值(0代表没有,1代表获胜)*/<BR>void
FormatPath(int key,struct Map
*point);/*更改当前布局的值,2个参数分别是用户按下的键值和当前关卡的指针*/<BR>void
Format(struct Map
*point);/*画当前布局一个参数,当前关卡的指针*/</P>
<P>/*初始化函数*/<BR>void CreateRole();/*创建任务*/</P>
<P><BR>/*全局变量*/<BR>int
IsVictory=0;/*判断是否胜利*/<BR>int
WorkerX;/*当前工人位置横向的值*/<BR>int
WorkerY;/*当前工人位置竖向的值*/<BR>struct Map
*head,*Dp;</P>
<P>void format1(s,x,y)<BR>{<BR>
setfillstyle(SOLID_FILL,YELLOW);<BR>
bar(x,y,x+(s-1),y+s-1);<BR>}<BR>void
main()<BR>{<BR> int
InputKey=0;/*获取用户的按键*/<BR> int
IsMove;/*判断是否可以移动*/<BR> int
IsVoc;/*判断是否获胜*/<BR> int
gdriver=DETECT,gmode;<BR>
/*初始化任务*/<BR> ReStart:<BR>
CreateRole();<BR>
initgraph(&gdriver,&gmode,"C:\Win-TC");/*进入图形模式*/<BR>
Format(Dp);</P>
<P>
while(InputKey!=VK_ESC)<BR>
{<BR>
outtextxy(20,20,"Enter \"R\" to
restart!");
<BR>
InputKey=bioskey(0);<BR>
if(InputKey==VK_R)/*重新开始*/<BR>
goto ReStart;<BR>
/*判断是否可以移动*/<BR>
IsMove=MayMove(InputKey,Dp);<BR>
if(IsMove)/*可以移动*/<BR>
{<BR>
FormatPath(InputKey,Dp);/*更改当前地图发生变动的值*/<BR>
cleardevice();<BR>
Format(Dp);<BR>
IsVoc=Victoryed(Dp);/*判断是否获胜*/<BR>
if(IsVoc)/*获胜*/<BR>
{<BR>
outtextxy(290,170,"Victory!");<BR>
}<BR>
else<BR>
{<BR>
IsVictory=0;<BR>
continue;<BR>
}<BR>
}<BR>
else<BR>
continue;<BR> }<BR>
closegraph();</P>
<P><BR>}</P>
<P>void CreateRole()<BR>{<BR> int
x,y;<BR> Dp=(struct Map
*)malloc(sizeof(struct Map));<BR>
head=Dp;<BR>
for(x=0;x<19;x++)<BR>
for(y=0;y<19;y++)<BR>
{<BR>
if((x>=3&&x<=14)&&(y==6))<BR>
Dp->path[x][y]=2;<BR>
else
if(x==3&&(y>=7&&y<=11))<BR>
Dp->path[x][y]=2;<BR>
else
if((x>=4&&x<=14)&&y==11)<BR>
Dp->path[x][y]=2;<BR>
else
if(x==14&&(y>=7&&y<=10))<BR>
Dp->path[x][y]=2;<BR>
else
if((x>=7&&x<=10)&&(y>=7&&y<=8))<BR>
Dp->path[x][y]=2;<BR>
else
if((x>=6&&x<=10)&&y==9)<BR>
Dp->path[x][y]=4;<BR>
else<BR>
Dp->path[x][y]=0;<BR>
}<BR>
Dp->path[4][7]=2;<BR>
Dp->path[5][8]=3;<BR>
Dp->path[6][9]=3;<BR>
Dp->path[4][10]=2;<BR>
Dp->path[9][10]=2;<BR>
Dp->path[13][10]=2;<BR>
Dp->path[11][8]=3;<BR>
Dp->path[12][9]=3;<BR>
Dp->path[11][10]=5;<BR>
Dp->NUM=1;<BR>
Dp->Condition=4;<BR>
WorkerX=11;<BR> WorkerY=10;<BR>}</P>
<P>void Format(struct Map
*Point)<BR>{<BR> int
x,y;<BR> int
LineX,LineY;<BR> int
style;<BR> LineX=139;<BR>
LineY=59;<BR>
for(y=0;y<19;y++)<BR>
{<BR>
for(x=0;x<19;x++)<BR>
{<BR>
style=Point->path[x][y];<BR>
if(style==6)<BR>
IsVictory+=1;<BR>
switch(style)<BR>
{<BR>
case
2:LineWall(LineX,LineY);break;<BR>
case
3:LineBox(LineX,LineY);break;<BR>
case
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -