华容道小游戏_第1页
华容道小游戏_第2页
华容道小游戏_第3页
华容道小游戏_第4页
华容道小游戏_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、华容道小游戏基于 GUI 界面的华容道小游戏(横刀立马)效果图如下:实现 5x4 方格界面, 初始化为华容道经典横刀立马局势 (如下图)。人物用数字来表示, 0 代表空方格, 1 代表曹操, 2 代表黄忠, 3 代表张飞, 4 代表马超, 5 代表赵云, 6 代表 关羽, 7代表小兵。 曹操占 4个方格,黄忠、张飞、马超、赵云、关羽分别占 2个方格, 小兵占 1 个方格。1、游戏用鼠标玩,点击人物的任一方格后,该人物被选中,随后点击空方格,如果可以移 动,则选中的人物移动到方格处。2、游戏的目标是曹操从最下面一行的中间走出,就算成功。3、游戏能够显示已完成步数。function varargo

2、ut = untitled(varargin)% UNTITLED MATLAB code for untitled.fig% UNTITLED, by itself, creates a new UNTITLED or raises the existing% singleton*.% H = UNTITLED returns the handle to a new UNTITLED or the handle to% the existing singleton*.% UNTITLED(CALLBACK,hObject,eventData,handles,.) calls the loca

3、l% function named CALLBACK in UNTITLED.M with the given input arguments. % UNTITLED(Property,Value,.) creates a new UNTITLED or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before untitled_OpeningFcn gets called. An% unrecognized property name

4、 or invalid value makes property application% stop. All inputs are passed to untitled_OpeningFcn via varargin.% *See GUI Options on GUIDEs Tools menu. Choose GUI allows only one% instance to run (singleton).% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help un

5、titled% Last Modified by GUIDE v2.5 18-Jul-2018 23:26:39% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct(gui_Name , mfilename, .gui_Singleton , gui_Singleton, .gui_OpeningFcn , untitled_OpeningFcn, .gui_OutputFcn , untitled_OutputFcn, . gui_LayoutFcn , , .gui_Callback ,

6、);if nargin & ischar(varargin1)gui_State.gui_Callback = str2func(varargin1);endif nargoutvarargout1:nargout = gui_mainfcn(gui_State, varargin:);elsegui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDIT% - Executes just before untitled is made visible.function untitled_OpeningF

7、cn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to untitled (see VARARG

8、IN)% Choose default command line output for untitledhandles.output = hObject;% Update handles structure guidata(hObject, handles);% UIWAIT makes untitled wait for user response (see UIRESUME)% uiwait(handles.figure1);% - Outputs from this function are returned to the command line. function varargout

9、 = untitled_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Get default command line outpu

10、t from handles structure varargout1 = handles.output;% - Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles) global A;A = 2 1 1 3;2 1 1 3;4 6 6 5;4 7 7 5;7 0 0 7;clahold on%在 1 的位置上写上曹操I J = find(A=1);x1 = min(J)-1;x2 = max(J);y1 = 5-(min(I)-1);y2 = 5-m

11、ax(I); drawrect(x1,y1,x2,y1,x2,y2,x1,y2, r ) text(0.5*(x1+x2)-0.5,0.5*(y1+y2), 曹操 , fontsize ,28)%在 2 的位置上写上黄忠I,J = find(A=2);x1 = min(J)-1;x2 = max(J);y1 = 5-(min(I)-1);,28)y2 = 5-max(I); drawrect(x1,y1,x2,y1,x2,y2,x1,y2, y ) text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1), 黄 , fontsizetext(0.5*(x1+x2)-

12、0.26,0.5*(0.5*(y1+y2)+y2),%在 3 的位置上写上张飞 忠 , fontsize,28)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(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1), 张 , fontsize,28)text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2), 飞 , fontsize,28)%在 4 的位置上写上马超I

13、,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(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1), 马 , fontsize,28)text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2), 超 , fontsize,28)%在 5 的位置上写上赵云I,J = find(A=5); x1 = min(J)-1; x2 = max(J);y1 = 5-(mi

14、n(I)-1); y2 = 5-max(I); drawrect(x1,y1,x2,y1,x2,y2,x1,y2,y )text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1), 赵 , fontsize,28)text(0.5*(x1+x2)-0.26,0.5*(0.5*(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

15、,y2,y )text(0.5*(x1+0.5*(x1+x2)-0.26,0.5*(y1+y2), 关 , fontsize,28)text(0.5*(0.5*(x1+x2)+x2)-0.26,0.5*(y1+y2), 羽 , fontsize,28)%在 7 的位置上写上小卒 I,J = find(A=7); for i = 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(0.5*(x1+x2)-0.26,0.5*(y1+y2),

16、 卒 , fontsize ,28)end% 画背景line(0 4,0 0,color,b, linewidth,4)line(0 4,5 5,color,b, linewidth,4)line(0 0,0 5,color,b, linewidth,4)line(4 4,0 5,color,b, linewidth,4)for i = 1:4line(0 4,i i,color , b , linestyle,- )end for i = 1:3line(i i,0 5,color , b , linestyle,- )end axis equal axis(0 4 0 5) axis of

17、f function drawrect(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)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% - Executes on mous

18、e press over axes background. function axes1_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to axes1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% - Executes during object creation, after settin

19、g all properties. function axes1_CreateFcn(hObject, eventdata, handles) % hObject handle to axes1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called% Hint: place code in OpeningFcn to populate axes1% -

20、Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles) step_number=0;global Awhile 1str=num2str(step_number), 步 ;set(handles.edit2, string ,str);if A(5,2)=1&A(5,3)=1ch = menu( 曹操成功逃出华容道!如果要继续玩,按“是”,否则按“否”,是,否);switch chcase 1untitledcase 2returnendendxpos,

21、ypos = ginput(1);col = ceil(xpos);row = 5-ceil(ypos)+1;juese = A(row,col);switch juesecase 1 %点击了曹操I,J = find(A=1);rm = max(I);rn = min(I);lm = max(J);ln = min(J);%判断是否能向左移if ln1&A(rn,rm,ln-1)=0;0A(rn,rm,ln-1)=1;1;A(rn,rm,lm)=0;0;drawmap(A)step_number=step_number+1;end%判断是否能向右移if lm1&(A(rn-1,ln,lm)=

22、0,0)A(rn-1,ln,lm)=1,1;A(rn+1,ln,lm)=0,0; drawmap(A) step_number=step_number+1;end%判断是否能向上移if rm1&A(rn,rm,ln-1)=0;0A(rn,rm,ln-1)=2;2;A(rn,rm,lm)=0;0;drawmap(A) step_number=step_number+1;end%判断是否能向右移if lm1&A(rn-1,ln)=0if rm5&A(rm+1,ln)=0 %如果又能上移又能下移,则要点击的部位 ch = menu( 请选择移到的方向: , 上 , 下 ) switch chcase

23、 1%上移A(rn-1,ln) = 2;A(rn+1,ln) = 0; drawmap(A) step_number=step_number+1;case 2%下移A(rm+1,ln) = 2;A(rm-1,ln) = 0; drawmap(A) step_number=step_number+1;endelse %只能上移A(rn-1,ln) = 2;A(rn+1,ln) = 0; drawmap(A) step_number=step_number+1;endelseif rm1&(A(rn,rm,ln-1)=0;0) A(rn,rm,ln-1)=3;3;A(rn,rm,lm)=0;0;

24、drawmap(A) step_number=step_number+1;end %判断是否能向右移 if lm1&A(rn-1,ln)=0if rm5&A(rm+1,ln)=0 %如果又能上移又能下移,则要点击的部位 ch = menu( 请选择移到的方向: , 上 , 下 ) switch chcase 1%上移A(rn-1,ln) = 3;A(rn+1,ln) = 0;drawmap(A)step_number=step_number+1;case 2%下移A(rm+1,ln) = 3;A(rm-1,ln) = 0;drawmap(A)step_number=step_number+1;

25、endelse %只能上移A(rn-1,ln) = 3;A(rn+1,ln) = 0;drawmap(A)step_number=step_number+1;endelseif rm1&(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%判断是否能向右移if lm1&A(rn-1,ln)=0if rm5&A(rm+1,ln)=0%如果又能上移又能下移,则要点击的部位ch = menu( 请选择移到的方向: , 上 , 下 )switch chcase 1%上

26、移A(rn-1,ln) = 4;A(rn+1,ln) = 0; drawmap(A) step_number=step_number+1;case 2%下移A(rm+1,ln) = 4;A(rm-1,ln) = 0; drawmap(A) step_number=step_number+1;endelse %只能上移A(rn-1,ln) = 4;A(rn+1,ln) = 0; drawmap(A) step_number=step_number+1;endelseif rm1&A(rn,rm,ln-1)=0;0 A(rn,rm,ln-1)=5;5;A(rn,rm,lm)=0;0; drawma

27、p(A) step_number=step_number+1;end%判断是否能向右移if lm1&A(rn-1,ln)=0if rm5&A(rm+1,ln)=0 %如果又能上移又能下移,则要点击的部位 ch = menu( 请选择移到的方向: , 上 , 下 ) switch chcase 1%上移A(rn-1,ln) = 5;A(rn+1,ln) = 0; drawmap(A) step_number=step_number+1;case 2%下移A(rm+1,ln) = 5;A(rm-1,ln) = 0; drawmap(A) step_number=step_number+1;ende

28、lse %只能上移 A(rn-1,ln) = 5; A(rn+1,ln) = 0; drawmap(A) step_number=step_number+1;endelseif rm1&(A(rn-1,ln,lm)=0,0)A(rn-1,ln,lm)=6,6;A(rn,ln,lm)=0,0; drawmap(A) step_number=step_number+1;end%判断是否能向下移if rm1&A(rn,ln-1)=0if lm4&A(rm,lm+1)=0 %如果又能左移又能右移,则要点击的部位 ch = menu( 请选择移到的方向: , 左 , 右 )switch chcase 1

29、%左移A(rm,ln-1) = 6;A(rm,ln+1) = 0; drawmap(A) step_number=step_number+1;case 2%右移A(rm,lm+1) = 6;A(rm,lm-1) = 0; drawmap(A) step_number=step_number+1;endelse %只能左移A(rm,ln-1) = 6;A(rm,ln+1) = 0; drawmap(A) step_number=step_number+1;end elseif lm1&A(row-1,col)=0% 上if col1&A(row,col-1)=0 % 左ch = menu( 请选

30、择移到的方向: , 上 , 左 ) switch chcase 1A(row-1,col) = 7; A(row,col) = 0; drawmap(A) step_number=step_number+1;case 2A(row,col-1) = 7;A(row,col) = 0; drawmap(A) step_number=step_number+1;end elseif row5&A(row+1,col)=0 % 下 ch = menu( 请选择移到的方向: , 上 , 下 ) switch chcase 1A(row-1,col) = 7; A(row,col) = 0;drawma

31、p(A) step_number=step_number+1;case 2 A(row+1,col) = 7; A(row,col) = 0; drawmap(A) step_number=step_number+1;end elseif col1&A(row,col-1)=0 %左 if row5&A(row+1,col)=0 %下 ch = menu( 请选择移到的方向: , 左 , 下 ) switch chcase 1A(row,col-1) = 7;A(row,col) = 0; drawmap(A) step_number=step_number+1;case 2 A(row+1,

32、col) = 7; A(row,col) = 0; drawmap(A) step_number=step_number+1;endelseif col4&A(row,col+1)=0 %右ch = menu( 请选择移到的方向: , 左 , 右 ) switch chcase 1A(row,col-1) = 7; A(row,col) = 0;drawmap(A) step_number=step_number+1;case 2 A(row,col+1) = 7; A(row,col) = 0; drawmap(A)step_number=step_number+1;endelse %只能向

33、左A(row,col-1) = 7;A(row,col) = 0;drawmap(A)step_number=step_number+1;endelseif row5&A(row+1,col)=0 %下if col4&A(row,col+1)=0 %右ch = menu( 请选择移到的方向: , 下 , 右 ) switch chcase 1A(row+1,col) = 7;A(row,col) = 0;drawmap(A)step_number=step_number+1;case 2A(row,col+1) = 7;A(row,col) = 0;drawmap(A)step_number=

34、step_number+1;endelse %只能向下A(row+1,col) = 7;A(row,col) = 0;drawmap(A)step_number=step_number+1;endelseif col4&A(row,col+1)=0 %只能向右A(row,col+1) = 7;A(row,col) = 0;drawmap(A)step_number=step_number+1;endendend% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) function

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论