有趣的MATLAB 1.游戏程序
更新时间:2024-03-09 05:04:01 阅读量: 综合文库 文档下载
- 有趣的matlab代码推荐度:
- 相关推荐
MATLAB游戏程序
目录
1.空格游戏 ............................................................................................................................ 2 2.华容道 ................................................................................................................................ 3 3.凑五子棋 .......................................................................................................................... 14 4.2048 .................................................................................................................................. 19 5.俄罗斯方块 ...................................................................................................................... 24
1.空格游戏
function pintu1() A = gen();
G = [1 2 3;4 5 6;7 8 0]; drawmap(A);
while 1
[xpos,ypos] = ginput(1); col = ceil(xpos);
row = 3-ceil(ypos)+1; num = A(row,col);
if row>1&A(row-1,col)==0 A(row-1,col) = num; A(row,col) = 0; end
if row<3&A(row+1,col)==0 A(row+1,col) = num; A(row,col) = 0; end
if col>1&A(row,col-1)==0 A(row,col-1) = num; A(row,col) = 0; end
if col<3&A(row,col+1)==0 A(row,col+1) = num; A(row,col) = 0; end
drawmap(A) zt = abs(A-G); if sum(zt(:))==0
msgbox('恭喜您成功完成!') break end end
function drawmap(A) clf; hold on
line([0 3],[0 0],'linewidth',4); line([3 3],[0 3],'linewidth',4); line([0 3],[3 3],'linewidth',4); line([0 0],[0 3],'linewidth',4);
for i = 1:3
for j = 1:3
drawrect([j-1 3-i],[j 3-i],[j 3-i+1],[j-1 3-i+1],'y',A(i,j)); end end
axis equal axis off
function drawrect(x1,x2,x3,x4,color,num) x = [x1(1) x2(1) x3(1) x4(1)]; y = [x1(2) x2(2) x3(2) x4(2)]; fill(x,y,color) if num==0
text(0.5*(x1(1)+x2(1)),0.5*(x1(2)+x4(2)),' ','fontsize',24) else
text(0.5*(x1(1)+x2(1))-0.05,0.5*(x1(2)+x4(2)),num2str(num),'fontsize',24) end
function y = gen() y = inf*ones(1,9); for i = 1:9 while 1
a = randint(1,1,9); if isempty(find(y==a)) y(i) = a; break end end end
y = reshape(y,3,3);
2.华容道
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)
3.凑五子棋
function [ ] = five()
global a h m1 n1 m2 n2 t h1 h2 h3 color score hsc ha ss hf=figure('resize','off','name','five',...
'position',[360 280 560 420],'numbertitle','off');
ha=axes;
set(gcf,'menubar','none','color',[0.3 0.3 0.3])
set(gca,'position',[0.2300 0.1100 0.7750 0.8150]) set(gca,'xlim',[0,9],'ylim',[0,9]) set(ha,'xtick',[],'ytick',[],'box','on') set(ha,'color',[0.7 0.6,0.6])
set(ha,'DataAspectRatio',[1 1 1],'PlotBoxAspectRatio',[1 1 1]) x=repmat([0;9],1,9); y=[1:9;1:9];
line(x,y,'color','k') line(y,x,'color','k')
hst=uicontrol('style','text','string','Score','fontsize',30,...
'units','normal','position',[0.02,0.55,0.26,0.14],'parent',hf,... 'ForegroundColor','w','backgroundcolor',[0.3 0.3 0.3],... 'fontweight','bold');
hsc=uicontrol('style','text','string','0','fontsize',24,...
'units','normal','position',[0.02,0.4,0.26,0.14],'parent',hf,... 'ForegroundColor','w','backgroundcolor',[0.3 0.3 0.3],... 'fontweight','bold');
hbt=uicontrol('style','pushbutton','string','Restart','fontsize',18,... 'units','normal','position',[0.02,0.16,0.26,0.14],'parent',hf,... 'fontweight','bold','callback',@restart); color=[... 1 1 0; 1 0 1; 0 1 1; 1 0 0; 0 1 0; 0 0 1; 0.7 0.3 0; ];
h1=annotation('ellipse',[0.04,0.84,0.06,0.08],'facecolor','k'); h2=annotation('ellipse',[0.12,0.84,0.06,0.08],'facecolor','k'); h3=annotation('ellipse',[0.2,0.84,0.06,0.08],'facecolor','k'); set(ha,'buttondownfcn',@select2) initialize
function initialize()
global a h m1 n1 m2 n2 t h1 h2 h3 color score hsc ss a=zeros(9);
h=zeros(9)*NaN; m1=[]; n1=[]; m2=[]; n2=[];
score=0; ss=0;
k=rs(1:81,5);
t=ceil(rand(1,5)*7); a(k)=t;
[m,n] = ind2sub([9,9],k); y=9.5-m; x=n-0.5; for p=1:5
h(k(p))=line(x(p),y(p),'marker','o','markersize',24,...
'markerfacecolor',color(t(p),:),'markeredgecolor','none',... 'buttondownfcn',@select1); end
t=ceil(rand(1,3)*7);
set(h1,'facecolor',color(t(1),:)) set(h2,'facecolor',color(t(2),:)) set(h3,'facecolor',color(t(3),:)) function [k]=rs(s,n); for m=1:n
t=ceil(rand*length(s)); k(m)=s(t); s(t)=[]; end
function select1(src,eventdata) global a h m1 n1
n1=ceil(get(src,'xdata')); m1=ceil(9-get(src,'ydata'));
set(h(~isnan(h)),'markeredgecolor','none') set(src,'markeredgecolor','w') function select2(src,eventdata)
global a h m1 n1 m2 n2 t h1 h2 h3 color score hsc ha ss if isempty(m1) || isempty(n1) return end
cp=get(src,'currentpoint'); n2=ceil(cp(1,1)); m2=ceil(9-cp(1,2)); if a(m2,n2) return end b=~a;
b(m1,n1)=1; b=bwlabel(b,4);
if b(m1,n1)~=b(m2,n2)
return end
a(m2,n2)=a(m1,n1); a(m1,n1)=0;
h(m2,n2)=h(m1,n1); h(m1,n1)=NaN;
set(h(m2,n2),'xdata',n2-0.5,'ydata',9.5-m2,'markeredgecolor','none') m1=[]; n1=[];
judgement;
if sum(sum(~a))<3
hgo=text(1,4.5,'Game Over','fontsize',36,'fontweight',... 'bold','parent',src); pause(3) delete(hgo);
delete(h(~isnan(h))) set(hsc,'string','0') initialize; return end if ~ss new; end
function judgement
global a h m1 n1 m2 n2 t h1 h2 h3 color score hsc ha ss b=logical(zeros(9,9)); ss=0; left=0; right=0; up=0; down=0; lu=0; rd=0; ld=0; ru=0;
while n2-left-1>0 && a(m2,n2-left-1)==a(m2,n2) left=left+1; end
while n2+right+1<10 && a(m2,n2+right+1)==a(m2,n2) right=right+1; end
while m2-up-1>0 && a(m2-up-1,n2)==a(m2,n2) up=up+1; end
while m2+down+1<10 && a(m2+down+1,n2)==a(m2,n2) down=down+1; end
while n2-lu-1>0 && m2-lu-1>0 && a(m2-lu-1,n2-lu-1)==a(m2,n2) lu=lu+1; end
while n2+rd+1<10 && m2+rd+1<10 && a(m2+rd+1,n2+rd+1)==a(m2,n2) rd=rd+1; end
while n2-ld-1>0 && m2+ld+1<10 && a(m2+ld+1,n2-ld-1)==a(m2,n2) ld=ld+1; end
while n2+ru+1<10 && m2-ru-1>0 && a(m2-ru-1,n2+ru+1)==a(m2,n2) ru=ru+1; end
if left+right+1>=5
b(m2,n2-left:n2+right)=1; end
if up+down+1>=5
b(m2-up:m2+down,n2)=1; end
if lu+rd+1>=5
ind=sub2ind([9,9],m2-lu:m2+rd,n2-lu:n2+rd); b(ind)=1; end
if ld+ru+1>=5
ind=sub2ind([9,9],m2+ld:-1:m2-ru,n2-ld:n2+ru); b(ind)=1; end
if sum(sum(b)) a(b)=0;
delete(h(b)); h(b)=NaN;
score=score+sum(sum(b)); set(hsc,'string',num2str(score)) ss=1; end
function new
global a h m1 n1 m2 n2 t h1 h2 h3 color score hsc ha k=rs(find(~a),3); a(k)=t;
[mt,nt] = ind2sub([9,9],k); y=9.5-mt; x=nt-0.5;
for p=1:3
h(k(p))=line(x(p),y(p),'marker','o','markersize',24,...
'markerfacecolor',color(t(p),:),'markeredgecolor','none',... 'buttondownfcn',@select1); end
for p=1:3
m2=mt(p); n2=nt(p); judgement; end
if sum(sum(~a))==0
hgo=text(1,4.5,'Game Over','fontsize',36,'fontweight',... 'bold','parent',ha); pause(3) delete(hgo);
delete(h(~isnan(h))) set(hsc,'string','0') initialize; return end
t=ceil(rand(1,3)*7);
set(h1,'facecolor',color(t(1),:)) set(h2,'facecolor',color(t(2),:)) set(h3,'facecolor',color(t(3),:)) function restart(src,eventdata)
global a h m1 n1 m2 n2 t h1 h2 h3 color score hsc ha ss delete(h(~isnan(h))) set(hsc,'string','0') initialize;
4.2048
function g2048(action)
global totalscore flag score_board if nargin<1
figure_h=figure;
set(figure_h,'Units','points')
set(figure_h,'UserData',figure_h); totalscore=0; flag=0;
score_board=zeros(1,16); action='initialize'; end
switch action
case 'initialize';
figure_h=get(gcf,'UserData'); set(figure_h,...
'Color',[0.4 0.4 0.4],... 'Menubar','none',... 'Name','2048',... 'NumberTitle','off',...
'Position',[200 200 320 355],... 'Resize','off'); axis('off')
game_score=uicontrol(figure_h,... 'BackgroundColor',[1 1 1],... 'ForegroundColor',[0 0 0], ... 'HorizontalAlignment','center',... 'FontSize',12,... 'Units','points',...
'Position',[235 305 65 30],... 'String','Score',... 'Style','edit',...
'Tag','game_score');
new_game_h=uicontrol(figure_h,... 'Callback','g2048 restart',... 'FontSize',12, ... 'Units','points',...
'Position',[35 30 65 30],... 'String','New Game',... 'Style','pushbutton'); % close
close_h=uicontrol(figure_h,... 'Callback','close(gcf)',... 'Fontsize',12, ... 'Units','points',...
'Position',[225 30 65 30],... 'String','Close',... 'Style','pushbutton'); % right
move_right=uicontrol(figure_h,... 'Callback','g2048 right',... 'Fontsize',12, ... 'Units','points',...
'Position',[255 185 60 30],... 'String','Right',... 'Style','pushbutton'); % left
move_left=uicontrol(figure_h,... 'Callback','g2048 left',... 'Fontsize',12, ... 'Units','points',...
'Position',[5 185 60 30],... 'String','Left',...
'Style','pushbutton'); % up
move_up=uicontrol(figure_h,... 'Callback','g2048 up',... 'Fontsize',12, ... 'Units','points',...
'Position',[130 300 60 30],... 'String','Up',...
'Style','pushbutton'); % down
move_down=uicontrol(figure_h,... 'Callback','g2048 down',... 'Fontsize',12, ... 'Units','points',...
'Position',[130 80 60 30],... 'String','Down',... 'Style','pushbutton'); % setup the game board irows=1;
for counter=1:16 jcols=rem(counter,4); if jcols==0 jcols=4; end
position=[40*jcols+40 85+40*irows 40 40]; index=(irows-1)*4+jcols; if jcols==4
irows=irows+1; end
board.squares(index)=uicontrol(figure_h,... 'FontSize',18,...
'FontWeight','bold',... 'Units','points',... 'Position',position,... 'Style','pushbutton',... 'Tag',num2str(index)); end
set(figure_h,'userdata',board);
g2048('restart') case 'restart' totalscore=0;
score_board=zeros(1,16); g2048('addnum') ; g2048('addnum') ; g2048('show') case'show'
num_0=find(score_board==0); board=get(gcf,'UserData');
set(board.squares,{'string'},num2cell(score_board)') set(board.squares,...
'BackgroundColor',[0.701961 0.701961 0.701961],... 'Enable','on',... 'Visible','on')
set(board.squares(num_0),... 'BackgroundColor','black',... 'Enable','off',... 'String',' ');
score_handle=findobj(gcf,'Tag','game_score'); set(score_handle,...
'String',num2str(totalscore),... 'Tag','game_score'); case 'down' C=score_board; for i=1:4
A=[score_board(i) score_board(i+4) score_board(i+8) score_board(i+12)]; [B score]=move(A);
score_board(i)=B(1); score_board(i+4)=B(2);score_board(i+8)=B(3); score_board(i+12)=B(4); totalscore=totalscore+score; end
if C==score_board else
g2048('show'); g2048('addnum') ; pause(0.2); g2048('show'); end case 'up'
C=score_board; for i=13:16
A=[score_board(i) score_board(i-4) score_board(i-8) score_board(i-12)]; [B score]=move(A);
score_board(i)=B(1); score_board(i-4)=B(2);score_board(i-8)=B(3); score_board(i-12)=B(4);
totalscore=totalscore+score; end
if C==score_board else
g2048('show'); g2048('addnum') ; pause(0.2); g2048('show'); end
case 'right' C=score_board; for i=4:4:16
A=[score_board(i) score_board(i-1) score_board(i-2) score_board(i-3)]; [B score]=move(A);
score_board(i)=B(1); score_board(i-1)=B(2);score_board(i-2)=B(3); score_board(i-3)=B(4); totalscore=totalscore+score; end
if C==score_board else
g2048('show'); g2048('addnum') ; pause(0.2); g2048('show'); end
case 'left'
C=score_board; for i=1:4:13
A=[score_board(i) score_board(i+1) score_board(i+2) score_board(i+3)]; [B score]=move(A);
score_board(i)=B(1); score_board(i+1)=B(2);score_board(i+2)=B(3); score_board(i+3)=B(4); totalscore=totalscore+score; end
if C==score_board else
g2048('show'); g2048('addnum') ; pause(0.2); g2048('show'); end
case'addnum'
num_0=find(score_board==0); l=length(num_0); if l>0
score_board(num_0(ceil(l*rand)))=2+2*(rand<0.1);
end end end
function Y=addnum(X) num_0=find(X==0); l=length(num_0);
X(num_0(ceil(l*rand)))=2+2*(rand<0.1); Y=X; end
function [B score]=move(A) score=0; for k=1:2 for i=1:3 if A(i)==0 for j=i:3 A(j)=A(j+1); end A(4)=0; end end end
if A(1)==A(2) if A(3)==A(4)
A(1)=A(1)+A(2);A(2)=A(3)+A(4);A(3)=0;A(4)=0;score=A(1)+A(2); else
A(1)=A(1)+A(2);A(2)=A(3);A(3)=A(4);A(4)=0;score=A(1); end
else if A(2)==A(3)
A(1)=A(1);A(2)=A(2)+A(3);A(3)=A(4);A(4)=0;score=A(2); else if A(3)==A(4)
A(1)=A(1);A(2)=A(2);A(3)=A(3)+A(4);A(4)=0;score=A(3); else score=0; end end end B=A; End
5.俄罗斯方块
%各个函数请分开写 if nargin == 0
OldHandle = findobj( 'Type', 'figure', 'Tag', 'RussiaBlock' ) ; if ishandle( OldHandle )
delete( OldHandle ) ; end
FigureHandle = figure( 'Name', '俄罗斯方块MATLAB版', 'Tag', 'RussiaBlock', 'NumberTitle', 'off',...
'Menubar', 'none', 'DoubleBuffer', 'on', 'Resize', 'off', 'visible', 'on',... 'KeyPressFcn', 'RussiaBlock( ''KeyPress_Callback'', gcbo )',... 'HelpFcn', 'helpdlg(''帮不了你- -!'',''不好意思'')',...
'CloseRequestFcn', 'RussiaBlock( ''CloseFigure_Callback'', gcbo )' ) ;
generate_FigureContent( FigureHandle ) ;
init_FigureContent( FigureHandle ) ;
set( FigureHandle, 'Visible', 'on' ) ;
elseif ischar( varargin{1} )
feval( varargin{:} ) ; end
% ------------------------------------------------------------------------- function generate_FigureContent( FigureHandle )
TabSpace = 30 ; BlockWidth = 20 ; BlockHeight = 20 ;
FigureWidth = BlockWidth * (12 + 1) + TabSpace * 7; FigureHeight = 500 ;
set( FigureHandle, 'Position', [0 0 FigureWidth FigureHeight] ) ; movegui( FigureHandle, 'center' ) ;
% 创建菜单
BeginMenu = uimenu( FigureHandle, 'Label', '开始' ) ;
StartMenu = uimenu( BeginMenu, 'Label', '开始新游戏', 'Accelerator', 'N',... 'Callback', 'RussiaBlock( ''StartNewGame_Callback'', gcbo )');
SaveMenu = uimenu( BeginMenu, 'Label', '保存', 'Accelerator', 'S', 'Enable', 'off',... 'Separator', 'on', 'Cal', 'RussiaBlock( ''SaveGame_Callback'', gcbo )' );
LoadMenu = uimenu( BeginMenu, 'Label', '读取', 'Accelerator', 'L', 'Enable', 'off',... 'Cal', 'RussiaBlock( ''LoadGame_Callback'', gcbo )' );
QuitMenu = uimenu( BeginMenu, 'Label', '退出', 'Accelerator', 'Q', 'Separator', 'on', 'Cal', 'close(gcf)');
OperationMenu = uimenu( FigureHandle, 'Label', '功能' );
BoardConfigMenu = uimenu( OperationMenu, 'label', '键盘设置', 'Enable', 'off',... 'Cal', 'RussiaBlock( ''BoardConfig_Callback'', gcbo )' );
FigureConfigMenu = uimenu( OperationMenu, 'label', '界面设置', 'Enable', 'off',... 'Cal', 'RussiaBlock( ''FigureConfig_Callback'', gcbo )' );
HighScoreMenu = uimenu( OperationMenu, 'label', '最高记录', 'Separator', 'on',... 'Cal', 'RussiaBlock( ''HighScore_Callback'', gcbo )', 'Enable', 'off' ); GameLevelMenu = uimenu( OperationMenu, 'Label', '游戏难度',... 'Cal','RussiaBlock( ''GameLevel_Callback'', gcbo )' );
HelpMenu = uimenu( FigureHandle, 'Label', '帮助' );
AboutMenu = uimenu( HelpMenu, 'Label', '关于此软件', 'Cal', 'helpdlg(''俄罗斯方块MATLAB版------冰风漫天(制作)(2006/11/21)'',''关于此软件??'')');
HelpDlgMenu = uimenu( HelpMenu, 'Label', '游戏帮助', 'Separator', 'on', 'Cal', 'helpdlg(''帮不了你- -!'',''不好意思'')' );
% 创建工具条,图标可以用imread从图片读取,但图片不要太大
BeginTool = uipushtool( 'ToolTipString', '开始', 'CData', rand(16,16,3), 'Tag', 'BeginTool',... 'ClickedCallback', 'RussiaBlock( ''StartNewGame_Callback'', gcbo )' ) ;
PauseTool = uitoggletool( 'ToolTipString', '暂停', 'Tag', 'PauseTool', 'Tag', 'PauseTool',... 'CData', reshape( repmat( [1 1 0], 16, 16), [16,16,3] ),...
'ClickedCallback', 'RussiaBlock( ''PauseGame_Callback'', gcbo )' ) ;
% 创建游戏窗口
MainWindowXPos = TabSpace; MainWindowYPos = TabSpace;
MainWindowWidth = BlockWidth * 12 ; MainWindowHeight = BlockHeight * 22 ; MainWindowPosition = [MainWindowXPos MainWindowHeight] ;
MainWindowYPos MainWindowWidth
% 定义游戏窗口的右键菜单
AxesContextMenu = uicontextmenu( 'Tag', 'uicontextmenu' ) ;
uimenu( AxesContextMenu, 'Label', '设置窗口颜色', 'Cal', 'RussiaBlock( ''WindowColor_Callback'', gcbo )' ) uimenu( AxesContextMenu, 'Label', '设置背景图片', 'Cal', 'RussiaBlock( ''WindowPicture_Callback'', gcbo )' )
uimenu( AxesContextMenu, 'Label', '设置方块颜色', 'Cal', 'RussiaBlock( ''BlockColor_Callback'', gcbo )' )
uimenu( AxesContextMenu, 'Label', '恢复默认', 'Cal', 'RussiaBlock( ''Default_Callback'', gcbo )' )
MainAxes = axes( 'Units', 'pixels', 'Pos', MainWindowPosition, 'XTick', [], 'YTick',[], 'XTickLabel', [],...
'YTickLabel', [], 'Box', 'on', 'Tag', 'MainAxes', 'UicontextMenu', AxesContextMenu,... 'XLim', [0 MainWindowWidth], 'YLim', [0 MainWindowHeight] ) ; hold on;
% 创建一个窗口用于显示下一个方块的图形
NextBlockWndXPos = MainWindowXPos + MainWindowWidth + TabSpace ; NextBlockWndHeight = 4 * TabSpace + BlockHeight ;
NextBlockWndYPos = MainWindowYPos + MainWindowHeight - NextBlockWndHeight ; NextBlockWndWidth = TabSpace * 4 + BlockWidth ;
NextBlockWndPosition = [NextBlockWndXPos NextBlockWndYPos NextBlockWndWidth NextBlockWndHeight] ;
NextBlockAxes = axes( 'Units', 'pixels', 'Pos', NextBlockWndPosition, 'XTick', [], 'YTick',[],... 'XTickLabel', [], 'YTickLabel', [], 'XLim', [0 NextBlockWndWidth],... 'YLim', [0 NextBlockWndHeight], ...
'Box', 'on', 'Tag', 'NextBlockAxes', 'Color', [0.85 0.85 0.85] ) ;
% 创建一组控件,包括(两个文本框用于显示当前方块数和成绩,两个按钮用于暂停和退出)
ButtonTag = { 'QuitButton', 'PauseButton', 'BlockNumText', 'ScoreText' } ; ButtonStyle = { 'pushbutton', 'togglebutton', 'text', 'text' } ; FontColor = { [0 0 0], [1 0 0], [0 0 1], [1 0 1] } ;
ButtonColor = { [0.7 0.8 0.9], [0.3 1 0.3], [0.5 1 1], [0.5 1 1] } ; ButtonString = { '退出', '暂停', '方块数', '积分' };
ButtonCallback = { 'close(gcf)', 'RussiaBlock( ''ButtonPauseGame_Callback'', gcbo )', '', '' } ;
ButtonNumber = length( ButtonTag ) ;
ButtonWidth = NextBlockWndWidth ; ButtonHeight = 50 ;
ButtonXPos = NextBlockWndXPos ;
ButtonYPos = MainWindowYPos + TabSpace ;
ButtonPosition = [ButtonXPos ButtonYPos ButtonWidth ButtonHeight] ;
ButtonTabSpace = (NextBlockWndYPo s - 2 * TabSpace - ButtonHeight * ButtonNumber) / ButtonNumber ;
for num = 1: ButtonNumber
TempButtonPosition = ButtonPosition ;
TempButtonPosition(2) = ButtonPosition(2) + (num - 1) * (ButtonTabSpace +
ButtonHeight);
if findstr( ButtonStyle{num}, 'button' )
TempButtonPosition(1) = TempButtonPosition(1) + 10 ; TempButtonPosition(2) = TempButtonPosition(2) + 5 ; TempButtonPosition(3) = TempButtonPosition(3) - 10 * 2 ; TempButtonPosition(4) = TempButtonPosition(4) - 5 * 2 ; else
TempButtonPosition(1) = TempButtonPosition(1) - 10 ; TempButtonPosition(2) = TempButtonPosition(2) - 5 ; TempButtonPosition(3) = TempButtonPosition(3) + 10 * 2; TempButtonPosition(4) = TempButtonPosition(4) + 5 * 2 ; end
ButtonHandle = uicontrol( 'Tag', ButtonTag{num}, 'Style', ButtonStyle{num}, 'Pos', TempButtonPosition,...
'Foregroundcolor', FontColor{num}, 'Backgroundcolor', ButtonColor{num},... 'Fontsize', 16, 'String', ButtonString{num}, 'Cal', ButtonCallback{num} ) ;
if findstr( ButtonStyle{num}, 'text' ) set( ButtonHandle, 'Max', 2 ) ; end
if findstr( ButtonTag{num}, 'PauseButton' )
set( ButtonHandle, 'Enable', 'inactive', 'ButtonDownFcn', ButtonCallback{num}, 'Cal', '' ) ;
end end
MainBlockAxes = axes( 'Units', 'pixels', 'Pos', MainWindowPosition, 'XTick', [], 'YTick',[], 'XTickLabel', [],...
'YTickLabel', [], 'Box', 'on', 'Tag', 'MainBlockAxes', 'Hittest', 'off',...
'XLim', [0 MainWindowWidth], 'YLim', [0 MainWindowHeight], 'Color', 'none' ) ;
line( 'Visible', 'on', 'Tag', 'BlockHandle', 'Markersize', 18, 'Parent', MainBlockAxes, 'HitTest', 'off',... 'Marker', 's', 'MarkerEdgeColor', 'k', 'XData', nan, 'YData', nan, 'LineStyle', 'none' ) ;
line( 'Visible', 'off', 'Tag', 'TempBlock', 'Markersize', 18, 'Parent', MainBlockAxes, 'HitTest', 'off',... 'Marker', 's', 'MarkerEdgeColor', 'k', 'XData', 130, 'YData', 30, 'LineStyle', 'none' ) ;
line( 'Visible', 'off', 'Tag', 'NextBlock', 'Markersize', 18, 'Parent', NextBlockAxes, 'HitTest', 'off',... 'Marker', 's', 'MarkerEdgeColor', 'k', 'XData', 30, 'YData', 30, 'LineStyle', 'none' ) ;
setappdata( FigureHandle, 'XLim', [0 MainWindowWidth] ) setappdata( FigureHandle, 'YLim', [0 MainWindowHeight] )
handles = guihandles( FigureHandle ) ; guidata( FigureHandle, handles ) ;
% ------------------------------------------------------------------------- function init_FigureContent( FigureHandle )
handles = guidata( FigureHandle ) ;
ColorInfo = [] ; try
ColorInfo = load('ColorInfo.mat') ; catch end
if isempty( ColorInfo )
ColorInfo.BlockColor = GetDefaultBlockColor ;
ColorInfo.MainAxesColor = GetDefaultMainAxesColor ; ColorInfo.MainAxesImage.ImageData = [] ; end
set( handles.MainAxes, 'Color', ColorInfo.MainAxesColor ) ; if ~isempty( ColorInfo.MainAxesImage.ImageDa ta )
ImageHandle = image( ColorInfo.MainAxesImage.ImageData, 'Parent', handles.MainAxes ) ; set( ImageHandle, ColorInfo.MainAxesImage.Property ) ;
setappdata( FigureHandle, 'ImageData', ColorInfo.MainAxesImage.ImageData ) ; end
set( handles.BlockHandle, 'MarkerFaceColor', ColorInfo.BlockColor ) ; set( handles.TempBlock, 'MarkerFaceColor', ColorInfo.BlockColor ) ; set( handles.NextBlock, 'MarkerFaceColor', ColorInfo.BlockColor ) ;
setappdata( FigureHandle, 'BlockColor', ColorInfo.BlockColor ) ;
% ------------------------------------------------------------ function StartNewGame_Callback( h, StartType )
handles = guidata( h ) ;
global PauseTime
if nargin == 1
StartType = 'NewStart' ;
setappdata( handles.RussiaBlock, 'BlockNumber', 0 ) ; set( handles.BlockNumText, 'String', {'方块数','0'} ) ;
setappdata( handles.RussiaBlock, 'CurrentScore', 0 ) ; set( handles.ScoreText, 'String', {'积分','0'} ) ;
set( handles.BlockHandle, 'XData', nan, 'YData', nan ) ; set( handles.TempBlock, 'XData', nan, 'YData', nan ) ;
TextHandle = findobj( 'Parent', handles.MainBlockAxes, 'Type', 'text' ) ; delete( TextHandle ) ; else end
set( handles.NextBlock, 'Visible', 'on' ) ; set( handles.TempBlock, 'Visible', 'on' ) ;
set( handles.PauseTool, 'State', 'off' ) ; set( handles.PauseButton, 'Value', 0 ) ;
YLim = get( handles.MainAxes, 'YLim' ) ;
while( ishandle( h ) )
TotalYData = get( handles.BlockHandle, 'YData' ) ; if any( TotalYData >= YLim(2) ) % Game over
text( 20, 200, 'GameOver', 'Parent', handles.MainBlockAxes,... 'FontSize', 30, 'Color', 'r', 'FontAngle', 'italic' ) ; break; end
if length( TotalYData ) >= 4
TotalXData = get( handles.BlockHandle, 'XData' ) ; LastBlockYData = TotalYData( end - 3: end ) ; LastBlockYData = unique( LastBlockYData ) ; CompleteLine = [] ; UsefulIndex = [] ;
for num = 1: length( LastBlockYData )
[YData, Index] = find( TotalYData == LastBlockYData(num) ) ; if length( YData ) == 12
CompleteLine = [CompleteLine, LastBlockYData(num)] ; UsefulIndex = [UsefulIndex, Index] ; end end
if ~isempty( CompleteLine )
TotalXData( UsefulIndex ) = [] ; TotalYData( UsefulIndex ) = [] ;
LineNumber = length( CompleteLine ) ; ScoreArray = [100 300 600 1000] ; NewScore = ScoreArray(LineNumber) ;
CurrentScore = getappdata( handles.RussiaBlock, 'CurrentScore' ) ; TextString = get( handles.ScoreText, 'String' ) ; TextString{2} = CurrentScore + NewScore ; set( handles.ScoreText, 'String', TextString ) ;
setappdata( handles.RussiaBlock, 'CurrentScore', CurrentScore + NewScore ) ;
UpdateGameLevel( handles.RussiaBlock, CurrentScore + NewScore ) ; % 处理需要下移的方块 for num = LineNumber : -1 : 1
[YData, Index] = find( TotalYData > LastBlockYData(num) ) ; TotalYData(Index) = TotalYData(Index) - 20 ; end end
set( handles.BlockHandle, 'XData', TotalXData, 'YData', TotalYData ) ; end
BlockNumber = getappdata( handles.RussiaBlock, 'BlockNumber' ) ; TextString = get( handles.BlockNumText, 'String' ) ; TextString{2} = BlockNumber + 1 ;
set( handles.BlockNumText, 'String', TextString ) ;
setappdata( handles.RussiaBlock, 'BlockNumber', BlockNumber + 1 ) ;
GameLevel = getappdata( handles.RussiaBlock, 'GameLevel' ) ; if isempty( GameLevel ) PauseTime = 0.3 ; else
PauseTime = ceil( 20 / GameLevel ) / 100 ; end
TempBlockPos.LeftStep = 0 ; TempBlockPos.DownStep = 0 ;
setappdata( handles.RussiaBlock, 'TempBlockPos', TempBlockPos ) ; Status = 1;
[BlockXArray,BlockYArray] = Com_GetBlock( h ) ;
set( handles.TempBlock, 'XData', BlockXArray, 'YData', BlockYArray ) ; while( Status & ishandle( h ) ) if(PauseTime) ~= 0 pause( PauseTime ) end
Status = test_MoveBlock( h, 'Down' ) ; end end
% ------------------------------------------------------------------------- function KeyPress_Callback( h )
handles = guidata( h ) ;
PauseState = get( handles.PauseTool, 'State' ) ; if strcmp( PauseState, 'on' ) return end
BoardConfig = getappdata( handles.RussiaBlock, 'BoardConfig' ) ;
if isempty( BoardConfig ) Left = 'leftarrow' ; Right = 'rightarrow' ; Down = 'downarrow' ; Change = 'uparrow' ; Drop = 'space' ; else
Left = BoardConfig.Left ; Right = BoardConfig.Right ; Down = BoardConfig.Down ; Change = BoardConfig.Change ; Drop = BoardConfig.Drop ; end
CurrentKey = get( handles.RussiaBlock, 'CurrentKey' ) ;
switch CurrentKey case Left
test_MoveBlock( h, 'Left' ) ; case Right
test_MoveBlock( h, 'Right' ) ; case Down
test_MoveBlock( h, 'Down' ) ; case Change
test_MoveBlock( h, 'Change' ) ; case Drop
test_MoveBlock( h, 'Drop' ) ;
otherwise return ; end
% ------------------------------------------------------------------------- function WindowColor_Callback( h )
handles = guidata( h ) ;
CurrentColor = get( handles.MainAxes, 'Color' ) ;
NewColor = uisetcolor( CurrentColor, '请选择窗口颜色' ) ;
if length( NewColor ) == 0 return; else
set( handles.MainAxes, 'Color', NewColor ) ; end
% ------------------------------------------------------------------------- function WindowPicture_Callback( h )
handles = guidata( h ) ;
[PictureFile, Path] = uigetfil e( {'*.jpg; *.bmp'},'请选择图片' );
if isnumeric( PictureFile ) return ; else
% if length( PictureFile ) > 31
% errordlg( '文件名过长,读取失败' ) ; % end try
Picture = imread( [Path, PictureFile] ) ;
for num = 1: size( Picture, 3 )
ValidPicture(:, :, num) = flipud( Picture(:,:,num) ) ; end
AxesXLim = get( handles.MainAxes, 'XLim' ) ; AxesYLim = get( handles.MainAxes, 'YLim' ) ;
BlockHandle = findobj( handles.MainAxes, 'Style', 'line' ) ; cla( BlockHandle ) ;
ImageXLimit = size(Picture, 2) ;
ImageYLimit = size(Picture, 1) ;
if diff( AxesXLim ) < size(Picture, 2) | diff( AxesYLim ) < size(Picture, 1) % 超出坐标轴范围,压缩显示
XScale = diff( AxesXLim ) / size(Picture, 2) ; YScale = diff( AxesYLim ) / size(Picture, 1) ; % 取较小比例压缩
Scale = min( XScale, YScale ) ;
ImageXLimit = size(Picture, 2) * Scale ; ImageYLimit = size(Picture, 1) * Scale ; end
ImageXData(1) = AxesXLim(1) + (diff( AxesXLim ) - ImageXLimit) / 2 + 1; ImageXData(2) = ImageXData(1) + ImageXLimit - 1;
ImageYData(1) = AxesYLim(1) + (diff( AxesYLim ) - ImageYLimit) / 2 + 1; ImageYData(2) = ImageYData(1) + ImageYLimit - 1;
image( ValidPicture, 'Parent', handles.MainAxes, 'Hittest', 'off', ... 'XData',ImageXData,'YData',ImageYData, 'Tag', 'MainImage' ); setappdata( handles.RussiaBlock, 'ImageData', ValidPicture ) ; catch
ErrorString = sprintf( ['读取图片失败,错误信息为:\\n',lasterr] ) ; errordlg( ErrorString ) ; end end
% ------------------------------------------------------------------------- function BlockColor_Callback( h )
handles = guidata( h ) ;
CurrentColor = getappdata( handles.RussiaBlock, 'BlockColor' ) ; if isempty( CurrentColor )
CurrentColor = GetDefaultBlockColor ;
setappdata( handles.RussiaBlock, 'BlockColor', CurrentColor ) ; end
NewColor = uisetcolor( CurrentColor, '请选择方块颜色' ) ;
if length( NewColor ) == 0 return; else
setappdata( handles.RussiaBlock, 'BlockColor', NewColor ) ;
set( handles.BlockHandle, 'MarkerFaceColor', NewColor ) ; set( handles.TempBlock, 'MarkerFaceColor', NewColor ) ; set( handles.NextBlock, 'MarkerFaceColor', NewColor ) ; end
% ------------------------------------------------------------------------ function Default_Callback( h )
handles = guidata( h ) ;
BlockColor = GetDefaultBlockColor ; AxesColor = GetDefaultMainAxesColor ;
set( handles.MainAxes, 'Color', AxesColor ) ;
set( handles.BlockHandle, 'MarkerFaceColor', BlockColor ) ; set( handles.TempBlock, 'MarkerFaceColor', BlockColor ) ; set( handles.NextBlock, 'M arkerFaceColor', BlockColor ) ;
% ------------------------------------------------------------------------- function PauseGame_Callback( h )
handles = guidata( h ) ;
ToolStart = get( handles.PauseTool, 'State' ) ;
if strcmp( ToolStart, 'on' )
set( handles.PauseButton, 'Value', 1 ) ; waitfor( handles.PauseTool, 'State', 'off' ) ; else
set( handles.PauseButton, 'Value', 0 ) ; end
% ------------------------------------------------------------------------- function ButtonPauseGame_Callback( h )
handles = guidata( h ) ;
ToggleButtonValue = get( h, 'Value' ) ;
if ToggleButtonValue == 0
set( h, 'Value', 1 ) ;
set( h, 'String', '继续' ) ;
set( handles.PauseTool, 'State', 'on' ) ; waitfor( handles.PauseTool, 'State', 'off' ) ; else
set( h, 'Value', 0 ) ;
set( h, 'String', '暂停' ) ;
set( handles.PauseTool, 'State', 'off' ) ;
set( handles.RussiaBlock, 'CurrentObject', handles.MainAxes ) ; end
% ------------------------------------------------------------------------ function CloseFigure_Callback( h )
handles = guidata( h ) ;
BlockColor = getappdata( handles.RussiaBlock, 'BlockColor' ) ; MainAxesColor = get( handles.MainAxes, 'Color' ) ;
MainAxesImageHandle = findobj( handles.MainAxes, 'Type', 'image' ) ; if ~isempty( MainAxesImageHandle )
MainAxesImage.Property.Tag = get( MainAxesImageHandle, 'Tag' );
MainAxesImage.Property.Hittest = get( MainAxesImageHandle, 'Hittest' ); MainAxesImage.Property.XData = get( MainAxesImageHandle, 'XData' ); MainAxesImage.Property.YData = get( MainAxesImageHandle, 'YData' );
MainAxesImage.ImageData = getappdata( handles.RussiaBlock, 'ImageData' ) ; else
MainAxesImage.ImageData = [] ; end
save ColorInfo.mat BlockColor MainAxesColor MainAxesImage
delete( handles.RussiaBlock ) ;
% ------------------------------------------------------------------------- function Color = GetDefaultBlockColor
Color = [0 0 1] ;
% ------------------------------------------------------------------------- function Color = GetDefaultMainAxesColor
Color = [1 1 1] ;
% ----------------------------------------------------------------
function [BlockXArray, BlockYArray] = Com_GetBlock( varargin )
global BlockIndex ;
BlockXArray = [] ; BlockYArray = [] ;
handles = guidata( varargin{1} ) ;
if nargin == 1
BlockArray = getappdata( handles.RussiaBlock, 'BlockArray' ) ; BlockIndex = ceil( rand(1) * 24 ) ; else % nargin == 2
BlockIndex = varargin{2} ; end
switch(BlockIndex)
case {1,2,3,4} % 方块
BlockXArray = [0;0;1;1] * 20 - 10 ; BlockYArray = [0;1;1;0] * 20 - 10 ; case {5,6} % 竖长条
BlockXArray = [0;0;0;0] * 20 - 10 ; BlockYArray = [-1;0;1;2] * 20 - 10 ; case {7,8} % 横长条
BlockXArray = [-1;0;1;2] * 20 - 10 ; BlockYArray = [1;1;1;1] * 20 - 10 ; case {9} % 4类T T1
BlockXArray = [-1;0;1;0] * 20 - 10 ; BlockYArray = [1;1;1;0] * 20 - 10 ; case {10} % T2
BlockXArray = [0;0;1;0] * 20 - 10 ; BlockYArray = [2;1;1;0] * 20 - 10 ; case {11} % T3
BlockXArray = [0;0;1;-1] * 20 - 10 ; BlockYArray = [2;1;1;1] * 20 - 10 ; case {12} % T4
BlockXArray = [0;0;0;-1] * 20 - 10 ; BlockYArray = [2;1;0;1] * 20 - 10 ; case {13} % 8类L L1
BlockXArray = [0;0;0;1] * 20 - 10 ;
BlockYArray = [1;0;-1;-1] * 20 - 10 ; case {14} % L2
BlockXArray = [-1;0;1;1] * 20 - 10 ; BlockYArray = [0;0;0;1] * 20 - 10 ; case {15} % L3
BlockXArray = [-1;0;0;0] * 20 - 10 ; BlockYArray = [1;1;0;-1] * 20 - 10 ; case {16} % L4
BlockXArray = [-1;-1;0;1] * 20 - 10 ; BlockYArray = [-1;0;0;0] * 20 - 10 ; case {17} % L5
BlockXArray = [-1;0;0;0] * 20 - 10 ; BlockYArray = [-1;-1;0;1] * 20 - 10 ; case {18} % L6
BlockXArray = [-1;-1;0;1] * 20 - 10 ; BlockYArray = [1;0;0;0] * 20 - 10 ; case {19} % L7
BlockXArray = [0;0;0;1] * 20 - 10 ; BlockYArray = [-1;0;1;1] * 20 - 10 ; case {20} % L8
BlockXArray = [-1;0;1;1] * 20 - 10 ; BlockYArray = [0;0;0;-1] * 20 - 10 ;
case {21 22} % 4类Z Z1
BlockXArray = [-1;0;0;1] * 20 - 10 ; BlockYArray = [1;1;0;0] * 20 - 10 ; case {23 24} % Z2
BlockXArray = [0;0;1;1] * 20 - 10 ; BlockYArray = [-1;0;0;1] * 20 - 10 ; case {25 26} % Z3
BlockXArray = [-1;0;0;1] * 20 - 10 ; BlockYArray = [0;0;1;1] * 20 - 10 ; case {27 28} % Z4
BlockXArray = [0;0;1;1] * 20 - 10 ; BlockYArray = [1;0;0;-1] * 20 - 10 ; end
if nargin == 1
NewBlockArray.BlockXArray = BlockXArray ; NewBlockArray.BlockYArray = BlockYArray ; NewBlockArray.BlockIndex = BlockIndex ;
NextAxesXLim = get( handles.NextBlockAxes, 'XLim' ) ; NextAxesYLim = get( handles.NextBlockAxes, 'YLim' ) ;
set( handles.NextBlock, 'XData', [BlockXArray + ceil( sum( BlockXArray ) / 4 ) ],...
0.5 * diff( NextAxesXLim ) - 'YData', [BlockYArray + 0.5 * diff( NextAxesYLim )] - ceil( sum( BlockYArray ) / 4 ) ) ;
setappdata( handles.RussiaBlock, 'BlockArray', NewBlockArray ) ; if isempty( BlockArray )
Com_GetBlock( varargin{1} ) ; else
BlockXArray = BlockArray.BlockXArray ; BlockYArray = BlockArray.BlockYArray ; BlockIndex = BlockArray.BlockIndex ; end end
AxesXLim = getappdata( handles.RussiaBlock, 'XLim' ) ; AxesYLim = getappdata( handles.RussiaBlock, 'YLim' ) ;
BlockXArray = BlockXArray + 0.5 * diff( AxesXLim ) ; BlockYArray = BlockYArray + diff( AxesYLim ) ;
% ------------------------------------------------------------------------- function Status = test_MoveBl ock( h, MoveMode )
Status = 1;
if ~ishandle( h ) return end
handles = guidata( h ) ;
TempXData = get( handles.TempBlock, 'XData' ) ; TempYData = get( handles.TempBlock, 'YData' ) ; TempXData = TempXData'; TempYData = TempYData' ;
TotalXData = get( handles.BlockHandle, 'XData' ) ; TotalYData = get( handles.BlockHandle, 'YData' ) ; TotalXData = TotalXData' ; TotalYData = TotalYData' ;
TempBlockPos = getappdata( handles.RussiaBlock, 'TempBlockPos' ) ; if isempty( TempBlockPos ) return end
AxesXLim = getappdata( handles.RussiaBlock, 'XLim' ) ; AxesYLim = getappdata( handles.RussiaBlock, 'YLim' ) ;
switch MoveMode case 'Left'
if any( TempXData - 20 < AxesXLim(1) ) return end
TestArray = ismember( [TempXData - 20, TempYData], [TotalXData, TotalYData], 'rows' ) ;
if any( TestArray ) return; else
set( handles.TempBlock, 'XData', TempXData - 20 ) ; TempBlockPos.LeftStep = TempBlockPos.LeftStep + 1 ;
setappdata( handles.RussiaBlock, 'TempBlockPos', TempBlockPos ) ; end case 'Right'
if any( TempXData + 20 > AxesXLim(2) ) return end
TestArray = ismember( [TempXData + 20, TempYData], [TotalXData, TotalYData], 'rows' ) ; if any( TestArray ) return; else
set( handles.TempBlock, 'XData', TempXData + 20 ) ; TempBlockPos.LeftStep = TempBlockPos.LeftStep - 1 ;
setappdata( handles.RussiaBlock, 'TempBlockPos', TempBlockPos ) ; end case 'Down'
if any( TempYData - 20 < AxesYLim(1) )
set( handles.BlockHandle, 'XData', [TotalXData; TempXData],... 'YData', [TotalYData; TempYData] ) ; Status = 0 ; return end
TestArray = ismember( [TempXData, TempYData - 20], [TotalXData, TotalYData], 'rows' ) ;
if any( TestArray )
set( handles.BlockHandle, 'XData', [TotalXData; TempXData],... 'YData', [TotalYData; TempYData] ) ; Status = 0 ; else
set( handles.TempBlock, 'YData', TempYData - 20 ) ;
TempBlockPos.DownStep = TempBlockPos.DownStep + 1 ;
setappdata( handles.RussiaBlock, 'TempBlockPos', TempBlockPos ) ; end case 'Drop'
global PauseTime PauseTime = 0 ; case 'Change'
global BlockIndex
OldBlockIndex = BlockIndex ; switch BlockIndex case {1,2,3,4} return; case {5,6}
NewIndex = 7 ; case {7,8}
NewIndex = 5 ; case {9,10,11,12}
NewIndex = mod( OldBlockIndex, 4 ) + 9; case {13,14,15,16}
NewIndex = mod( OldBlockIndex, 4 ) + 13; case {17,18,19,20}
NewIndex = mod( OldBlockIndex, 4 ) + 17; case {21,22}
NewIndex = 23; case {23,24}
NewIndex = 21; case {25,26}
NewIndex = 27 ; case {27,28}
NewIndex = 25 ; end
[BlockXArray, BlockYArray] = Com_GetBlock( h, NewIndex ) ; NewTempXData = BlockXArray - TempBlockPos.LeftStep * 20 ; NewTempYData = BlockYArray - TempBlockPos.DownStep * 20 ;
if any( NewTempXData < AxesXLim(1) ) | any( NewTempXData > AxesXLim(2) ) |... any( NewTempYData < AxesYLim(1) ) BlockIndex = OldBlockIndex ; return; end
TestArray = ismember( [NewTempXData, NewTempYData], [TotalXData, TotalYData], 'rows' ) ;
if any( TestArray )
BlockIndex = OldBlockIndex ;
else
BlockIndex = NewIndex ;
set( handles.TempBlock, 'XData', NewTempXData, 'YData', NewTempYData ) ; end end
% ------------------------------------------------------------------------- function UpdateGameLevel( FigureHandle, Score ) ;
GameLevel = ceil( Score / 10000 ) ;
if GameLevel > 9 GameLevel = 9 ; end
setappdata( FigureHandle, 'GameLevel', GameLevel ) ;
% ------------------------------------------------------------------------- function GameLevel_Callback( h )
handles = guidata( h ) ;
ScoreFigure = figure( 'name', '游戏难度设置', 'menubar', 'none', 'numbertitle', 'off',... 'pos', [200 200 200 180], 'windowstyle', 'modal', 'resize', 'off' ) ;
uicontrol( 'Parent', ScoreFigure, 'Style', 'listbox', 'Pos', [20 20 80 140],... 'String', [sprintf( '%d 级|', 1:8 ),'9 级'] ) ;
uicontrol( 'Parent', ScoreFigure, 'Style', 'pushbutton', 'Pos', [120 100 60 50], 'String', '确定',... 'Cal', ['GameLevel = get( findobj( ''Style'', ''listbox''), ''Value'');',... 'set( 0, ''Userdata'', GameLevel ); close( gcf );'] ) ;
uicontrol( 'Parent', ScoreFigure, 'Style', 'pushbutton', 'Pos', [120 30 60 50], 'String', '取消',... 'Cal', 'close( gcf )' ) ;
waitfor( ScoreFigure ) ;
ButtonAction = questdlg( '重新设置游戏难度将初始化界面,是否继续?', '设置确认', '是的', '取消', '取消' ) ;
if strcmp( '取消', ButtonAction ) return end
GameLevel = get( 0, 'Userdata' ) ;
setappdata( handles.RussiaBlock, 'GameLevel', GameLevel ) ; i
i
以上内容均来自于网络,由万里霜晨月整理。
正在阅读:
有趣的MATLAB 1.游戏程序03-09
专题研究报告-《基础设施和公用事业特许经营管理办法》评析及与《市政公用事业特许经营管理办法》条文比较 - 图文12-13
西门子电烤箱使用说明05-17
完整升级版七年级政治上册全套教案105-04
连锁养老机构人力资源管理制度附表03-31
泉州市财政局项目质量计划06-01
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 有趣
- 程序
- MATLAB
- 游戏
- 2019年中考数学一轮复习第七章图形与变换第三节立体图形的三视图
- 2019-2020年八年级数学上学期12月提优训练试题 苏科版
- 调价申请
- 数据库原理及应用实验报告 9
- 翟中和细胞生物学各章习题及答案
- 国能安全161号 二十五项反措题库
- 浅析煤炭企业财务风险评估问题 - 图文
- 常用工具软件试题
- 2018-2019年赤峰市元宝山区元宝山矿区小学元宝山区风水沟小学一
- 什么是入党志愿书
- 如何列二元一次方程组解应用题
- 人民政府办公室关于加强今冬明春消防安全工作方案DOC可编辑范文
- 转炉炼钢电气仪表安装施工方案
- 中传通信、信号专业复试面试常见问题
- 学校物资采购入库领用制度
- 职务犯罪预防工作积极参与社会管理创新的途径和强化对策
- 2018年农村信用社稽核监察工作总结范文与2018年农村党支部工作总
- 锅炉检修专业试题
- 危险化学品运输车辆事故的应急预案解读
- 校园导航系统课程设计