基于Matlab制作一款简单的龙舟小游戏
效果图:
没找到合适的背景就自己画了个,大家如果有更好看的可以换一下。。。
步骤
1 创建Axes及图片导入
窗口创建:
Mainfig=figure("units","pixels","position",[50 100 760 400],... "Numbertitle","off","menubar","none","resize","off",... "name","dragonBoat"); axes("parent",Mainfig,"position",[0 0 1 1],... "XLim", [0 760],... "YLim", [0 400],... "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([0 760],[0 400],bkg_C); t=0; tempBkg_C=[bkg_C,bkg_C]; fps = 20; game = timer("ExecutionMode", "FixedRate", "Period",1/fps, "TimerFcn", @dragongame); start(game) function dragongame(~,~) 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])]; for i=1:size(stonePos,1) drawStoneHdl(i)=image([stonePos(i,1)-39 stonePos(i,1)+39],[stonePos(i,2)-20 stonePos(i,2)+20],stone_C,"AlphaData",stone_Alp); end function dragongame(~,~) %这里是之前写的背景部分代码 %。。。。。。。。。。。 %。。。。。。。。。。。 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; for ii=1:size(stonePos,1) set(drawStoneHdl(ii),"XData",[stonePos(ii,1)-39 stonePos(ii,1)+39],... "YData",[stonePos(ii,2)-20 stonePos(ii,2)+20]); end end
4 绘制船并创建鼠标回调
boatPos=[380,200]; DrawBoatHdl=image([boatPos(1)-75 boatPos(1)+75],[boatPos(2)-50 boatPos(2)+50],boat_C,"AlphaData",boat_Alp); set(gcf,"WindowButtonMotionFcn",@moveBoat,"tag","mov") function moveBoat(~,~) 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-50 temp_y+50]); end
5 碰撞判断函数
function flag=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函数做如下改写
function dragongame(~,~) %这里是之前一大堆代码 %。。。。。。。。。。 %。。。。。。。。。。 %。。。。。。。。。。 if judge(boatPos,stonePos) stop(game) set(gcf,"WindowButtonMotionFcn",[]); text(50,200,"游戏结束","FontSize",54,"Color","w","tag","txt") end end
6 完整代码
function dragonBoat Mainfig=figure("units","pixels","position",[50 100 760 400],... "Numbertitle","off","menubar","none","resize","off",... "name","dragonBoat"); axes("parent",Mainfig,"position",[0 0 1 1],... "XLim", [0 760],... "YLim", [0 400],... "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([0 760],[0 400],bkg_C); stonePos=[600;870;1140;1410]; stonePos=[stonePos,randi([90,330],[4,1])]; for i=1:size(stonePos,1) drawStoneHdl(i)=image([stonePos(i,1)-39 stonePos(i,1)+39],[stonePos(i,2)-20 stonePos(i,2)+20],stone_C,"AlphaData",stone_Alp); end boatPos=[380,200]; DrawBoatHdl=image([boatPos(1)-75 boatPos(1)+75],[boatPos(2)-50 boatPos(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") function dragongame(~,~) 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; for ii=1:size(stonePos,1) set(drawStoneHdl(ii),"XData",[stonePos(ii,1)-39 stonePos(ii,1)+39],... "YData",[stonePos(ii,2)-20 stonePos(ii,2)+20]); end set(findobj("tag","txt"),"String",["已前进",num2str(t),"米"]); if judge(boatPos,stonePos) stop(game) set(gcf,"WindowButtonMotionFcn",[]); text(50,200,"游戏结束","FontSize",54,"Color","w","tag","txt") end end function moveBoat(~,~) 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-50 temp_y+50]); end function flag=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 end
到此这篇关于基于Matlab制作一款简单的龙舟小游戏的文章就介绍到这了,更多相关Matlab龙舟游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?