⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 untitled.m

📁 推箱子游戏,这是用matlab语言编写的源程序
💻 M
字号:
% 用matlab写的推箱子游戏:第1关
close all;clc;clear;
figure('NumberTitle','off','Name','欢迎光临推箱子游戏:第1关');
% \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)]});
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:8;
        h(m,n)=uicontrol(gcf,'style','text',...
            'unit','normalized','position',[0.2+0.06*n,0.9-0.08*m,0.05,0.07],...
            'BackgroundColor',0.7*[1,1,1],'fontsize',16,...
            'fontname','times new roman');
    end
end
p1m=4;p1n=2;
p2m=6;p2n=2;
p3m=7;p3n=3; % the position of aims
mt=3;nt=6;   % the position of hand
b1m=4;b1n=5;
b2m=4;b2n=6;
b3m=6;b3n=5; % the position of boxes
wm=[2,2,3,3,4,4,5,5,5,5,5,6,6,7,7];
wn=[5,6,5,7,3,7,2,3,4,5,6,3,4,4,5];
S=zeros(8);
S(p1m,p1n)=1;
S(p2m,p2n)=1;
S(p3m,p3n)=1;
S(mt,nt)=1;
S(b1m,b1n)=2;
S(b2m,b2n)=2;
S(b3m,b3n)=2;
for kk=1:length(wm);
    S(wm(kk),wn(kk))=1;
end
set(h(S>0),'BackgroundColor','w');
set([h(p1m,p1n),h(p2m,p2n),h(p3m,p3n)],'string','p');
set([h(b1m,b1n),h(b2m,b2n),h(b3m,b3n)],'string','b','style','push');
set(h(mt,nt),'string','h','style','push');

Pz=[p1m+p1n*i,p2m+p2n*i,p3m+p3n*i];
Fen=[0,0,0];
set(gcf,'KeyPressFcn',['K=get(gcf,''CurrentKey'');',...
        'if K(1)==116;push_box1;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;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'');',...
        '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 sum(Fen)==3;set(Gx,''string'',''恭喜你通过第1关'');end;clc;']);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -