課程設(shè)計(jì)
平臺(tái):matlab GUIDE
功能實(shí)現(xiàn):掃雷游戲
目的:
1.熟悉matlab基本的代碼編寫能力;
2.學(xué)會(huì)利用matlab GUI設(shè)計(jì)圖形交互界面,方便使用者操作;
演示:
1.創(chuàng)建15行×20列的掃雷界面,界面上包括棋子數(shù)量統(tǒng)計(jì)和炸彈數(shù)量設(shè)置.炸彈數(shù)量越多,掃雷難度越大.
2. 正常掃雷演示:
3. 點(diǎn)擊到炸彈后,彈出對(duì)話框,可選擇關(guān)閉程序或者重新開始.
代碼示例:
每個(gè)按鈕被點(diǎn)擊后執(zhí)行的回調(diào)函數(shù):文章來源:http://www.zghlxwxcb.cn/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
%如果點(diǎn)到了炸彈
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('很遺憾,你輸了','游戲結(jié)束','close','restart','close');
if isempty(buttonName2)
close;
return
end
if strcmp(buttonName2,'restart')
set(handles.edit1,'enable','on')
set(handles.text1,'string','標(biāo)記:')
mines=35;%炸彈總數(shù)
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
%如果點(diǎn)到的不是炸彈,但是其九宮格內(nèi)有炸彈的話,只翻當(dāng)前點(diǎn)擊的該格子
if (mine(place(1),place(2))==0)&&(around(place(1),place(2))~=0)&&~isequal(get(hObject,'CData'),Tags)
drawnum(hObject, eventdata, handles,place)
end
%如果點(diǎn)到的不是炸彈,而且其九宮格內(nèi)也沒有炸彈的話,就隨機(jī)擴(kuò)展當(dāng)前點(diǎn)擊的格子周邊的連接的不是炸彈的多個(gè)格子
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);
歡迎交流
代碼事宜,私信博主文章來源地址http://www.zghlxwxcb.cn/news/detail-521062.html
到了這里,關(guān)于matlab GUI課程設(shè)計(jì)——掃雷游戲的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!