文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

怎么用Matlab编写简易版连连看小游戏

2023-06-29 09:12

关注

这篇文章主要介绍“怎么用Matlab编写简易版连连看小游戏”,在日常操作中,相信很多人在怎么用Matlab编写简易版连连看小游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Matlab编写简易版连连看小游戏”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

游戏截图

怎么用Matlab编写简易版连连看小游戏

怎么用Matlab编写简易版连连看小游戏

怎么用Matlab编写简易版连连看小游戏

游戏组装说明

我们的变量path输入的是图像包的相对路径或绝对路径

怎么用Matlab编写简易版连连看小游戏

这里我们由于文件夹名称就叫“图片”所以直接path='图片’是没啥问题的。

怎么用Matlab编写简易版连连看小游戏

文件夹里就是图片素材啦,其名称没有任何要求,形状最好是方的,不过不是的话问题也不大,程序会自动将其拉伸成方的。。。。。。。。

怎么用Matlab编写简易版连连看小游戏

我们这里要求的是读入jpg格式,想要png,jpg都读的话,。。。。。最简单的就是直接读两次

完整代码

function ConnectingGame%连连看图片导入============================================================path='图片';picInformation=dir(fullfile(path,'*.jpg'));N=length(picInformation);for i=1:N    picList.(['pic',num2str(i)])=...        imread([path,'\',picInformation(i).name]);end%全局变量==================================================================global selectedPos clickPosglobal redLinePntsglobal picMatselectedPos=[];redLinePnts=[];%图片矩阵生成==============================================================    function createMat(~,~)        tempMat=zeros(8,6);        while any(any(tempMat==0))            zeosPos=find(tempMat==0);            temprand=rand(1,length(zeosPos));            [~,tempSort]=sort(temprand);            tempNum=zeosPos(tempSort(1:2));            tempMat(tempNum)=randi(N);        end        picMat=zeros(10,8);        picMat(2:9,2:7)=tempMat;    endcreateMat()%主要框架生成==============================================================MainFig=figure('units','pixels','position',[750 250 (9*100+10*5+20)*0.5 (7*100+8*5+20)*0.5],...    'Numbertitle','off','menubar','none','resize','off',...    'name','ConnectingGame | by slandarer');axes('parent',MainFig,'position',[0 0 1 1],...    'XLim', [40 10*100+10*5-40],...    'YLim', [40 8*100+8*5-40],...    'color',[0.95,0.95,0.95],...    'NextPlot','add',...    'layer','bottom',...    'Visible','on',...    'YDir','reverse',...    'XTick',[], ...    'YTick',[]);uh2=uimenu('label','帮助');uimenu(uh2,'label','重新开始','callback',@restartGame)for i=2:9    for j=2:7        drawPicHdl(i,j)=image([(i-1)*100,i*100]+(i-1)*5,[(j-1)*100,j*100]+(j-1)*5,...            picList.(['pic',num2str(picMat(i,j))]),'tag',[num2str(i),num2str(j)],...            'ButtonDownFcn',@clickOnPic);    endend%重新开始函数==============================================================    function restartGame(~,~)        createMat()        for ii=2:9            for jj=2:7                set(drawPicHdl(ii,jj),'CData',picList.(['pic',num2str(picMat(ii,jj))]))            end        end    end%主函数====================================================================    function clickOnPic(object,~)        redLinePnts=[];        clickPos=[str2num(object.Tag(1)),str2num(object.Tag(2))];        if isempty(selectedPos),selectedPos=clickPos;end        if ~all(selectedPos==clickPos)            %-------------------------------------------------------------------------------------------------            condition1=0;condition2=0;            switch 1                case any(selectedPos(1)==clickPos(1))                    condition1=abs(selectedPos(2)-clickPos(2))==1;                    tempVector1=sort([selectedPos(2),clickPos(2)])+[1 -1];                    tempSum=sum(picMat(clickPos(1),tempVector1(1):tempVector1(2)));                    condition2=(tempSum==0)&~isempty(picMat(clickPos(1),tempVector1(1):tempVector1(2)));                case any(selectedPos(2)==clickPos(2))                    condition1=abs(selectedPos(1)-clickPos(1))==1;                    tempVector1=sort([selectedPos(1),clickPos(1)])+[1 -1];                    tempSum=sum(picMat(tempVector1(1):tempVector1(2),clickPos(2)));                    condition2=(tempSum==0)&~isempty(picMat(clickPos(1),tempVector1(1):tempVector1(2)));            end            if (condition1||condition2)&&picMat(clickPos(1),clickPos(2))==picMat(selectedPos(1),selectedPos(2))                redLinePnts=[selectedPos;clickPos];            end            %-------------------------------------------------------------------------------------------------            if isempty(redLinePnts)                tempNode=[selectedPos(1),clickPos(2)];                tempVector1=[selectedPos(2)+1,clickPos(2)].*(clickPos(2)>selectedPos(2))+...                    [clickPos(2),selectedPos(2)-1].*(clickPos(2)<=selectedPos(2));                tempVector2=[selectedPos(1),clickPos(1)-1].*(clickPos(1)>selectedPos(1))+...                    [clickPos(1)+1,selectedPos(1)].*(clickPos(1)<=selectedPos(1));                condition1=(sum(picMat(selectedPos(1),tempVector1(1):tempVector1(2)))==0);                condition2=sum(picMat(tempVector2(1):tempVector2(2),clickPos(2)))==0;                condition3=picMat(selectedPos(1),selectedPos(2))==picMat(clickPos(1),clickPos(2));                if all([condition1,condition2,condition3])                    redLinePnts=[selectedPos;tempNode;clickPos];                end            end            if isempty(redLinePnts)                tempNode=[clickPos(1),selectedPos(2)];                tempVector1=[clickPos(2)+1,selectedPos(2)].*(selectedPos(2)>clickPos(2))+...                    [selectedPos(2),clickPos(2)-1].*(selectedPos(2)<=clickPos(2));                tempVector2=[clickPos(1),selectedPos(1)-1].*(selectedPos(1)>clickPos(1))+...                    [selectedPos(1)+1,clickPos(1)].*(selectedPos(1)<=clickPos(1));                condition1=sum(picMat(clickPos(1),tempVector1(1):tempVector1(2)))==0;                condition2=sum(picMat(tempVector2(1):tempVector2(2),selectedPos(2)))==0;                condition3=picMat(selectedPos(1),selectedPos(2))==picMat(clickPos(1),clickPos(2));                if all([condition1,condition2,condition3])                    redLinePnts=[selectedPos;tempNode;clickPos];                end            end            %-------------------------------------------------------------------------------------------------            if isempty(redLinePnts)                for ii=[selectedPos(1):-1:1,selectedPos(1):10]                    tempNode1=[ii,selectedPos(2)];                    tempNode2=[ii,clickPos(2)];                    tempVector1=[selectedPos(1)+1,ii].*(ii>selectedPos(1))+[ii,selectedPos(1)-1].*(ii<=selectedPos(1));                    tempVector2=[clickPos(1)+1,ii].*(ii>clickPos(1))+[ii,clickPos(1)-1].*(ii<=clickPos(1));                    tempVector3=sort([selectedPos(2),clickPos(2)]);                    condition1=sum(picMat(tempVector1(1):tempVector1(2),selectedPos(2)))==0;                    condition2=sum(picMat(tempVector2(1):tempVector2(2),clickPos(2)))==0;                    condition3=sum(picMat(ii,tempVector3(1):tempVector3(2)))==0;                    condition4=picMat(selectedPos(1),selectedPos(2))==picMat(clickPos(1),clickPos(2));                    if all([condition1,condition2,condition3,condition4])                        redLinePnts=[selectedPos;tempNode1;tempNode2;clickPos];                        break                    end                end            end            if isempty(redLinePnts)                for jj=[selectedPos(2):-1:1,selectedPos(2):8]                    tempNode1=[selectedPos(1),jj];                    tempNode2=[clickPos(1),jj];                    tempVector1=[selectedPos(2)+1,jj].*(jj>selectedPos(2))+[jj,selectedPos(2)-1].*(jj<=selectedPos(2));                    tempVector2=[clickPos(2)+1,jj].*(jj>clickPos(2))+[jj,clickPos(2)-1].*(jj<=clickPos(2));                    tempVector3=sort([selectedPos(1),clickPos(1)]);                    condition1=sum(picMat(selectedPos(1),tempVector1(1):tempVector1(2)))==0;                    condition2=sum(picMat(clickPos(1),tempVector2(1):tempVector2(2)))==0;                    condition3=sum(picMat(tempVector3(1):tempVector3(2),jj))==0;                    condition4=picMat(selectedPos(1),selectedPos(2))==picMat(clickPos(1),clickPos(2));                    if all([condition1,condition2,condition3,condition4])                        redLinePnts=[selectedPos;tempNode1;tempNode2;clickPos];                        break                    end                end            end            %-------------------------------------------------------------------------------------------------            if ~isempty(redLinePnts)                redLinePntsX=(redLinePnts(:,1)-1).*100+(redLinePnts(:,1)-1).*5+50;                redLinePntsY=(redLinePnts(:,2)-1).*100+(redLinePnts(:,2)-1).*5+50;                RedLine=plot(redLinePntsX,redLinePntsY,'Color',[1 0 0],'LineWidth',2.5);                pause(0.3)                delete(RedLine)                picMat(selectedPos(1),selectedPos(2))=0;                picMat(clickPos(1),clickPos(2))=0;                set(drawPicHdl(selectedPos(1),selectedPos(2)),'CData',ones(100,100,3).*0.95);                set(drawPicHdl(clickPos(1),clickPos(2)),'CData',ones(100,100,3).*0.95);                selectedPos=[];            else                selectedPos=clickPos;            end            end     endend

到此,关于“怎么用Matlab编写简易版连连看小游戏”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯