华容道小游戏.docx
- 文档编号:25530124
- 上传时间:2023-06-09
- 格式:DOCX
- 页数:24
- 大小:18.46KB
华容道小游戏.docx
《华容道小游戏.docx》由会员分享,可在线阅读,更多相关《华容道小游戏.docx(24页珍藏版)》请在冰豆网上搜索。
华容道小游戏
华容道小游戏
基于GUI界面的华容道小游戏(横刀立马)效果图如下:
实现5x4方格界面,初始化为华容道经典横刀立马局势(如下图)。
人物用数字来表示,0代表空方格,1代表曹操,2代表黄忠,3代表张飞,4代表马超,5代表赵云,6代表关羽,7代表小兵。
曹操占4个方格,黄忠、张飞、马超、赵云、关羽分别占2个方格,小兵占1个方格。
1、游戏用鼠标玩,点击人物的任一方格后,该人物被选中,随后点击空方格,如果可以移动,则选中的人物移动到方格处。
2、游戏的目标是曹操从最下面一行的中间走出,就算成功。
3、游戏能够显示已完成步数。
functionvarargout=untitled(varargin)
%UNTITLEDMATLABcodefor
%UNTITLED,byitself,createsanewUNTITLEDorraisestheexisting
%singleton*.
%
%H=UNTITLEDreturnsthehandletoanewUNTITLEDorthehandleto
%theexistingsingleton*.
%
%UNTITLED('CALLBACK',hObject,eventData,handles,...)callsthelocal
%functionnamedCALLBACKinwiththegiveninputarguments.
%
%UNTITLED('Property','Value',...)createsanewUNTITLEDorraisesthe
%existingsingleton*.Startingfromtheleft,propertyvaluepairsare
%appliedtotheGUIbeforeuntitled_OpeningFcngetscalled.An
%unrecognizedpropertynameorinvalidvaluemakespropertyapplication
%stop.Allinputsarepassedtountitled_OpeningFcnviavarargin.
%
%*SeeGUIOptionsonGUIDE'sToolsmenu.Choose"GUIallowsonlyone
%instancetorun(singleton)".
%
%Seealso:
GUIDE,GUIDATA,GUIHANDLES
%Edittheabovetexttomodifytheresponsetohelpuntitled
%LastModifiedbyGUIDE18-Jul-201823:
26:
39
%Begininitializationcode-DONOTEDIT
gui_Singleton=1;
gui_State=struct('gui_Name',mfilename,...
'gui_Singleton',gui_Singleton,...
'gui_OpeningFcn',@untitled_OpeningFcn,...
'gui_OutputFcn',@untitled_OutputFcn,...
'gui_LayoutFcn',[],...
'gui_Callback',[]);
ifnargin&&ischar(varargin{1})
=str2func(varargin{1});
end
ifnargout
[varargout{1:
nargout}]=gui_mainfcn(gui_State,varargin{:
});
else
gui_mainfcn(gui_State,varargin{:
});
end
%Endinitializationcode-DONOTEDIT
%---Executesjustbeforeuntitledismadevisible.
functionuntitled_OpeningFcn(hObject,eventdata,handles,varargin)
%Thisfunctionhasnooutputargs,seeOutputFcn.
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%varargincommandlineargumentstountitled(seeVARARGIN)
%Choosedefaultcommandlineoutputforuntitled
=hObject;
%Updatehandlesstructure
guidata(hObject,handles);
%UIWAITmakesuntitledwaitforuserresponse(seeUIRESUME)
%uiwait;
%---Outputsfromthisfunctionarereturnedtothecommandline.
functionvarargout=untitled_OutputFcn(hObject,eventdata,handles)
%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Getdefaultcommandlineoutputfromhandlesstructure
varargout{1}=;
%---Executesonbuttonpressinpushbutton1.
functionpushbutton1_Callback(hObject,eventdata,handles)
globalA;
A=[2113;
2113;
4665;
4775;
7007];
cla
holdon
%在1的位置上写上曹操
[IJ]=find(A==1);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'r')
text*(x1+x2),*(y1+y2),'曹操','fontsize',28)
%在2的位置上写上黄忠
[I,J]=find(A==2);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text*(x1+x2),**(y1+y2)+y1),'黄','fontsize',28)
text*(x1+x2),**(y1+y2)+y2),'忠','fontsize',28)
%在3的位置上写上张飞
[I,J]=find(A==3);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text*(x1+x2),**(y1+y2)+y1),'张','fontsize',28)
text*(x1+x2),**(y1+y2)+y2),'飞','fontsize',28)
%在4的位置上写上马超
[I,J]=find(A==4);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text*(x1+x2),**(y1+y2)+y1),'马','fontsize',28)
text*(x1+x2),**(y1+y2)+y2),'超','fontsize',28)
%在5的位置上写上赵云
[I,J]=find(A==5);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text*(x1+x2),**(y1+y2)+y1),'赵','fontsize',28)
text*(x1+x2),**(y1+y2)+y2),'云','fontsize',28)
%在6的位置上写上关羽
[I,J]=find(A==6);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text*(x1+*(x1+x2)),*(y1+y2),'关','fontsize',28)
text**(x1+x2)+x2),*(y1+y2),'羽','fontsize',28)
%在7的位置上写上小卒
[I,J]=find(A==7);
fori=1:
length(I)
x1=J(i)-1;
x2=J(i);
y1=5-(I(i)-1);
y2=5-I(i);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'g')
text*(x1+x2),*(y1+y2),'卒','fontsize',28)
end
%画背景
line([04],[00],'color','b','linewidth',4)
line([04],[55],'color','b','linewidth',4)
line([00],[05],'color','b','linewidth',4)
line([44],[05],'color','b','linewidth',4)
fori=1:
4
line([04],[ii],'color','b','linestyle','--')
end
fori=1:
3
line([ii],[05],'color','b','linestyle','--')
end
axisequal
axis([0405])
axisoff
functiondrawrect(x1,x2,x3,x4,color)
x=[x1
(1)x2
(1)x3
(1)x4
(1)];
y=[x1
(2)x2
(2)x3
(2)x4
(2)];
fill(x,y,color)
%hObjecthandletopushbutton1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonmousepressoveraxesbackground.
functionaxes1_ButtonDownFcn(hObject,eventdata,handles)
%hObjecthandletoaxes1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesduringobjectcreation,aftersettingallproperties.
functionaxes1_CreateFcn(hObject,eventdata,handles)
%hObjecthandletoaxes1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesempty-handlesnotcreateduntilafterallCreateFcnscalled
%Hint:
placecodeinOpeningFcntopopulateaxes1
%---Executesonbuttonpressinpushbutton2.
functionpushbutton2_Callback(hObject,eventdata,handles)
step_number=0;
globalA
while1
str=[num2str(step_number),'步'];
set,'string',str);
ifA(5,2)==1&&A(5,3)==1
ch=menu('曹操成功逃出华容道!
如果要继续玩,按“是”,否则按“否”','是','否');
switchch
case1
untitled
case2
return
end
end
[xpos,ypos]=ginput
(1);
col=ceil(xpos);
row=5-ceil(ypos)+1;
juese=A(row,col);
switchjuese
case1%点击了曹操
[I,J]=find(A==1);
rm=max(I);
rn=min(I);
lm=max(J);
ln=min(J);
%判断是否能向左移
ifln>1&A([rn,rm],ln-1)==[0;0]
A([rn,rm],ln-1)=[1;1];
A([rn,rm],lm)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向右移
iflm<4&A([rn,rm],lm+1)==[0;0]
A([rn,rm],lm+1)=[1;1];
A([rn,rm],ln)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向下移
ifrn>1&(A(rn-1,[ln,lm])==[0,0])
A(rn-1,[ln,lm])=[1,1];
A(rn+1,[ln,lm])=[0,0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向上移
ifrm<5&A(rm+1,[ln,lm])==[0,0]
A(rm+1,[ln,lm])=[1,1];
A(rm-1,[ln,lm])=[0,0];
drawmap(A)
step_number=step_number+1;
end
case2%点击了黄忠
[I,J]=find(A==2);
rm=max(I);
rn=min(I);
lm=max(J);
ln=min(J);
%判断是否能向左移
ifln>1&A([rn,rm],ln-1)==[0;0]
A([rn,rm],ln-1)=[2;2];
A([rn,rm],lm)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向右移
iflm<4&A([rn,rm],lm+1)==[0;0]
A([rn,rm],lm+1)=[2;2];
A([rn,rm],ln)=[0;0];
drawmap(A)
step_number=step_number+1;
end
ifrn>1&A(rn-1,ln)==0
ifrm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch=menu('请选择移到的方向:
','上','下')
switchch
case1%上移
A(rn-1,ln)=2;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
case2%下移
A(rm+1,ln)=2;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
else%只能上移
A(rn-1,ln)=2;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
elseifrm<5&A(rm+1,ln)==0
A(rm+1,ln)=2;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
case3%张飞
[I,J]=find(A==3);
rm=max(I);
rn=min(I);
lm=max(J);
ln=min(J);
%判断是否能向左移
ifln>1&(A([rn,rm],ln-1)==[0;0])
A([rn,rm],ln-1)=[3;3];
A([rn,rm],lm)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向右移
iflm<4&A([rn,rm],lm+1)==[0;0]
A([rn,rm],lm+1)=[3;3];
A([rn,rm],ln)=[0;0];
drawmap(A)
step_number=step_number+1;
end
ifrn>1&A(rn-1,ln)==0
ifrm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch=menu('请选择移到的方向:
','上','下')
switchch
case1%上移
A(rn-1,ln)=3;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
case2%下移
A(rm+1,ln)=3;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
else%只能上移
A(rn-1,ln)=3;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
elseifrm<5&A(rm+1,ln)==0
A(rm+1,ln)=3;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
case4%马超
[I,J]=find(A==4);
rm=max(I);
rn=min(I);
lm=max(J);
ln=min(J);
%判断是否能向左移
ifln>1&(A([rn,rm],ln-1)==[0;0])
A([rn,rm],ln-1)=[4;4];
A([rn,rm],lm)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向右移
iflm<4&A([rn,rm],lm+1)==[0;0]
A([rn,rm],lm+1)=[4;4];
A([rn,rm],ln)=[0;0];
drawmap(A)
step_number=step_number+1;
end
ifrn>1&A(rn-1,ln)==0
ifrm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch=menu('请选择移到的方向:
','上','下')
switchch
case1%上移
A(rn-1,ln)=4;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
case2%下移
A(rm+1,ln)=4;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
else%只能上移
A(rn-1,ln)=4;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
elseifrm<5&A(rm+1,ln)==0
A(rm+1,ln)=4;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
case5%赵云
[I,J]=find(A==5);
rm=max(I);
rn=min(I);
lm=max(J);
ln=min(J);
%判断是否能向左移
ifln>1&A([rn,rm],ln-1)==[0;0]
A([rn,rm],ln-1)=[5;5];
A([rn,rm],lm)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向右移
iflm<4&(A([rn,rm],lm+1)==[0;0])
A([rn,rm],lm+1)=[5;5];
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 华容道 小游戏
![提示](https://static.bdocx.com/images/bang_tan.gif)