matlab GUI课程设计——扫雷游戏


课程设计

平台:matlab GUIDE

功能实现:扫雷游戏

目的:

1.熟悉matlab基本的代码编写能力;
2.学会利用matlab GUI设计图形交互界面,方便使用者操作;

演示:

1.创建15行×20列的扫雷界面,界面上包括棋子数量统计和炸弹数量设置.炸弹数量越多,扫雷难度越大.
matlab GUI-扫雷游戏
2. 正常扫雷演示:
matlab GUI-扫雷游戏
3. 点击到炸弹后,弹出对话框,可选择关闭程序或者重新开始.
matlab GUI-扫雷游戏

代码示例:

每个按钮被点击后执行的回调函数:

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);

欢迎交流

代码事宜,私信博主

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值