如何用Matlab制作一款简单的龙舟小游戏
如何用Matlab制作一款简单的龙舟小游戏
今天小编给大家分享一下如何用Matlab制作一款简单的龙舟小游戏的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
效果图:
步骤
1 创建Axes及图片导入
窗口创建:
Mainfig=figure('units','pixels','position',[50100760400],...'Numbertitle','off','menubar','none','resize','off',...'name','dragonBoat');axes('parent',Mainfig,'position',[0011],...'XLim',[0760],...'YLim',[0400],...'NextPlot','add',...'layer','bottom',...'Visible','on',...'YDir','reverse',...'XTick',[],...'YTick',[]);
图片导入:
[bkg_C,~,~]=imread('river.png');[boat_C,~,boat_Alp]=imread('boat.png');[stone_C,~,stone_Alp]=imread('stone.png');
图片素材可以从这里拿哟
2 创建timer函数移动背景
DrawBkgHdl=image([0760],[0400],bkg_C);t=0;tempBkg_C=[bkg_C,bkg_C];fps=20;game=timer('ExecutionMode','FixedRate','Period',1/fps,'TimerFcn',@dragongame);start(game)functiondragongame(~,~)t=t+6;modt=mod(t,720);newBkg_C=tempBkg_C(:,1+modt:684+modt,:);set(DrawBkgHdl,'CData',newBkg_C)end
3 绘制石块并移动
其实是5个石块来回变位置,当一个石块位置减小到负数就把数值增加并重新绘制
stonePos=[600;870;1140;1410];stonePos=[stonePos,randi([90,330],[4,1])];fori=1:size(stonePos,1)drawStoneHdl(i)=image([stonePos(i,1)-39stonePos(i,1)+39],[stonePos(i,2)-20stonePos(i,2)+20],stone_C,'AlphaData',stone_Alp);endfunctiondragongame(~,~)%这里是之前写的背景部分代码%。。。。。。。。。。。%。。。。。。。。。。。stonePos(:,1)=stonePos(:,1)-20/3;stonePos(stonePos(:,1)<0,2)=randi([90,330],[1,1]);stonePos(stonePos(:,1)<0,1)=stonePos(stonePos(:,1)<0,1)+1080;forii=1:size(stonePos,1)set(drawStoneHdl(ii),'XData',[stonePos(ii,1)-39stonePos(ii,1)+39],...'YData',[stonePos(ii,2)-20stonePos(ii,2)+20]);endend
4 绘制船并创建鼠标回调
boatPos=[380,200];DrawBoatHdl=image([boatPos(1)-75boatPos(1)+75],[boatPos(2)-50boatPos(2)+50],boat_C,'AlphaData',boat_Alp);set(gcf,'WindowButtonMotionFcn',@moveBoat,'tag','mov')functionmoveBoat(~,~)xy=get(gca,'CurrentPoint');temp_y=xy(1,2);temp_y(temp_y<100)=90;temp_y(temp_y>340)=330;boatPos=[380,temp_y];set(DrawBoatHdl,'YData',[temp_y-50temp_y+50]);end
5 碰撞判断函数
functionflag=judge(Bpos,Spos)flag1=abs(Bpos(1)-Spos(:,1))<80;flag2=abs((Bpos(2)+35)-Spos(:,2))<30;flag3=flag1&flag2;flag=any(flag3);end
dragongame函数做如下改写
functiondragongame(~,~)%这里是之前一大堆代码%。。。。。。。。。。%。。。。。。。。。。%。。。。。。。。。。ifjudge(boatPos,stonePos)stop(game)set(gcf,'WindowButtonMotionFcn',[]);text(50,200,'游戏结束','FontSize',54,'Color','w','tag','txt')endend
6 完整代码
functiondragonBoatMainfig=figure('units','pixels','position',[50100760400],...'Numbertitle','off','menubar','none','resize','off',...'name','dragonBoat');axes('parent',Mainfig,'position',[0011],...'XLim',[0760],...'YLim',[0400],...'NextPlot','add',...'layer','bottom',...'Visible','on',...'YDir','reverse',...'XTick',[],...'YTick',[]);[bkg_C,~,~]=imread('river.png');[boat_C,~,boat_Alp]=imread('boat.png');[stone_C,~,stone_Alp]=imread('stone.png');DrawBkgHdl=image([0760],[0400],bkg_C);stonePos=[600;870;1140;1410];stonePos=[stonePos,randi([90,330],[4,1])];fori=1:size(stonePos,1)drawStoneHdl(i)=image([stonePos(i,1)-39stonePos(i,1)+39],[stonePos(i,2)-20stonePos(i,2)+20],stone_C,'AlphaData',stone_Alp);endboatPos=[380,200];DrawBoatHdl=image([boatPos(1)-75boatPos(1)+75],[boatPos(2)-50boatPos(2)+50],boat_C,'AlphaData',boat_Alp);t=0;tempBkg_C=[bkg_C,bkg_C];fps=20;game=timer('ExecutionMode','FixedRate','Period',1/fps,'TimerFcn',@dragongame);start(game)text(10,20,['已前进',num2str(t),'米'],'FontSize',14,'Color','w','tag','txt');set(gcf,'WindowButtonMotionFcn',@moveBoat,'tag','mov')functiondragongame(~,~)t=t+6;modt=mod(t,720);newBkg_C=tempBkg_C(:,1+modt:684+modt,:);set(DrawBkgHdl,'CData',newBkg_C)stonePos(:,1)=stonePos(:,1)-20/3;stonePos(stonePos(:,1)<0,2)=randi([90,330],[1,1]);stonePos(stonePos(:,1)<0,1)=stonePos(stonePos(:,1)<0,1)+1080;forii=1:size(stonePos,1)set(drawStoneHdl(ii),'XData',[stonePos(ii,1)-39stonePos(ii,1)+39],...'YData',[stonePos(ii,2)-20stonePos(ii,2)+20]);endset(findobj('tag','txt'),'String',['已前进',num2str(t),'米']);ifjudge(boatPos,stonePos)stop(game)set(gcf,'WindowButtonMotionFcn',[]);text(50,200,'游戏结束','FontSize',54,'Color','w','tag','txt')endendfunctionmoveBoat(~,~)xy=get(gca,'CurrentPoint');temp_y=xy(1,2);temp_y(temp_y<100)=90;temp_y(temp_y>340)=330;boatPos=[380,temp_y];set(DrawBoatHdl,'YData',[temp_y-50temp_y+50]);endfunctionflag=judge(Bpos,Spos)flag1=abs(Bpos(1)-Spos(:,1))<80;flag2=abs((Bpos(2)+35)-Spos(:,2))<30;flag3=flag1&flag2;flag=any(flag3);endend
以上就是“如何用Matlab制作一款简单的龙舟小游戏”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注恰卡编程网行业资讯频道。