效果:
简单原理介绍:
1构造0,1矩阵作为每片拼图的透明度,可以构造出不规则形状的拼图(image函数有alphadata属性可以设置)
jigsawmask=zeros(101*5,101*5); jigsawmask(102:404,102:404)=1; [xmesh,ymesh]=meshgrid(1:101*5,1:101*5); dis1=sqrt((xmesh-51).^2+(ymesh-253).^2); dis2=sqrt((xmesh-505+50).^2+(ymesh-253).^2); dis3=sqrt((xmesh-253).^2+(ymesh-152).^2); dis4=sqrt((xmesh-253).^2+(ymesh-505+151).^2); bool1=dis1<=50; bool2=dis2<=50; bool3=dis3<=50; bool4=dis4<=50; jigsawmask(bool1)=1; jigsawmask(bool2)=1; jigsawmask(bool3)=0; jigsawmask(bool4)=0; jigsawmask(253-25:253+25,51:505-50)=1; jigsawmask(1:152,253-25:253+25)=0; jigsawmask(505-151:505,253-25:253+25)=0;
2为每片拼图块设置buttondownfcn属性,将其改造成按钮
完整代码:
function jigsaw2(path) if nargin<1||isempty(path) [filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.jpeg','all image files';... '*.*','all files' }); path = [pathname,filename]; end oripic=imread(path); %imshow(oripic) jigsawmask=zeros(101*5,101*5); jigsawmask(102:404,102:404)=1; [xmesh,ymesh]=meshgrid(1:101*5,1:101*5); dis1=sqrt((xmesh-51).^2+(ymesh-253).^2); dis2=sqrt((xmesh-505+50).^2+(ymesh-253).^2); dis3=sqrt((xmesh-253).^2+(ymesh-152).^2); dis4=sqrt((xmesh-253).^2+(ymesh-505+151).^2); bool1=dis1<=50; bool2=dis2<=50; bool3=dis3<=50; bool4=dis4<=50; jigsawmask(bool1)=1; jigsawmask(bool2)=1; jigsawmask(bool3)=0; jigsawmask(bool4)=0; jigsawmask(253-25:253+25,51:505-50)=1; jigsawmask(1:152,253-25:253+25)=0; jigsawmask(505-151:505,253-25:253+25)=0; resizepic=imresize(oripic,[101*(3*4+2),101*(3*4+2)]); mainfig=figure('units','pixels','position',[300 80 720 400],... 'numbertitle','off','menubar','none','resize','off',... 'name','jigsaw'); mainaxes=axes('parent',mainfig,'position',[0 0 1 1],... 'xlim', [0 720],... 'ylim', [0 400],... 'nextplot','add',... 'layer','bottom',... 'ydir','reverse',... 'visible','on',... 'xtick',[], ... 'ytick',[]); image(mainaxes,[420,420+14*20],[20,20+14*20],resizepic) whitemask=150*ones(100,100,3); whitemask(2:99,2:99,:)=255; for i=1:4 for j=1:4 image(mainaxes,440+[0,60]+(j-1)*60,40+[0,60]+(i-1)*60,uint8(whitemask),... 'userdata',[i,j]','visible','on'); end end for i=1:4 for j=1:4 pichdlr(j+(i-1)*4)=image(mainaxes,420+[0,100]+(j-1)*60,20+[0,100]+(i-1)*60,uint8(zeros(100,100,3)),'alphadata',zeros(100,100),... 'userdata',j+(i-1)*4,'buttondownfcn',@putpiece,'visible','on'); end end logsheetr=zeros(1,16); function putpiece(object,~) object.userdata if logsheetr(object.userdata)==0&&handhdl.userdata~=0 object.cdata=handhdl.cdata; object.alphadata=handhdl.alphadata; logsheetr(object.userdata)=handhdl.userdata; handhdl.userdata=0; handhdl.cdata=uint8(zeros(100,100,3)); handhdl.alphadata=zeros(100,100); elseif logsheetr(object.userdata)~=0&&handhdl.userdata==0 handhdl.userdata=logsheetr(object.userdata); handhdl.cdata=object.cdata; handhdl.alphadata=object.alphadata; logsheetr(object.userdata)=0; object.cdata=uint8(zeros(100,100,3)); object.alphadata=zeros(100,100); end if all(logsheetr==1:16) text1.string='恭喜你,游戏胜利!'; end end %========================================================================== for i=1:4 for j=1:4 temppiece=resizepic((i-1)*303+1:(i-1)*303+505,(j-1)*303+1:(j-1)*303+505,:); if mod(i+j,2)==0 tempmask=jigsawmask'; else tempmask=jigsawmask; end if j==1 tempmask(:,1:101)=0; tempmask(102:404,102:201)=1; end if j==4 tempmask(:,405:505)=0; tempmask(102:404,304:404)=1; end if i==1 tempmask(1:101,:)=0; tempmask(102:201,102:404)=1; end if i==4 tempmask(405:505,:)=0; tempmask(304:404,102:404)=1; end pichdl(j+(i-1)*4)=image(mainaxes,[0,100]+(j-1)*100,[0,100]+(i-1)*100,temppiece,'alphadata',tempmask,... 'userdata',j+(i-1)*4,'buttondownfcn',@selectpiece); whitehdl(j+(i-1)*4)=image(mainaxes,[0,100]+(j-1)*100,[0,100]+(i-1)*100,uint8(240*ones(100,100,3)),'alphadata',ones(100,100),... 'userdata',[i,j],'buttondownfcn',@selectpiece,'visible','off'); end end randnum=rand(1,16); [~,logsheet]=sort(randnum); for i=1:4 for j=1:4 pichdl(logsheet(j+(i-1)*4)).xdata=[0,100]+(j-1)*100; pichdl(logsheet(j+(i-1)*4)).ydata=[0,100]+(i-1)*100; end end handhdl=image(mainaxes,[0,100],[0,100],uint8(zeros(100,100,3)),... 'alphadata',zeros(100,100),'userdata',0,'pickableparts','none'); set(gcf,'windowbuttonmotionfcn',@onhandfunc) function onhandfunc(~,~) xy=get(gca,'currentpoint'); x=xy(1,1);y=xy(1,2); handhdl.xdata=[x-50,x+50]; handhdl.ydata=[y-50,y+50]; end function selectpiece(object,~) %object.userdata if length(object.userdata)==1 if handhdl.userdata~=0 pichdl(handhdl.userdata).visible='on'; whitehdl(logsheet==handhdl.userdata).visible='off'; end object.visible='off'; whitehdl(logsheet==object.userdata).visible='on'; handhdl.userdata=object.userdata; handhdl.cdata=object.cdata; handhdl.alphadata=object.alphadata; else if handhdl.userdata==0 else ii=object.userdata(1); jj=object.userdata(2); object.visible='off'; pichdl(handhdl.userdata).xdata=[0,100]+(jj-1)*100; pichdl(handhdl.userdata).ydata=[0,100]+(ii-1)*100; pichdl(handhdl.userdata).visible='on'; logsheet(jj+(ii-1)*4)=handhdl.userdata; handhdl.userdata=0; handhdl.cdata=uint8(zeros(100,100,3)); handhdl.alphadata=zeros(100,100); end end end %========================================================================== fill([420,420+14*20,420+14*20,420],[320,320,380,380],[0.9412 0.9412 0.9412],'linewidth',5,'edgecolor',[0.7,0.7,0.7]) text1=text(430,350,'请点击拼图块中心位置移动拼图块','fontsize',12); end
运行m文件后选择一张接近方形的图片即可开始游戏
总结
到此这篇关于如何利用matlab制作一款真正的拼图小游戏的文章就介绍到这了,更多相关matlab拼图小游戏内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/595537.html