第一次发现 原来可以用matlab做个小游戏,借用了一个华容道的模板,希望各位大佬给小弟点意见。如有错误的地方,见谅。各位大佬有没有很好的游戏推荐?
function huarongdao()
A = [2 1 1 3;
2 1 1 3;
4 6 6 5;
4 7 7 5;
7 0 0 7];
drawmap(A)
while 1
if A(5,2)==1&A(5,3)==1
ch = menu('曹操成功逃出华容道!如果要继续玩,按“是”,否则按“否”','是','否');
switch ch
case 1
huarongdao();
case 2
return
end
end
[xpos,ypos] = ginput(1);
col = ceil(xpos);
row = 5-ceil(ypos)+1;
juese = A(row,col);
switch juese
case 1%点击了曹操
[I,J] = find(A==1);
rm = max(I);
rn = min(I);
lm = max(J);
ln = min(J);
%判断是否能向左移
if ln>1&isequalm(A([rn,rm],ln-1),[0;0])
A([rn,rm],ln-1)=[1;1];
A([rn,rm],lm)=[0;0];
drawmap(A)
end
%判断是否能向右移
if lm<4&isequalm(A([rn,rm],lm+1),[0;0])
A([rn,rm],lm+1)=[1;1];
A([rn,rm],ln)=[0;0];
drawmap(A)
end
%判断是否能向下移
if rn>1&isequalm(A(rn-1,[ln,lm]),[0,0])
A(rn-1,[ln,lm])=[1,1];
A(rn+1,[ln,lm])=[0,0];
drawmap(A)
end
%判断是否能向上移
if rm<5&isequalm(A(rm+1,[ln,lm]),[0,0])
A(rm+1,[ln,lm])=[1,1];
A(rm-1,[ln,lm])=[0,0];
drawmap(A)
end
case 2% 点击了黄忠
[I,J] = find(A==2);
rm = max(I);
rn = min(I);
lm = max(J);
ln = min(J);
%判断是否能向左移
if ln>1&isequalm(A([rn,rm],ln-1),[0;0])
A([rn,rm],ln-1)=[2;2];
A([rn,rm],lm)=[0;0];
drawmap(A)
end
%判断是否能向右移
if lm<4&isequalm(A([rn,rm],lm+1),[0;0])
A([rn,rm],lm+1)=[2;2];
A([rn,rm],ln)=[0;0];
drawmap(A)
end
if rn>1&A(rn-1,ln)==0
if rm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch = menu('请选择移到的方向:','上','下')
switch ch
case 1%上移
A(rn-1,ln) = 2;
A(rn+1,ln) = 0;
drawmap(A)
case 2%下移
A(rm+1,ln) = 2;
A(rm-1,ln) = 0;
drawmap(A)
end
else%只能上移
A(rn-1,ln) = 2;
A(rn+1,ln) = 0;
drawmap(A)
end
elseif rm<5&A(rm+1,ln)==0
A(rm+1,ln) = 2;
A(rm-1,ln) = 0;
drawmap(A)
end
case 3%张飞
[I,J] = find(A==3);
rm = max(I);
rn = min(I);
lm = max(J);
ln = min(J);
%判断是否能向左移
if ln>1&isequalm(A([rn,rm],ln-1),[0;0])
A([rn,rm],ln-1)=[3;3];
A([rn,rm],lm)=[0;0];
drawmap(A)
end
%判断是否能向右移
if lm<4&isequalm(A([rn,rm],lm+1),[0;0])
A([rn,rm],lm+1)=[3;3];
A([rn,rm],ln)=[0;0];
drawmap(A)
end
if rn>1&A(rn-1,ln)==0
if rm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch = menu('请选择移到的方向:','上','下')
switch ch
case 1%上移
A(rn-1,ln) = 3;
A(rn+1,ln) = 0;
drawmap(A)
case 2%下移
A(rm+1,ln) = 3;
A(rm-1,ln) = 0;
drawmap(A)
end
else%只能上移
A(rn-1,ln) = 3;
A(rn+1,ln) = 0;
drawmap(A)
end
elseif rm<5&A(rm+1,ln)==0
A(rm+1,ln) = 3;
A(rm-1,ln) = 0;
drawmap(A)
end
case 4%马超
[I,J] = find(A==4);
rm = max(I);
rn = min(I);
lm = max(J);
ln = min(J);
%判断是否能向左移
if ln>1&isequalm(A([rn,rm],ln-1),[0;0])
A([rn,rm],ln-1)=[4;4];
A([rn,rm],lm)=[0;0];
drawmap(A)
end
%判断是否能向右移
if lm<4&isequalm(A([rn,rm],lm+1),[0;0])
A([rn,rm],lm+1)=[4;4];
A([rn,rm],ln)=[0;0];
drawmap(A)
end
if rn>1&A(rn-1,ln)==0
if rm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch = menu('请选择移到的方向:','上','下')
switch ch
case 1%上移
A(rn-1,ln) = 4;
A(rn+1,ln) = 0;
drawmap(A)
case 2%下移
A(rm+1,ln) = 4;
A(rm-1,ln) = 0;
drawmap(A)
end
else%只能上移
A(rn-1,ln) = 4;
A(rn+1,ln) = 0;
drawmap(A)
end
elseif rm<5&A(rm+1,ln)==0
A(rm+1,ln) = 4;
A(rm-1,ln) = 0;
drawmap(A)
end
case 5%赵云
[I,J] = find(A==5);
rm = max(I);
rn = min(I);
lm = max(J);
ln = min(J);
%判断是否能向左移
if ln>1&isequalm(A([rn,rm],ln-1),[0;0])
A([rn,rm],ln-1)=[5;5];
A([rn,rm],lm)=[0;0];
drawmap(A)
end
%判断是否能向右移
if lm<4&isequalm(A([rn,rm],lm+1),[0;0])
A([rn,rm],lm+1)=[5;5];
A([rn,rm],ln)=[0;0];
drawmap(A)
end
if rn>1&A(rn-1,ln)==0
if rm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch = menu('请选择移到的方向:','上','下')
switch ch
case 1%上移
A(rn-1,ln) = 5;
A(rn+1,ln) = 0;
drawmap(A)
case 2%下移
A(rm+1,ln) = 5;
A(rm-1,ln) = 0;
drawmap(A)
end
else%只能上移
A(rn-1,ln) = 5;
A(rn+1,ln) = 0;
drawmap(A)
end
elseif rm<5&A(rm+1,ln)==0
A(rm+1,ln) = 5;
A(rm-1,ln) = 0;
drawmap(A)
end
case 6%关羽
[I,J] = find(A==6);
rm = max(I);
rn = min(I);
lm = max(J);
ln = min(J);
%判断是否能向上移
if rn>1 & isequalm(A(rn-1,[ln,lm]),[0,0])
A(rn-1,[ln,lm])=[6,6];
A(rn,[ln,lm])=[0,0];
drawmap(A)
end
%判断是否能向下移
if rm<5&isequalm(A(rm+1,[ln,lm]),[0,0])
A(rm+1,[ln,lm])=[6,6];
A(rm,[ln,lm])=[0,0];
drawmap(A)
end
if ln>1&A(rn,ln-1)==0
if lm<4&A(rm,lm+1)==0%如果又能左移又能右移,则要点击的部位
ch = menu('请选择移到的方向:','左','右')
switch ch
case 1%左移
A(rm,ln-1) = 6;
A(rm,ln+1) = 0;
drawmap(A)
case 2%右移
A(rm,lm+1) = 6;
A(rm,lm-1) = 0;
drawmap(A)
end
else%只能左移
A(rm,ln-1) = 6;
A(rm,ln+1) = 0;
drawmap(A)
end
elseif lm<4&A(rm,lm+1)==0
A(rm,lm+1) = 6;
A(rm,lm-1) = 0;
drawmap(A)
end
case 7 %小卒
if row>1&A(row-1,col)==0 % 上
if col>1&A(row,col-1)==0 % 左
ch = menu('请选择移到的方向:','上','左')
switch ch
case 1
A(row-1,col) = 7;
A(row,col) = 0;
drawmap(A)
case 2
A(row,col-1) = 7;
A(row,col) = 0;
drawmap(A)
end
elseif row<5&A(row+1,col)==0% 下
ch = menu('请选择移到的方向:','上','下')
switch ch
case 1
A(row-1,col) = 7;
A(row,col) = 0;
drawmap(A)
case 2
A(row+1,col) = 7;
A(row,col) = 0;
drawmap(A)
end
elseif col<4&A(row,col+1)==0 %右
ch = menu('请选择移到的方向:','上','右')
switch ch
case 1
A(row-1,col) = 7;
A(row,col) = 0;
drawmap(A)
case 2
A(row,col+1) = 7;
A(row,col) = 0;
drawmap(A)
end
else %只能向上
A(row-1,col) = 7;
A(row,col) = 0;
drawmap(A)
end
elseif col>1&A(row,col-1)==0%左
if row<5&A(row+1,col)==0%下
ch = menu('请选择移到的方向:','左','下')
switch ch
case 1
A(row,col-1) = 7;
A(row,col) = 0;
drawmap(A)
case 2
A(row+1,col) = 7;
A(row,col) = 0;
drawmap(A)
end
elseif col<4&A(row,col+1)==0%右
ch = menu('请选择移到的方向:','左','右')
switch ch
case 1
A(row,col-1) = 7;
A(row,col) = 0;
drawmap(A)
case 2
A(row,col+1) = 7;
A(row,col) = 0;
drawmap(A)
end
else%只能向左
A(row,col-1) = 7;
A(row,col) = 0;
drawmap(A)
end
elseif row<5&A(row+1,col)==0%下
if col<4&A(row,col+1)==0%右
ch = menu('请选择移到的方向:','下','右')
switch ch
case 1
A(row+1,col) = 7;
A(row,col) = 0;
drawmap(A)
case 2
A(row,col+1) = 7;
A(row,col) = 0;
drawmap(A)
end
else%只能向下
A(row+1,col) = 7;
A(row,col) = 0;
drawmap(A)
end
elseif col<4&A(row,col+1)==0%只能向右
A(row,col+1) = 7;
A(row,col) = 0;
drawmap(A)
end
end
end
function drawmap(A)
clf
hold on
%曹操
[I J] = find(A==1);
x1 = min(J)-1;
x2 = max(J);
y1 = 5-(min(I)-1);
y2 = 5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'r')
text(0.5*(x1+x2)-0.5,0.5*(y1+y2),'曹操','fontsize',28)
% 黄忠
[I,J] = find(A==2);
x1 = min(J)-1;
x2 = max(J);
y1 = 5-(min(I)-1);
y2 = 5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1),'黄','fontsize',28)
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2),'忠','fontsize',28)
% 张飞
[I,J] = find(A==3);
x1 = min(J)-1;
x2 = max(J);
y1 = 5-(min(I)-1);
y2 = 5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1),'张','fontsize',28)
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2),'飞','fontsize',28)
% 马超
[I,J] = find(A==4);
x1 = min(J)-1;
x2 = max(J);
y1 = 5-(min(I)-1);
y2 = 5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1),'马','fontsize',28)
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2),'超','fontsize',28)
% 赵云
[I,J] = find(A==5);
x1 = min(J)-1;
x2 = max(J);
y1 = 5-(min(I)-1);
y2 = 5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1),'赵','fontsize',28)
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2),'云','fontsize',28)
% 关羽
[I,J] = find(A==6);
x1 = min(J)-1;
x2 = max(J);
y1 = 5-(min(I)-1);
y2 = 5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+0.5*(x1+x2))-0.26,0.5*(y1+y2),'关','fontsize',28)
text(0.5*(0.5*(x1+x2)+x2)-0.26,0.5*(y1+y2),'羽','fontsize',28)
%小卒
[I,J] = find(A==7);
for i = 1:length(I)
x1 = J(i)-1;
x2 = J(i);
y1 = 5-(I(i)-1);
y2 = 5-I(i);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'g')
text(0.5*(x1+x2)-0.26,0.5*(y1+y2),'卒','fontsize',28)
end
% 画背景
line([0 4],[0 0],'color','b','linewidth',4)
line([0 4],[5 5],'color','b','linewidth',4)
line([0 0],[0 5],'color','b','linewidth',4)
line([4 4],[0 5],'color','b','linewidth',4)
for i = 1:4
line([0 4],[i i],'color','b','linestyle','--')
end
for i = 1:3
line([i i],[0 5],'color','b','linestyle','--')
end
axis equal
axis([0 4 0 5])
axis off
function drawrect(x1,x2,x3,x4,color)
x = [x1(1) x2(1) x3(1) x4(1)];
y = [x1(2) x2(2) x3(2) x4(2)];
fill(x,y,color)