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; %按下键盘时触发 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论