有趣的MATLAB-1.游戏程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

MATLAB游戏程序目录1.空格游戏.............................................................................................................................22.华容道.................................................................................................................................33.凑五子棋...........................................................................................................................144.2048...................................................................................................................................195.俄罗斯方块.......................................................................................................................241.空格游戏functionpintu1()A=gen();G=[123;456;780];drawmap(A);while1[xpos,ypos]=ginput(1);col=ceil(xpos);row=3-ceil(ypos)+1;num=A(row,col);ifrow1&A(row-1,col)==0A(row-1,col)=num;A(row,col)=0;endifrow3&A(row+1,col)==0A(row+1,col)=num;A(row,col)=0;endifcol1&A(row,col-1)==0A(row,col-1)=num;A(row,col)=0;endifcol3&A(row,col+1)==0A(row,col+1)=num;A(row,col)=0;enddrawmap(A)zt=abs(A-G);ifsum(zt(:))==0msgbox('恭喜您成功完成!')breakendendfunctiondrawmap(A)clf;holdonline([03],[00],'linewidth',4);line([33],[03],'linewidth',4);line([03],[33],'linewidth',4);line([00],[03],'linewidth',4);fori=1:3forj=1:3drawrect([j-13-i],[j3-i],[j3-i+1],[j-13-i+1],'y',A(i,j));endendaxisequalaxisofffunctiondrawrect(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)ifnum==0text(0.5*(x1(1)+x2(1)),0.5*(x1(2)+x4(2)),'','fontsize',24)elsetext(0.5*(x1(1)+x2(1))-0.05,0.5*(x1(2)+x4(2)),num2str(num),'fontsize',24)endfunctiony=gen()y=inf*ones(1,9);fori=1:9while1a=randint(1,1,9);ifisempty(find(y==a))y(i)=a;breakendendendy=reshape(y,3,3);2.华容道functionhuarongdao()A=[2113;2113;4665;4775;7007];drawmap(A)while1ifA(5,2)==1&A(5,3)==1ch=menu('曹操成功逃出华容道!如果要继续玩,按“是”,否则按“否”','是','否');switchchcase1huarongdao();case2returnendend[xpos,ypos]=ginput(1);col=ceil(xpos);row=5-ceil(ypos)+1;juese=A(row,col);switchjuesecase1%点击了曹操[I,J]=find(A==1);rm=max(I);rn=min(I);lm=max(J);ln=min(J);%判断是否能向左移ifln1&isequalm(A([rn,rm],ln-1),[0;0])A([rn,rm],ln-1)=[1;1];A([rn,rm],lm)=[0;0];drawmap(A)end%判断是否能向右移iflm4&isequalm(A([rn,rm],lm+1),[0;0])A([rn,rm],lm+1)=[1;1];A([rn,rm],ln)=[0;0];drawmap(A)end%判断是否能向下移ifrn1&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%判断是否能向上移ifrm5&isequalm(A(rm+1,[ln,lm]),[0,0])A(rm+1,[ln,lm])=[1,1];A(rm-1,[ln,lm])=[0,0];drawmap(A)endcase2%点击了黄忠[I,J]=find(A==2);rm=max(I);rn=min(I);lm=max(J);ln=min(J);%判断是否能向左移ifln1&isequalm(A([rn,rm],ln-1),[0;0])A([rn,rm],ln-1)=[2;2];A([rn,rm],lm)=[0;0];drawmap(A)end%判断是否能向右移iflm4&isequalm(A([rn,rm],lm+1),[0;0])A([rn,rm],lm+1)=[2;2];A([rn,rm],ln)=[0;0];drawmap(A)endifrn1&A(rn-1,ln)==0ifrm5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位ch=menu('请选择移到的方向:','上','下')switchchcase1%上移A(rn-1,ln)=2;A(rn+1,ln)=0;drawmap(A)case2%下移A(rm+1,ln)=2;A(rm-1,ln)=0;drawmap(A)endelse%只能上移A(rn-1,ln)=2;A(rn+1,ln)=0;drawmap(A)endelseifrm5&A(rm+1,ln)==0A(rm+1,ln)=2;A(rm-1,ln)=0;drawmap(A)endcase3%张飞[I,J]=find(A==3);rm=max(I);rn=min(I);lm=max(J);ln=min(J);%判断是否能向左移ifln1&isequalm(A([rn,rm],ln-1),[0;0])A([rn,rm],ln-1)=[3;3];A([rn,rm],lm)=[0;0];drawmap(A)end%判断是否能向右移iflm4&isequalm(A([rn,rm],lm+1),[0;0])A([rn,rm],lm+1)=[3;3];A([rn,rm],ln)=[0;0];drawmap(A)endifrn1&A(rn-1,ln)==0ifrm5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位ch=menu('请选择移到的方向:','上','下')switchchcase1%上移A(rn-1,ln)=3;A(rn+1,ln)=0;drawmap(A)case2%下移A(rm+1,ln)=3;A(rm-1,ln)=0;drawmap(A)endelse%只能上移A(rn-1,ln)=3;A(rn+1,ln)=0;drawmap(A)endelseifrm5&A(rm+1,ln)==0A(rm+1,ln)=3;A(rm-1,ln)=0;drawmap(A)endcase4%马超[I,J]=find(A==4);rm=max(I);rn=min(I);lm=max(J);ln=min(J);%判断是否能向左移ifln1&isequalm(A([rn,rm],ln-1),[0;0])A([rn,rm],ln-1)=[4;4];A([rn,rm],lm)=[0;0];drawmap(A)end%判断是否能向右移iflm4&isequalm(A([rn,rm],lm+1),[0;0])A([rn,rm],lm+1)=[4;4];A([rn,rm],ln)=[0;0];drawmap(A)endifrn1&A(rn-1,ln)==0ifrm5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位ch=menu('请选择移到的方向:','上','下')switchchcase1%上移A(rn-1,ln)=4;A(rn+1,ln)=0;drawmap(A)case2%下移A(rm+1,ln)=4;A(rm-1,ln)=0;drawmap(A)endelse%只能上移A(rn-1,ln)=4;A(rn+1,ln)=0;drawmap(A)endelseifrm5&A(rm+1,ln)==0A(rm+1,ln)=4;A(rm-1,ln)=0;drawmap(A)endcase5%赵云[I,J]=find(A==5);rm=max(I);rn=min(I);lm=max(J);ln=min(J);%判断是否能向左移ifln1&isequalm(A([rn,rm],ln-1),[0;0])A([rn,rm],ln-1)=[5;5];A([rn,rm],lm)=[0;0];drawmap(A)end%判断是否能向右移iflm4&isequalm(A([rn,rm],lm+1),[0;0])A([rn,rm],lm+1)=[5;5];A([rn,rm],ln)=[0;0];drawmap(A)endifrn1&A(rn-1,ln)==0ifrm5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位ch=menu('请选择移到的方向:','上','下')switchchcase1%上移A(rn-1,ln)=5;A(rn+1,ln)=0;drawmap(A)case2%下移A(rm+1,ln)=5;A(rm-1,ln)=0;drawmap(A)endelse%只能上移A(rn-1,ln)=5;A(rn+1,ln)=0;drawmap(A)endelseifrm5&A(rm+1,ln)==0A(rm+1,ln)=5;A(rm-1,ln)=0;drawmap(A)endcase6%关羽[I,J]=find(A==6);rm=max(I);rn=min(I);lm=max(J);ln=min(J);%判断是否能向上移ifrn1&isequalm(A(rn-1,[ln,lm]),[0,0])A(rn-1,[ln,lm])=[6,6];A(rn,[ln,lm])=[0,0];drawmap(A)end%判断是否能向下移ifrm5&isequalm(A(rm+1,[ln,lm]),[0,0])A(rm+1,[ln,lm])=[6,6];A(rm,[ln,lm])=[0,0];drawmap(A)endifl

1 / 43
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功