Matlab实现简易纪念碑谷游戏的示例代码
按上下左右键(↑↓←→)移动物块
按AD键转动视角
游戏效果:如图所示原本无法通过的路径经过视角调整即可通过
完整代码
function maze4 global maze; global GUI; global role; GUI.fig=figure('units','pixels',... 'position',[350 100 500 500],... 'Numbertitle','off',... 'name','maze',... 'Color',[0 0 0],... 'resize','off'); % 'menubar','none',... GUI.axes=axes('Units','pixels',... 'parent',GUI.fig,... 'Color',[0.05 0.05 0.05],... 'Position',[0 0 500 500],... 'Box','on', ... 'XLim',[0 10],... 'YLim',[0 10],... 'ZLim',[-1 8],... 'XColor',[0.05 0.05 0.05],... 'YColor',[0.05 0.05 0.05],... 'ZColor',[0.05 0.05 0.05],... 'xtick',[],'ytick',[],'ztick',[]); hold on;axis equal plotcube([10 10,0.5],[0 0 -0.5],1,[0.85 0.85 0.87]); maze.layer0.map=ones(10,10); maze.layer1.map=[0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 1 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 1 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 1 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0 0 0; 0 0 1 0 0 0 0 1 0 0]; maze.layer2.map=[0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 1 0 0 0; 0 0 0 0 0 1 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 1 1 0 0 0 0 0 0 0; 0 0 1 1 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0 0 0; 0 0 1 0 0 0 0 1 0 0]; maze.layer3.map=[0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 1 1 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 1 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0 0 0; 0 0 1 0 0 0 0 1 0 0]; maze.layer4.map=[0 0 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 1 0 0 1; 0 0 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 0 0 0 0; 1 0 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0 0 0; 0 0 1 0 0 0 0 1 0 0]; maze.layer5.map=[0 0 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 1; 1 0 0 0 0 0 0 0 0 0; 0 1 0 0 0 0 0 1 0 0]; maze.layer6.map=[0 0 0 0 0 0 0 0 0 1; 1 1 1 0 0 0 0 0 0 0; 1 0 0 0 0 0 0 0 0 0; 1 0 0 0 0 1 0 0 0 0; 1 1 0 0 0 1 0 0 0 0; 0 0 0 0 0 1 0 0 0 1; 0 0 0 0 0 1 0 0 0 1; 0 0 0 0 1 0 0 1 0 1; 0 0 0 0 1 0 0 1 0 0; 1 0 0 0 1 1 1 1 0 0]; maze.layer7.map=zeros(10,10); maze.layer8.map=zeros(10,10); for i=1:6 [x,y]=find(maze.(['layer',num2str(i)]).map==1); maze.(['layer',num2str(i)]).cube.num=sum(sum(maze.(['layer',num2str(i)]).map)); maze.(['layer',num2str(i)]).cube.pos=[x,y,ones(maze.(['layer',num2str(i)]).cube.num,1).*i]; maze.(['layer',num2str(i)]).cube.color=[0.85 0.85 0.95]; maze.(['layer',num2str(i)]).cube.size=[1 1 1]; maze.(['layer',num2str(i)]).cube.alpha=1; tempSize =maze.(['layer',num2str(i)]).cube.size; tempPos =maze.(['layer',num2str(i)]).cube.pos; tempAlpha=maze.(['layer',num2str(i)]).cube.alpha; tempColor=maze.(['layer',num2str(i)]).cube.color; tempNum =maze.(['layer',num2str(i)]).cube.num; arrayfun(@(i)plotcube(tempSize,tempPos(i,:)-tempSize,tempAlpha,tempColor),1:tempNum); end ax=GUI.axes; ax.CameraPosition=[39.4959 -29.1496 71.8289]; maze.scene=0; moveDirect=[-1 0 0;0 1 0;1 0 0;0 -1 0]; role.pos=[1,1,1]; role.cubex=[0 1 1 0 0 0;1 1 0 0 1 1;1 1 0 0 1 1;0 1 1 0 0 0]; role.cubey=[0 0 1 0 0 0;0 1 1 1 0 0;0 1 1 1 1 1;0 0 1 0 1 1]; role.cubez=[0 0 0 0 0 1;0 0 0 0 0 1;1 1 1 1 0 1;1 1 1 1 0 1]; fill3(role.cubex+role.pos(1)-1,... role.cubey+role.pos(2)-1,... role.cubez+role.pos(3)-1,[0.65 0.65 0.87],'tag','role') fill3(role.cubex+1-1,... role.cubey+10-1,... role.cubez+7-1,[0.85 0.65 0.67]) while(0) pause(2) disp(ax.CameraPosition) end set(gcf, 'KeyPressFcn', @key); function key(~,event) tempRolePos=role.pos; switch event.Key case 'uparrow',tempRolePos=role.pos+moveDirect(1,:); case 'rightarrow',tempRolePos=role.pos+moveDirect(2,:); case 'downarrow',tempRolePos=role.pos+moveDirect(3,:); case 'leftarrow',tempRolePos=role.pos+moveDirect(4,:); case 'a' for j=1:30 pause(0.02) tempCamPos=ax.CameraPosition; ax.CameraPosition(1)=tempCamPos(1)*cos(pi/60)-tempCamPos(2)*sin(pi/60); ax.CameraPosition(2)=tempCamPos(1)*sin(pi/60)+tempCamPos(2)*cos(pi/60); end maze.scene=mod(maze.scene+1,4); moveDirect=[moveDirect(end,:);moveDirect(1:3,:)]; case 'd' for j=1:30 pause(0.02) tempCamPos=ax.CameraPosition; ax.CameraPosition(1)=tempCamPos(1)*cos(-pi/60)-tempCamPos(2)*sin(-pi/60); ax.CameraPosition(2)=tempCamPos(1)*sin(-pi/60)+tempCamPos(2)*cos(-pi/60); end maze.scene=mod(maze.scene+3,4); moveDirect=[moveDirect(2:end,:);moveDirect(1,:)]; end switch maze.scene case 0,ax.CameraPosition=[39.4959 -29.1496 71.8289]; case 2,ax.CameraPosition=[-39.8792 27.4237 70.6419]; end switch maze.scene case 0 switch 1 case all(tempRolePos==changePos(role.pos,tempRolePos,0)) role.pos=changePos(role.pos,tempRolePos,0); case specialChange(tempRolePos,[-1 2 -2],[0 1 0]) case specialChange(tempRolePos,[-2 1 -2],[-1 0 0]) case all(tempRolePos+[0 0 1]==changePos(role.pos,tempRolePos+[0 0 1],1)) role.pos=changePos(role.pos,tempRolePos+[0 0 1],1); case all(tempRolePos+[0 0 -1]==changePos(role.pos,tempRolePos+[0 0 -1],-1)) role.pos=changePos(role.pos,tempRolePos+[0 0 -1],-1); end case 1 switch 1 case all(tempRolePos+[0 0 1]==changePos(role.pos,tempRolePos+[0 0 1],1)) role.pos=changePos(role.pos,tempRolePos+[0 0 1],1); case all(tempRolePos==changePos(role.pos,tempRolePos,0)) role.pos=changePos(role.pos,tempRolePos,0); case all(tempRolePos+[0 0 -1]==changePos(role.pos,tempRolePos+[0 0 -1],-1)) role.pos=changePos(role.pos,tempRolePos+[0 0 -1],-1); end case 2 switch 1 case all(tempRolePos==changePos(role.pos,tempRolePos,0)) role.pos=changePos(role.pos,tempRolePos,0); case specialChange(tempRolePos,[-3 1 3],[-1 0 0]) case specialChange(tempRolePos,[2 -2 -2],[0 -1 0]) case all(tempRolePos+[0 0 1]==changePos(role.pos,tempRolePos+[0 0 1],1)) role.pos=changePos(role.pos,tempRolePos+[0 0 1],1); case all(tempRolePos+[0 0 -1]==changePos(role.pos,tempRolePos+[0 0 -1],-1)) role.pos=changePos(role.pos,tempRolePos+[0 0 -1],-1); end case 3 switch 1 case all(tempRolePos+[0 0 1]==changePos(role.pos,tempRolePos+[0 0 1],1)) role.pos=changePos(role.pos,tempRolePos+[0 0 1],1); case all(tempRolePos==changePos(role.pos,tempRolePos,0)) role.pos=changePos(role.pos,tempRolePos,0); case all(tempRolePos+[0 0 -1]==changePos(role.pos,tempRolePos+[0 0 -1],-1)) role.pos=changePos(role.pos,tempRolePos+[0 0 -1],-1); end end delete(findobj('tag','role')); fill3(role.cubex+role.pos(1)-1,... role.cubey+role.pos(2)-1,... role.cubez+role.pos(3)-1,[0.65 0.65 0.87],'tag','role') end function bool=specialChange(tempRolePos,spDir,Dir) bool=0; dir=[0 0 -1;0 0 0;0 0 1]; switch 1 case role.pos(3)~=1&&role.pos(3)+spDir(3)-1~=1&&sum(abs(tempRolePos-role.pos-Dir))==0&&all(role.pos+spDir+dir(1,:)==changePos(role.pos,role.pos+spDir+dir(1,:),-1)) role.pos=changePos(role.pos,role.pos+spDir+dir(1,:),-1);bool=1; case role.pos(3)~=1&&role.pos(3)+spDir(3)~=1&&sum(abs(tempRolePos-role.pos-Dir))==0&&all(role.pos+spDir+dir(2,:)==changePos(role.pos,role.pos+spDir+dir(2,:),0)) role.pos=changePos(role.pos,role.pos+spDir+dir(2,:),0);bool=1; case role.pos(3)~=1&&role.pos(3)+spDir(3)+1~=1&&sum(abs(tempRolePos-role.pos-Dir))==0&&all(role.pos+spDir+dir(3,:)==changePos(role.pos,role.pos+spDir+dir(3,:),1)) role.pos=changePos(role.pos,role.pos+spDir+dir(3,:),1);bool=1; case role.pos(3)~=1&&role.pos(3)-spDir(3)+1~=1&&sum(abs(tempRolePos-role.pos+Dir))==0&&all(role.pos-spDir-dir(1,:)==changePos(role.pos,role.pos-spDir-dir(1,:),-1)) role.pos=changePos(role.pos,role.pos-spDir-dir(1,:),1);bool=1; case role.pos(3)~=1&&role.pos(3)-spDir(3)~=1&&sum(abs(tempRolePos-role.pos+Dir))==0&&all(role.pos-spDir-dir(2,:)==changePos(role.pos,role.pos-spDir-dir(2,:),0)) role.pos=changePos(role.pos,role.pos-spDir-dir(2,:),0);bool=1; case role.pos(3)~=1&&role.pos(3)-spDir(3)-1~=1&&sum(abs(tempRolePos-role.pos+Dir))==0&&all(role.pos-spDir-dir(3,:)==changePos(role.pos,role.pos-spDir-dir(3,:),1)) role.pos=changePos(role.pos,role.pos-spDir-dir(3,:),-1);bool=1; end end function newPos=changePos(oriPos,objPos,ydir) newPos=oriPos; if all(objPos(1:2)>=1&objPos(1:2)<=10)&&objPos(3)<=7&&objPos(3)>=1 switch ydir case 1 if maze.(['layer',num2str(objPos(3))]).map(objPos(1),objPos(2))==0&&... maze.(['layer',num2str(objPos(3)-1)]).map(objPos(1),objPos(2))==1&&... (oriPos(3)+1>=8||maze.(['layer',num2str(oriPos(3)+1)]).map(oriPos(1),oriPos(2))==0) newPos=objPos; end case 0 if maze.(['layer',num2str(objPos(3))]).map(objPos(1),objPos(2))==0&&... maze.(['layer',num2str(objPos(3)-1)]).map(objPos(1),objPos(2))==1 newPos=objPos; end case -1 if maze.(['layer',num2str(objPos(3))]).map(objPos(1),objPos(2))==0&&... maze.(['layer',num2str(objPos(3)-1)]).map(objPos(1),objPos(2))==1&&... maze.(['layer',num2str(objPos(3)+1)]).map(objPos(1),objPos(2))==0 newPos=objPos; end end end end function plotcube(varargin) % PLOTCUBE - Display a 3D-cube in the current axes % % PLOTCUBE(EDGES,ORIGIN,ALPHA,COLOR) displays a 3D-cube in the current axes % with the following properties: % * EDGES : 3-elements vector that defines the length of cube edges % * ORIGIN: 3-elements vector that defines the start point of the cube % * ALPHA : scalar that defines the transparency of the cube faces (from 0 % to 1) % * COLOR : 3-elements vector that defines the faces color of the cube % % Example: % >> plotcube([5 5 5],[ 2 2 2],.8,[1 0 0]); % >> plotcube([5 5 5],[10 10 10],.8,[0 1 0]); % >> plotcube([5 5 5],[20 20 20],.8,[0 0 1]); % Default input arguments inArgs = { ... [10 56 100] , ... % Default edge sizes (x,y and z) [10 10 10] , ... % Default coordinates of the origin point of the cube .7 , ... % Default alpha value for the cube's faces [1 0 0] ... % Default Color for the cube }; % Replace default input arguments by input values inArgs(1:nargin) = varargin; % Create all variables [edges,origin,alpha,clr] = deal(inArgs{:}); XYZ = { ... [0 0 0 0] [0 0 1 1] [0 1 1 0] ; ... [1 1 1 1] [0 0 1 1] [0 1 1 0] ; ... [0 1 1 0] [0 0 0 0] [0 0 1 1] ; ... [0 1 1 0] [1 1 1 1] [0 0 1 1] ; ... [0 1 1 0] [0 0 1 1] [0 0 0 0] ; ... [0 1 1 0] [0 0 1 1] [1 1 1 1] ... }; XYZ = mat2cell(... cellfun( @(x,y,z) x*y+z , ... XYZ , ... repmat(mat2cell(edges,1,[1 1 1]),6,1) , ... repmat(mat2cell(origin,1,[1 1 1]),6,1) , ... 'UniformOutput',false), ... 6,[1 1 1]); cellfun(@patch,XYZ{1},XYZ{2},XYZ{3},... repmat({clr},6,1),... repmat({'FaceAlpha'},6,1),... repmat({alpha},6,1)... ); view(3); end end
到此这篇关于Matlab实现简易纪念碑谷游戏的示例代码的文章就介绍到这了,更多相关Matlab纪念碑谷游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)