课程设计
平台:matlab GUIDE
功能实现:扫雷游戏
目的:
1.熟悉matlab基本的代码编写能力;
2.学会利用matlab GUI设计图形交互界面,方便使用者操作;
演示:
1.创建15行×20列的扫雷界面,界面上包括棋子数量统计和炸弹数量设置.炸弹数量越多,扫雷难度越大.
2. 正常扫雷演示:
3. 点击到炸弹后,弹出对话框,可选择关闭程序或者重新开始.
代码示例:
每个按钮被点击后执行的回调函数:文章来源:https://www.toymoban.com/news/detail-521062.html
function pushcallback(hObject, eventdata, handles)
global row col mines times flags mine control around Tags Bombs
a = get(hObject,'position');
hang=double((a(2)-0.86)/(-0.054))-0.0001;
lie=double((a(1)-0.013)/0.045)-0.0001;
place=ceil([hang,lie]);
if times==1
mines=str2num(get(handles.edit1,'string'));
set(handles.edit1,'enable','off')
first_time(hObject, eventdata, handles,place)
times=0;
end
%如果点到了炸弹
if mine(place(1),place(2))==1&&~isequal(get(hObject,'CData'),Tags)
[p,q]=find(mine==1);
bombs=p+row*(q-1);
set(handles.h(bombs),'CData',Bombs,'ForegroundColor','k','backgroundcolor',0.85*[1,1,1]);
set(hObject,'CData',imread('爆炸圆.png'),'ForegroundColor','k','backgroundcolor',0.85*[1,1,1]);
buttonName2=questdlg('很遗憾,你输了','游戏结束','close','restart','close');
if isempty(buttonName2)
close;
return
end
if strcmp(buttonName2,'restart')
set(handles.edit1,'enable','on')
set(handles.text1,'string','标记:')
mines=35;%炸弹总数
times=1;
flags=0;
for m=1:row
for n=1:col
set(handles.h(m,n),'style','pushbutton','foregroundColor',0.7*[1 1 1],...
'BackgroundColor',0.7*[1 1 1],'CData',[]);
end
end
mine=zeros(row,col);
control=ones(row,col);
around=zeros(row,col);
return
else
if strcmp(buttonName2,'close')
close;
return
end
end
end
%如果点到的不是炸弹,但是其九宫格内有炸弹的话,只翻当前点击的该格子
if (mine(place(1),place(2))==0)&&(around(place(1),place(2))~=0)&&~isequal(get(hObject,'CData'),Tags)
drawnum(hObject, eventdata, handles,place)
end
%如果点到的不是炸弹,而且其九宫格内也没有炸弹的话,就随机扩展当前点击的格子周边的连接的不是炸弹的多个格子
if (mine(place(1),place(2))==0)&&(around(place(1),place(2))==0)&&~isequal(get(hObject,'CData'),Tags)
begins=place;
[whitea,whiteb]=find(around==0);
white=[whitea,whiteb];
next=[begins;begins+[1,0];begins+[-1,0];begins+[0,1];begins+[0,-1]];
while ~isempty(intersect(white,next,'rows'))
[a,b,~]=intersect(white,next,'rows');
begins=[a;begins];
white(b,:)=[];
ad=length(sum(begins,2));
next=[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]];
end
drawbegins=begins(:,1)+row*(begins(:,2)-ones(ad,1));
set(handles.h(drawbegins),'style','text','string','','backgroundcolor',0.85*[1,1,1]);
control(drawbegins)=0;
colors=[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]];
colors=unique(colors,'rows');
[txa,txb]=find(around~=0);
tx=[txa,txb];
txcolors=intersect(tx,colors,'rows');
for i=1:length(sum(txcolors,2))
drawnum(handles.h(txcolors(i,1),txcolors(i,2)),eventdata, handles,txcolors(i,1:2))
end
end
guidata(hObject, handles);
欢迎交流
代码事宜,私信博主文章来源地址https://www.toymoban.com/news/detail-521062.html
到了这里,关于matlab GUI课程设计——扫雷游戏的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!