中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用Matlab制作一款真正的拼圖小游戲

發布時間:2021-05-11 11:53:16 來源:億速云 閱讀:530 作者:小新 欄目:開發技術

這篇文章主要介紹怎么使用Matlab制作一款真正的拼圖小游戲,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

效果:

怎么使用Matlab制作一款真正的拼圖小游戲
怎么使用Matlab制作一款真正的拼圖小游戲
怎么使用Matlab制作一款真正的拼圖小游戲

簡單原理介紹:

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;

怎么使用Matlab制作一款真正的拼圖小游戲

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制作一款真正的拼圖小游戲”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

孙吴县| 裕民县| 镇江市| 长寿区| 丹棱县| 伊川县| 武宁县| 无为县| 陇西县| 东安县| 徐汇区| 略阳县| 阜新市| 巴东县| 临沧市| 靖州| 仪征市| 潢川县| 海原县| 永修县| 鄢陵县| 吴川市| 郁南县| 荔浦县| 抚州市| 峨眉山市| 都匀市| 昌宁县| 梅州市| 青阳县| 海城市| 花莲市| 梁平县| 密山市| 比如县| 兴宁市| 门头沟区| 丹棱县| 呼伦贝尔市| 乐昌市| 邯郸县|