如何用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制作一款简单的龙舟小游戏”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注恰卡编程网行业资讯频道。

展开全文
内容来源于互联网和用户投稿,文章中一旦含有亚博电竞手机版的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系亚博电竞手机版删除

最新文章

网站地图