MATLAB编写扫雷小游戏

本文介绍了如何使用MATLAB编写扫雷游戏,包括构建棋盘、初始化、左键和右键功能的实现,以及游戏的运行结果。通过循环输出创建棋盘,随机布雷,左键点击显示数字或结束游戏,右键点击标记雷区。游戏胜利条件是标记的雷数与实际雷数相同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编写程序在架构上参考了博主slandarer的文章matlab扫雷小游戏

一、构建棋盘

        首先在matlab中构建一个新的脚本文件,建立一个figure,可以简单的添加目录标题美化视图,可以用循环输出按钮的方式建立一个长20格,宽15格的扫雷棋盘。代码如下

 

clear 
global row col mines times number visit h flag F success f
f=0;
row=15;col=20;mines=45;
total=mines;times=1;
remine=row*col;
flag=0;
hf=figure('NumberTitle','off','Name','扫雷','menubar','none','position',[370,130,600,500]);
uh1=uimenu('label','帮助');
uimenu(uh1,'label','游戏规则','callback',['msgbox(''和windows自带的扫雷一个样,嗯哒'')'])
colormap([1 0 0;0 0 0;.65 .65 .65;1 1 1]);
axis off
hold on;
C=uicontrol(gcf,'style','text','unit','normalized',...
    'position',[0.45,0.86,0.09,0.078],'fontsize',17,...
    'BackgroundColor',0.85*[1 1 1],...
    'string','o');
H=uicontrol(gcf,'style','text','unit','normalized',...
    'position',[0.58,0.86,0.3,0.078],'fontsize',12,...
    'BackgroundColor',0.85*[1 1 1],...
   'string',['total:' num2str(total)]);
F=uicontrol(gcf,'style','text','unit','normalized',...
    'position',[0.1,0.86,0.3,0.078],'fontsize',12,...
    'BackgroundColor',0.85*[1 1 1],...
   'string',['flag:' num2str(flag)]);
for m=1:row 
    for n=1:col
        h(m,n)=uicontrol(gcf,'style','push',...
            'foregroundColor',0.7*[1 1 1],...
            'BackgroundColor',0.7*[1 1 1],...
            'fontsize',15,'fontname','time new roman',...
            'Unit','normalized','position',[0.0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值