MATLAB制作GUI—扫雷游戏的实现(完整版)

这几天做实验好累,今天终于有时间来继续做自己的扫雷小游戏了,经过一番调试,终于完成了自己地扫雷小游戏。

下面是扫雷小游戏的最终版:

这是我自己实现的第一个小游戏,在整个过程中,遇到了一些问题,不过最终还是解决了,自己也感觉收获满满 。下面我将自己实现的代码贴出来供大家学习:

主函数:

function SaoLeiGaming(tmp)

%This is the SaoLei game,that we used to played in our childhood.
%Author:等等登登-ande
%Email:[email protected]

global NumButton Plusboom HBOX data

if nargin==0
    tmp = 'inital';
    NumButton = 6;
end
switch(tmp)
    case 'inital'
        fullsizescreen = get(0,'ScreenSize');%获取屏幕的大小以及右下角座标
        H = figure('name','扫雷_zd',...
            'position',[fullsizescreen(3)/3,fullsizescreen(4)/3,80+35*NumButton,80+35*NumButton],...
            'Menubar','none');%创建函数句柄H
        menu1 = uimenu(H,'Text','&Game');%在H函数句柄上创建菜单Game
        uimenu(menu1,'Text','&NewGame','Accelerator','M',...
            'CallBack',[mfilename,'(''quit'');',mfilename]);%在菜单Game下创建子菜单NewGame
        uimenu(menu1,'Text','&Quit','Accelerator','Q',...
            'CallBack',[mfilename,'(''quit'')']);%在菜单Game下创建子菜单Quit
        HBOX = zeros(NumButton);
        for i = 1:NumButton
            for j = 1:NumButton
                HBOX(i,j) = uicontrol(H,'Style','pushbutton',...
                    'FontWeight','bold','FontSize',10,...
                    'position',[40+35*(i-1),40+35*(NumButton-j),35,35],...
                    'tag',num2str([j,i]),...
                    'tooltipstring','This is not boom!',...
                    'ButtondownFcn','RightKey',...
                    'CallBack','LeftKey');%创建NumBotton*NumBotton个格子
            end 
        end
    case 'quit'
        closereq
end
%设置炸弹
boom = zeros(NumButton);
%每行随机设置一个炸弹
for i = 1:NumButton
    boom(randi(NumButton),i)=1;
end 
%对数据进行扩展
Plusboom = zeros(NumButton+2,NumButton+2);
Plusboom(2:end-1,2:end-1) = boom;

左键触发函数:

function LeftKey
%左键触发函数

global Plusboom HBOX NumButton cdata

cdata = readdata;
[labelx,labely] = find(Plusboom == 1);
pos = get(gco,'tag');
posx = str2double(pos(1))+1;
posy = str2double(pos(4))+1;
All_posx = [posx-1,posx,posx+1];
All_posy = [posy-1,posy,posy+1];

label = [labelx,labely];
length_label = length(label);
pox = [posx,posy];
Judge = ismember(label,pox,'row');
Judge_one = find(Judge==1);
if length(Judge_one)==0
    All_boom=sum(sum(Plusboom(All_posx,All_posy)));%注意这里的x,y代表的行列,容易搞错。
    if All_boom == 0
    [pox1 poy1]=findzeros(posy-1,posx-1);
    for j=1:length(pox1-1)
        set(HBOX(poy1(j),pox1(j)),'string','.','Fontsize',15,'foregroundcolor','b');
    end
    elseif All_boom == 1
        set(gco,'String',num2str(All_boom),'FontSize',15,'foregroundcolor','r');
    elseif All_boom == 2
        set(gco,'string',num2str(All_boom),'FontSize',15,'foregroundcolor','b');
    elseif All_boom == 3
        set(gco,'string',num2str(All_boom),'Fontsize',15,'foregroundcolor','k');
    else
        set(gco,'string',num2str(All_boom),'Fontsize',15,'foregroundcolor','g');
    end
else
    for i = 1:NumButton
    set(HBOX (labely(i)-1,labelx(i)-1), 'cdata' ,cdata);
    end
end

右键触发函数:

function RightKey
%右键触发函数
content = get(gco,'String');
if(isempty(content))
    set(gco,'String','L');
elseif(isequal(content,'L'))
    set(gco,'String','?');
elseif(isequal(content,'?'))
    set(gco,'String','');
end

寻零函数:

function [pox poy]=findzeros(x,y)

%当格子周围地雷为0时,寻找周围同样地雷数为0的格子
global Plusboom
Kernel = ones(3);
boom = Plusboom(2:end-1,2:end-1);
map = conv2(boom,Kernel);
Fmap = map(2:end-1,2:end-1);

Boundariy = bwboundaries(~Fmap,4);
label = 0;
length1 = length(Boundariy);
for i=1:length1
    tmp = Boundariy{i};
    for j=1:length(tmp)
        if (tmp(j,1)==y && tmp(j,2)==x)
            label = i;
            break
        end
    end
end
if label~=0
    P = Boundariy{label};
    pox = P(:,1);
    poy = P(:,2);
else
    pox = [];
    poy = [];
end

数据读取函数:

function cdata = readdata
%读取地雷图片
pic = imread('地雷.jpg');
cdata = imresize(pic,0.03);

希望大家也可以实现自己的扫雷小游戏~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章