matlab編寫的迷宮小游戲.doc_第1頁
matlab編寫的迷宮小游戲.doc_第2頁
matlab編寫的迷宮小游戲.doc_第3頁
matlab編寫的迷宮小游戲.doc_第4頁
免費預覽已結束,剩余1頁可下載查看

下載本文檔

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

文檔簡介

function mazerow = 20;col = 39;rand(state,sum(100*clock)cc,rr = meshgrid(1:col,1:row);state = reshape(1:row*col,row,col);id = reshape(1:row*col,row,col);ptr_left = zeros(size(id);ptr_up = zeros(size(id);ptr_right = zeros(size(id);ptr_down = zeros(size(id);ptr_left(:,2:size(id,2) = id(:,1:size(id,2)-1);ptr_up(2:size(id,1),:) = id(1:size(id,1)-1,:);ptr_right(:,1:size(id,2)-1) = id(:,2:size(id,2);ptr_down(1:size(id,1)-1,:) = id(2:size(id,1),:);the_maze = cat(2,reshape(id,row*col,1),reshape(rr,row*col,1),reshape(cc,row*col,1),reshape(state,row*col,1),. reshape(ptr_left,row*col,1),reshape(ptr_up,row*col,1),reshape(ptr_right,row*col,1),reshape(ptr_down,row*col,1) );the_maze = sortrows(the_maze);id = the_maze(:,1);rr = the_maze(:,2);cc = the_maze(:,3);state = the_maze(:,4);ptr_left = the_maze(:,5);ptr_up = the_maze(:,6);ptr_right = the_maze(:,7);ptr_down = the_maze(:,8);clear the_maze;state, ptr_left, ptr_up, ptr_right, ptr_down=. make_pattern(row,col,rr, cc, state, ptr_left, ptr_up, ptr_right, ptr_down);f = figure(Name,迷宮,. %顯示圖形窗口的標題 Numbertitle,off,. %標題欄中是否顯示Figure No. n,其中n為圖形窗口的編號 Menubar,none,. %轉換圖形窗口菜單條的“關” Color,white,. DoubleBuffer,on,. outerposition,get(0,ScreenSize),. Colormap,.1 .71 0;.3 .4 .4;0 0 0;1 1 0,. CloseRequestFcn,close_window,. KeyPressFcn,move_spot);%建菜單gameMenu = uimenu(f,Label,游戲);uimenu(gameMenu,Label,新游戲,Accelerator,N,Callback,new_game);uimenu(gameMenu,Label,退出,Accelerator,Q,Separator,on,Callback,close_window);show_maze(row, col, rr, cc, ptr_left, ptr_up, ptr_right, ptr_down,f);% 開始cursor_pos = 1,1;current_id = 1;text(cursor_pos(1),cursor_pos(2),diamondsuit,HorizontalAlignment,Center,color,r);%計時timing = 1;start_time = clock; %按下鍵盤時觸發(fā) function move_spot(src,evnt)%獲取方向建 % 記錄過程 key = double(get(gcbf,Currentcharacter); if (ischar(key)|isscalar(key) return; end if all(cursor_pos = col,row) key = double(get(gcbf,Currentcharacter); switch key case 28 % left if ptr_left(current_id) 0 % check for legal move current_id =- ptr_left(current_id); text(cursor_pos(1),cursor_pos(2),diamondsuit,HorizontalAlignment,Center,color,.8,.8,.8); cursor_pos(1) = cursor_pos(1) - 1; text(cursor_pos(1),cursor_pos(2),diamondsuit,HorizontalAlignment,Center,color,r); end case 29 % right if ptr_right(current_id) 0 % check for legal move current_id =- ptr_right(current_id); text(cursor_pos(1),cursor_pos(2),diamondsuit,HorizontalAlignment,Center,color,.8,.8,.8); cursor_pos(1) = cursor_pos(1) + 1; text(cursor_pos(1),cursor_pos(2),diamondsuit,HorizontalAlignment,Center,color,r); end case 30 % up if ptr_up(current_id) 0 % check for legal move current_id =- ptr_up(current_id); text(cursor_pos(1),cursor_pos(2),diamondsuit,HorizontalAlignment,Center,color,.8,.8,.8); cursor_pos(2) = cursor_pos(2) - 1; text(cursor_pos(1),cursor_pos(2),diamondsuit,HorizontalAlignment,Center,color,r); end case 31 % down if ptr_down(current_id) 0 % right passage blocked line(cc(ii)+.5,cc(ii)+.5,rr(ii)-.5,rr(ii)+.5); hold on end if ptr_down(ii)0 % down passage blocked line(cc(ii)-.5,cc(ii)+.5,rr(ii)+.5,rr(ii)+.5); hold on end endaxis equalaxis(.5,col+.5,.5,row+.5)axis offset(gca,YDir,reverse)end%產生邊界道路信息function state, ptr_left, ptr_up, ptr_right, ptr_down=make_pattern(row,col, rr, cc, state, ptr_left, ptr_up, ptr_right, ptr_down)while max(state) 1 tid = ceil(col*row*rand(15,1); cityblock = cc(tid) + rr(tid); is_linked = (state(tid) = 1); temp = sortrows(cat(2,tid,cityblock,is_linked),3,2); tid = temp(1,1); dir = ceil(4*rand); switch dir case 1 if ptr_left(tid) 0 & state(tid) = state(ptr_left(tid) state( state = state(tid) | state = state(ptr_left(tid) ) = min(state(tid),state(ptr_left(tid); ptr_right(ptr_left(tid) =- ptr_right(ptr_left(tid); ptr_left(tid) =- ptr_left(tid); end case 2 if ptr_right(tid) 0 & state(tid) = state(ptr_right(tid) state( state = state(tid) | state = state(ptr_right(tid) ) = min(state(tid),state(ptr_right(tid); ptr_left(ptr_right(tid) =- ptr_left(ptr_right(tid); ptr_right(tid) =- ptr_right(tid); end case 3 if ptr_up(tid) 0 & state(tid) = state(ptr_up(tid) state( state = state(tid) | state = state(ptr_up(tid) ) = min(state(tid),state(ptr_up(tid); ptr_down(ptr_up(tid) =- ptr_down(ptr_up(tid); ptr_up(tid) =- ptr_up(tid); end case 4 if ptr_down(tid) 0 & state(tid) = state(pt

溫馨提示

  • 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

提交評論