華容道小游戲_第1頁
華容道小游戲_第2頁
華容道小游戲_第3頁
華容道小游戲_第4頁
華容道小游戲_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、華容道小游戲基于 GUI 界面的華容道小游戲(橫刀立馬)效果圖如下:實現(xiàn) 5x4 方格界面, 初始化為華容道經典橫刀立馬局勢 (如下圖)。人物用數(shù)字來表示, 0 代表空方格, 1 代表曹操, 2 代表黃忠, 3 代表張飛, 4 代表馬超, 5 代表趙云, 6 代表 關羽, 7代表小兵。 曹操占 4個方格,黃忠、張飛、馬超、趙云、關羽分別占 2個方格, 小兵占 1 個方格。1、游戲用鼠標玩,點擊人物的任一方格后,該人物被選中,隨后點擊空方格,如果可以移 動,則選中的人物移動到方格處。2、游戲的目標是曹操從最下面一行的中間走出,就算成功。3、游戲能夠顯示已完成步數(shù)。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% 畫背景l(fā)ine(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( 曹操成功逃出華容道!如果要繼續(xù)玩,按“是”,否則按“否”,是,否);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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論