利用Matlab复刻扫雷小游戏
效果图
文章图片
点击帮助切换插旗功能:
文章图片
文章图片
游戏失败:
文章图片
完整代码
function SLsXpbombsglobal Row; Row=10; %雷区行数global Col; Col=10; %雷区列数global MineMap; MineMap=[]; %地雷图global aroundMap; aroundMap=[]; %周围地雷数global MarkMap; MarkMap=ones([Row,Col]); %标记图global MineNum; MineNum=8; %地雷总数global FlagNum; FlagNum=0; %旗子总数global times; times=1; %挖雷次数%global Surplus; Surplus=MineNum; %剩余地雷=地雷总数-旗子总数global tool; tool='扫雷'; global XPBlabel XPBimage scoreImage; XPBFig=uifigure('units','pixels',...'position',[500,200,260,310],...'Numbertitle','off',...'menubar','none',...'resize','off',...'name','扫雷'); XPBfaceIm=uiimage(XPBFig); XPBfaceIm.Position=[130-13,275,26,26]; XPBfaceIm.ImageSource='image\face1.png'; %'image\none.png'; XPBfaceIm.UserData=https://www.it610.com/article/0; set(XPBfaceIm,'ImageClickedFcn',@restart)uilabel(XPBFig,'Text','','BackgroundColor',[0 0 0],'Position',[20-1,270,66,40]); uilabel(XPBFig,'Text','','BackgroundColor',[0 0 0],'Position',[+260-40-44-1,270,66,40]); for i=1:3scoreImage(1,i)=uiimage(XPBFig,'ImageSource','image\0.png',...'Position',[20+(i-1)*22,270,20,40]); scoreImage(2,i)=uiimage(XPBFig,'ImageSource','image\0.png',...'Position',[(i-1)*22+260-40-44,270,20,40]); end; drawScore(MineNum,MarkMap)XPBMenu=uimenu(XPBFig); XPBMenu.Text='帮助'; XPBMenu_1=uimenu(XPBMenu); XPBMenu_1.Text='插旗'; set(XPBMenu_1,'MenuSelectedFcn',@MenuSelected)function MenuSelected(~,~)switch 1case strcmp(XPBMenu_1.Text,'插旗'),XPBMenu_1.Text='扫雷'; tool='插旗'; case strcmp(XPBMenu_1.Text,'扫雷'),XPBMenu_1.Text='插旗'; tool='扫雷'; endendfor m=1:Rowfor n=1:ColXPBlabel(m,n)=uilabel(XPBFig,'Text','','HorizontalAlignment','center',...'FontSize',16,'FontWeight','bold',...'BackgroundColor',0.85*[1,1,1],'Position',[10+1+(m-1)*24,10+1+(n-1)*26,24-2,26-2]); XPBimage(m,n)=uiimage(XPBFig,'ImageSource','image\button.png',...'Position',[10+(m-1)*24,10+(n-1)*26,24,26],'UserData',[m,n],'ImageClickedFcn',@clickButton); endend%==========================================================================function clickButton(obj,~)clickPos=obj.UserData; if strcmp(tool,'插旗')switch MarkMap(clickPos(1),clickPos(2))case 1,set(XPBimage(clickPos(1),clickPos(2)),'visible','on','ImageSource','image\flagbutton.png'); case -1,set(XPBimage(clickPos(1),clickPos(2)),'visible','on','ImageSource','image\button.png'); endMarkMap(clickPos(1),clickPos(2))=-MarkMap(clickPos(1),clickPos(2)); drawScore(MineNum,MarkMap)return; endif times==1,[MineMap,aroundMap]=createMap(clickPos); drawNum(aroundMap); times=inf; endif MarkMap(clickPos(1),clickPos(2))==-1,return; endset(XPBimage(clickPos(1),clickPos(2)),'visible','off'); MarkMap(clickPos(1),clickPos(2))=0; switch MineMap(clickPos(1),clickPos(2))case 1,gameOver(MineMap,clickPos); case 0,searchZone(aroundMap,clickPos); enddrawScore(MineNum,MarkMap)if all(all(abs(MarkMap)==MineMap))win()endend%==========================================================================function drawScore(MineNum,MarkMap)MarkMap(MarkMap==-1)=0; score1=num2str(MineNum); L1=length(score1); score2=num2str(sum(sum(MarkMap))); L2=length(score2); for ii=1:3if ii<=3-L1set(scoreImage(1,ii),'ImageSource','image\0.png')elsetempStr=score1(ii-(3-L1)); set(scoreImage(1,ii),'ImageSource',['image\',tempStr,'.png']); endif ii<=3-L2set(scoreImage(2,ii),'ImageSource','image\0.png')elsetempStr=score2(ii-(3-L2)); set(scoreImage(2,ii),'ImageSource',['image\',tempStr,'.png']); endendendfunction win(~,~)for mm=1:Rowfor nn=1:Colif MineMap(mm,nn)==1set(XPBimage(mm,nn),'visible','on','ImageSource','image\flagbutton.png'); endendendXPBfaceIm.ImageSource='image\face3.png'; endfunction gameOver(MineMap,pos)for mm=1:Rowfor nn=1:Colset(XPBimage(mm,nn),'visible','off'); if MineMap(mm,nn)==1set(XPBimage(mm,nn),'visible','on','ImageSource','image\mine.png'); endendendset(XPBlabel(pos(1),pos(2)),'BackgroundColor',[1 0 0]); XPBfaceIm.ImageSource='image\face2.png'; endfunction searchZone(aroundMap,pos)if aroundMap(pos(1),pos(2))~=0,return; endbegins=pos; [nonea,noneb]=find(aroundMap==0); none=[nonea,noneb]; listZone=[begins; begins+[1,0]; begins+[-1,0]; begins+[0,1]; begins+[0,-1]; begins+[-1,1]; begins+[-1,-1]; begins+[1,1]; begins+[1,-1]]; while ~isempty(intersect(none,listZone,'rows'))[a,b,~]=intersect(none,listZone,'rows'); begins=[a; begins]; none(b,:)=[]; ad=length(sum(begins,2)); listZone=[begins; begins+ones(ad,1)*[1,0]; begins+ones(ad,1)*[-1,0]; begins+ones(ad,1)*[0,1]; begins+ones(ad,1)*[0,-1]; begins+ones(ad,1)*[-1,1]; begins+ones(ad,1)*[-1,-1]; begins+ones(ad,1)*[1,1]; begins+ones(ad,1)*[1,-1]]; listZone=unique(listZone,'rows'); endlistZone(sum(listZone<1,2)>0,:)=[]; listZone(sum(listZone>10,2)>0,:)=[]; listZone=round(listZone); for ii=1:size(listZone,1)set(XPBimage(listZone(ii,1),listZone(ii,2)),'visible','off')MarkMap(listZone(ii,1),listZone(ii,2))=0; endendfunction restart(~,~)MineMap=[]; aroundMap=[]; MarkMap=ones([Row,Col]); times=1; tool='扫雷'; for mm=1:Rowfor nn=1:Colset(XPBlabel(mm,nn),'Text','','BackgroundColor',0.85*[1,1,1])set(XPBimage(mm,nn),'ImageSource','image\button.png','visible','on'); endenddrawScore(MineNum,MarkMap)XPBfaceIm.ImageSource='image\face1.png'; end%==========================================================================function drawNum(aroundMap)for mm=1:Rowfor nn=1:Colswitch aroundMap(mm,nn)case 0case 1,set(XPBlabel(mm,nn),'Text','1','FontColor',[0 0 1])case 2,set(XPBlabel(mm,nn),'Text','2','FontColor',[0,0.7,0])case 3,set(XPBlabel(mm,nn),'Text','3','FontColor',[0.8,0,0])case 4,set(XPBlabel(mm,nn),'Text','4','FontColor',[0,0,0.6])case 5,set(XPBlabel(mm,nn),'Text','5','FontColor',[0.5,0,0])case 6,set(XPBlabel(mm,nn),'Text','6','FontColor',[0,0.6,0])case 7,set(XPBlabel(mm,nn),'Text','7','FontColor',[0.75,0,0]) case 8,set(XPBlabel(mm,nn),'Text','8','FontColor',[0.4,0,0])endendendend%第一次挖雷时生成地雷分布图(这样第一次不会碰到雷)function [randMap,surrMap]=createMap(pos)%生成地雷分布图randMap=rand([Row,Col]); randMap(pos(1),pos(2))=inf; [~,St]=sort(randMap(:)); randMap=(randMap<=randMap(St(MineNum))); %生成周围地雷数分布图frameMap=zeros([Row+2,Col+2]); xSet=2:Row+1; ySet=2:Col+1; frameMap(xSet,ySet)=randMap; surrMap(xSet-1,ySet-1)=frameMap(xSet-1,ySet+1)+frameMap(xSet-1,ySet)+...frameMap(xSet-1,ySet-1)+frameMap(xSet,ySet+1)+...frameMap(xSet,ySet-1)+frameMap(xSet+1,ySet+1)+...frameMap(xSet+1,ySet)+frameMap(xSet+1,ySet-1); surrMap(randMap==1)=0; end%==========================================================================end
【利用Matlab复刻扫雷小游戏】以上就是利用Matlab复刻扫雷小游戏的详细内容,更多关于Matlab扫雷游戏的资料请关注脚本之家其它相关文章!
推荐阅读
- 智能算法|MATLAB-网页访问与关闭
- #MATLAB|MATLAB(2)--MATLAB矩阵的表示
- matlab|matlab报错索引超出数组范围
- [scp] 利用ssh进行文件互传
- 利用Kubernetes实现容器的持久化存储
- 基于Matlab制作一款简单的龙舟小游戏
- 教你用Matlab制作黄金矿工小游戏
- 如何利用Matlab绘制出好看的火山图
- 神经网络学习小记录|神经网络学习小记录62——Tensorflow2 利用efficientnet系列模型搭建yolov3目标检测平台
- excite与matlab联合仿真|excite与matlab联合仿真,只有你想不到,没有EXCITE做不到的瞬态工况模拟