这是一款MATLAB连连看小游戏,基本实现了连连看的功能,不过目前没有写出死局判定的模块,日后可能会对其进行改进。 游戏截图 游戏组装说明 我们的变量path输入的是图像包的相
这是一款MATLAB连连看小游戏,基本实现了连连看的功能,不过目前没有写出死局判定的模块,日后可能会对其进行改进。
游戏截图
游戏组装说明
我们的变量path输入的是图像包的相对路径或绝对路径
这里我们由于文件夹名称就叫“图片”所以直接path='图片’是没啥问题的。
文件夹里就是图片素材啦,其名称没有任何要求,形状最好是方的,不过不是的话问题也不大,程序会自动将其拉伸成方的。。。。。。。。
我们这里要求的是读入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 clickPos
global redLinePnts
global picMat
selectedPos=[];
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;
end
createMat()
%主要框架生成==============================================================
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',[]);
uh1=uimenu('label','帮助');
uimenu(uh1,'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);
end
end
%重新开始函数==============================================================
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
end
end
到此这篇关于利用Matlab编写简易版连连看小游戏的文章就介绍到这了,更多相关Matlab连连看游戏内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 利用Matlab编写简易版连连看小游戏
本文链接: https://lsjlt.com/news/141285.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0