首页
学习
活动
专区
圈层
工具
发布

MATLAB自动扫雷(3)——整体运行

整体运行扫雷

close

clear

clc

global gameover

global rect_in

global map

global blocks_x

global blocks_y

global block_width

global block_height

global left

global top

%% 扫雷游戏窗口

class_name = "TMain";

title_name = "Minesweeper Arbiter ";

rect = win.getWindowRect(class_name, title_name);

% 窗口坐标

left = rect(1);

top = rect(2);

right = rect(3)+rect(1);

bottom = rect(4)+rect(2);

if rect

disp("找到窗口")

disp("窗口坐标:")

disp([num2str(left) , ' ' , num2str(right) , ' ' ,...

num2str(top) , ' ' , num2str(bottom)])

else

disp("未找到窗口")

end

%% 锁定雷区坐标

left = left + 15;

top = top + 101;

right = right - 15;

bottom = bottom - 43;

% 抓取雷区图像

rect_in = [left, top, right-left, bottom-top];

img = win.screenCapture(rect_in);

% img = img/255;

% 每个方块16*16

block_width = 16;

block_height = 16;

% 横向有blocks_x个方块

blocks_x = floor((right - left) / block_width);

% 纵向有blocks_y个方块

blocks_y = floor((bottom - top) / block_height);

map = zeros(blocks_x,blocks_y);

% 是否游戏结束的标记

gameover = 0;

%%

win.setMouse(left+5, top+5);

win.leftClick;%开局点一下

showmap();

start = datetime('now');

%设定一个时间、如果一直没成功就自然结束

while (datetime('now')-start<duration(0,0,600))

if (gameover == 0)

banner();

banner();

click_blank();

else

gameover = 0;%点到雷重来

win.setMouse(left+5, top+5);

win.leftClick;

win.key(113)

win.leftClick;

showmap();

end

if all(all(map~=0)) && gameover==0

break%成功扫雷

end

end

下一篇
举报
领券