📄 untitled2.m
字号:
% 用matlab写的推箱子游戏:第8关
close all;clc;clear;
figure('NumberTitle','off','Name','欢迎光临推箱子游戏:第8关');
% \copyright: zjliu
% Author's email: zjliu2001@163.com
uicontrol(gcf,'style','text',...
'unit','normalized','position',[0.09,0.02,0.82,0.18],...
'BackgroundColor',0.7*[1,1,1],'ForegroundColor',[0.1,0.1,0.9],...
'fontsize',12,'fontname','times new roman','string',...
{'规则很简单,只需把所有的''b''都推到''p''上面去(会变成绿色)',...
'就过关了。但是玩起来难度可是相当大的,不要轻视喔。 ',...
'h是代表推箱子的手,请使用方向键来控制手h的移动。按t',...
['键或者tab键重开。',repmat(' ',1,87)]});
uicontrol(gcf,'style','text',...
'unit','normalized','position',[0.85,0.85,0.1,0.08],...
'BackgroundColor',0.8*[1,1,1],'ForegroundColor',[0.8,0.1,0.1],...
'fontsize',12,'fontname','times new roman','string','步数:');
Bs=uicontrol(gcf,'style','text',...
'unit','normalized','position',[0.9,0.75,0.1,0.08],...
'BackgroundColor',0.8*[1,1,1],'ForegroundColor',[0.8,0.1,0.1],...
'fontsize',12,'fontname','times new roman','string','0',...
'HorizontalAlignment','left');
Gx=uicontrol(gcf,'style','text',...
'unit','normalized','position',[0.3,0.85,0.3,0.1],...
'BackgroundColor',0.8*[1,1,1],'ForegroundColor',[0.8,0.1,0.1],...
'fontsize',12,'fontname','times new roman');
for m=1:8;
for n=1:11;
h(m,n)=uicontrol(gcf,'style','text',...
'unit','normalized','position',[0.12+0.06*n,0.9-0.08*m,0.05,0.07],...
'BackgroundColor',0.5*[1,1,1],'fontsize',16,...
'fontname','times new roman');
end
end
S=zeros(8,11);
p1m=4;p1n=3;
p2m=4;p2n=6;
p3m=5;p3n=6;
p4m=5;p4n=9; % the position of aims
mt=2;nt=2; % the position of hand
b1m=4;b1n=5;
b2m=4;b2n=7;
b3m=5;b3n=5;
b4m=5;b4n=7; % the position of boxes
wm=[2*ones(1,8),3,3,3,[4,4,4,4],[5,5,5,5]];
wn=[3:10,[2,7,10],[2,4,8,10],[2,4,8,10]];
wm=[wm,[6,6,6],7*ones(1,9)];
wn=[wn,[2,5,10],2:10];
S(p1m,p1n)=1;
S(p2m,p2n)=1;
S(p3m,p3n)=1;
S(p4m,p4n)=1;
S(mt,nt)=1;
S(b1m,b1n)=2;
S(b2m,b2n)=2;
S(b3m,b3n)=2;
S(b4m,b4n)=2;
for kk=1:length(wm);
S(wm(kk),wn(kk))=1;
end
set(h(S<0),'BackgroundColor',0.8*[1,1,1]);
set(h(S>0),'BackgroundColor','w');
set([h(p1m,p1n),h(p2m,p2n),h(p3m,p3n),h(p4m,p4n)],'string','p');
set([h(b1m,b1n),h(b2m,b2n),h(b3m,b3n),h(b4m,b4n)],'string','b','style','push');
set(h(mt,nt),'string','h','style','push');
Pz=[p1m+p1n*i,p2m+p2n*i,p3m+p3n*i,p4m+p4n*i];
Fen=[0,0,0,0];Bsn=0;
set(gcf,'KeyPressFcn',['K=get(gcf,''CurrentKey'');',...
'if K(1)==116;push_box8;end;',...
'if K(1)==117;dm=-1;dn= 0;end;',...
'if K(1)==100;dm= 1;dn= 0;end;',...
'if K(1)==108;dm= 0;dn=-1;end;',...
'if K(1)==114;dm= 0;dn= 1;end;',...
'mtn=mt+dm;ntn=nt+dn;',...
'if S(mtn,ntn)==1;',...
'set(h(mt,nt),''style'',''text'',''string'','''');',...
'if min(abs(mt+nt*i-Pz))<0.5;',...
'set(h(mt,nt),''String'',''p'');end;',...
'set(h(mtn,ntn),''style'',''push'',''string'',''h'');',...
'mt=mtn;nt=ntn;Bsn=Bsn+1;end;',...
'if S(mtn,ntn)==2&S(mtn+dm,ntn+dn)==1;',...
'set(h(mt,nt),''style'',''text'',''string'','''');',...
'set(h(mtn,ntn),''style'',''push'',''string'',''h'');',...
'set(h(mtn+dm,ntn+dn),''style'',''push'',''string'',''b'');',...
'Bsn=Bsn+1;if min(abs(mt+nt*i-Pz))<0.5;',...
'set(h(mt,nt),''string'',''p'');end;',...
'S(mtn+dm,ntn+dn)=2;S(mtn,ntn)=1;mt=mtn;nt=ntn;end;',...
'if S(p1m,p1n)==2;Fen(1)=1;set(h(p1m,p1n),''BackgroundColor'',''g'');',...
'else Fen(1)=0;set(h(p1m,p1n),''BackgroundColor'',''w'');end;',...
'if S(p2m,p2n)==2;Fen(2)=1;set(h(p2m,p2n),''BackgroundColor'',''g'');',...
'else Fen(2)=0;set(h(p2m,p2n),''BackgroundColor'',''w'');end;',...
'if S(p3m,p3n)==2;Fen(3)=1;set(h(p3m,p3n),''BackgroundColor'',''g'');',...
'else Fen(3)=0;set(h(p3m,p3n),''BackgroundColor'',''w'');end;',...
'if S(p4m,p4n)==2;Fen(4)=1;set(h(p4m,p4n),''BackgroundColor'',''g'');',...
'else Fen(4)=0;set(h(p4m,p4n),''BackgroundColor'',''w'');end;',...
'if sum(Fen)==4;set(Gx,''string'',''恭喜你通过第8关'');end;clc;',...
'set(Bs,''string'',num2str(Bsn));']);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -