华容道小游戏.docx

上传人:b****3 文档编号:10979021 上传时间:2023-05-28 格式:DOCX 页数:25 大小:21.50KB
下载 相关 举报
华容道小游戏.docx_第1页
第1页 / 共25页
华容道小游戏.docx_第2页
第2页 / 共25页
华容道小游戏.docx_第3页
第3页 / 共25页
华容道小游戏.docx_第4页
第4页 / 共25页
华容道小游戏.docx_第5页
第5页 / 共25页
华容道小游戏.docx_第6页
第6页 / 共25页
华容道小游戏.docx_第7页
第7页 / 共25页
华容道小游戏.docx_第8页
第8页 / 共25页
华容道小游戏.docx_第9页
第9页 / 共25页
华容道小游戏.docx_第10页
第10页 / 共25页
华容道小游戏.docx_第11页
第11页 / 共25页
华容道小游戏.docx_第12页
第12页 / 共25页
华容道小游戏.docx_第13页
第13页 / 共25页
华容道小游戏.docx_第14页
第14页 / 共25页
华容道小游戏.docx_第15页
第15页 / 共25页
华容道小游戏.docx_第16页
第16页 / 共25页
华容道小游戏.docx_第17页
第17页 / 共25页
华容道小游戏.docx_第18页
第18页 / 共25页
华容道小游戏.docx_第19页
第19页 / 共25页
华容道小游戏.docx_第20页
第20页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

华容道小游戏.docx

《华容道小游戏.docx》由会员分享,可在线阅读,更多相关《华容道小游戏.docx(25页珍藏版)》请在冰点文库上搜索。

华容道小游戏.docx

华容道小游戏

Revisedat2pmonDecember25,2020.

 

华容道小游戏

华容道小游戏

基于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&

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2