教你用Matlab制作黃金礦工小游戲
效果
步驟
圖片準(zhǔn)備
背景構(gòu)建
function goldMiner Mainfig=figure('units','pixels','position',[50 100 750 500],... 'Numbertitle','off','menubar','none','resize','off',... 'name','goldMiner'); axes('parent',Mainfig,'position',[0 0 1 1],... 'XLim', [0 750],... 'YLim', [0 500],... 'NextPlot','add',... 'layer','bottom',... 'Visible','on',... 'YDir','reverse',... 'XTick',[], ... 'YTick',[]); bkgPic=imread('.\pic\bkg.png'); image([0,750],[0,500],bkgPic) [manPic,~,manAlp]=imread('.\pic\man.png'); image([400-60,400+60],[49.5-45,49.5+45],manPic,'AlphaData',manAlp)
繪制爪子
由于爪子要不斷調(diào)整角度因此用surface格式繪制,我們需要將爪子圖片矩陣范圍調(diào)整至[0,1],并將透明處數(shù)值調(diào)成nan
[clawPic,~,clawAlp]=imread('.\Pic\claw.png'); clawPic=double(clawPic)./255; clawPicR=clawPic(:,:,1); clawPicG=clawPic(:,:,2); clawPicB=clawPic(:,:,3); clawPicR(clawAlp<1)=nan; clawPicG(clawAlp<1)=nan; clawPicB(clawAlp<1)=nan; clawPic(:,:,1)=clawPicR; clawPic(:,:,2)=clawPicG; clawPic(:,:,3)=clawPicB; clawPos=[380,75]; ropePos=[380,75]; [xgrid,ygrid]=meshgrid((1:size(clawAlp,2))./2,(1:size(clawAlp,1))./2); xgrid=xgrid-size(clawAlp,2)/4; thetaList=linspace(-2*pi/5,2*pi/5,50); thetaIndex=1; theta=thetaList(thetaIndex);%當(dāng)前爪子轉(zhuǎn)動(dòng)角度 v=0;%爪子下移速度 dir=1;%1或-1爪子轉(zhuǎn)動(dòng)方向 grabbing=false;%是否正在抓取石塊 cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; drawClawHdl=surface(rotateX+clawPos(1),rotateY+clawPos(2),... zeros(size(clawAlp)),clawPic,... 'EdgeColor','none'); drawLineHdl=plot([clawPos(1),ropePos(1)],[clawPos(2),ropePos(2)],'k','LineWidth',2);
讓爪子轉(zhuǎn)起來
爪子旋轉(zhuǎn)就是單純的使用旋轉(zhuǎn)矩陣:
fps=20; game=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @minergame); start(game) function minergame(~,~) if ~grabbing switch 1 case thetaIndex==1,dir=1; case thetaIndex==50,dir=-1; end thetaIndex=thetaIndex+dir; theta=thetaList(thetaIndex); cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; else end end
繪制石塊
stoneName={'gold','gold','stone1','stone2','diamond'}; stonePic{length(stoneName)}=[]; stoneAlp{length(stoneName)}=[]; for i=1:length(stoneName) [C,~,Alp]=imread(['.\pic\',stoneName{i},'.png']); stonePic{i}=C; stoneAlp{i}=Alp; end stoneV=[-2,-3,-3,-3,-5];%拿起石頭后爪子移動(dòng)速度 stonePrice=[800,500,200,100,1000]; stoneSize=[50,50;30,30;24,20;15,12;8,8]; stonePos=[200,300;400,350;500,200;50,240;50,300; 700,420;170,180]; stoneType=[1,2,3,4,5,1,2]; stoneTag=1:length(stoneType); stoneXrange=[stonePos(:,1)-stoneSize(stoneType',1),stonePos(:,1)+stoneSize(stoneType',1)]; stoneYrange=[stonePos(:,2)-stoneSize(stoneType',2),stonePos(:,2)+stoneSize(stoneType',2)]; for i=1:length(stoneTag) drawStone(stonePos(i,:),stoneType(i),stoneTag(i)) end function drawStone(pos,i,j) image([-stoneSize(i,1),stoneSize(i,1)]+pos(1),... [-stoneSize(i,2),stoneSize(i,2)]+pos(2),... stonePic{i},... 'AlphaData',stoneAlp{i},... 'UserData',j) end
點(diǎn)擊下箭頭移動(dòng)爪子
set(gcf, 'KeyPressFcn', @key) function key(~,event) switch event.Key case 'downarrow' grabbing=true;v=4; end end function minergame(~,~) if ~grabbing %這里是讓爪子轉(zhuǎn)動(dòng)的一堆代碼 %。。。。。。。。。。。。。 %。。。。。。。。。。。。。 else cost=cos(theta); sint=sin(theta); clawPos=clawPos+[sint,cost].*v; end set(drawClawHdl,'XData',rotateX+clawPos(1),'YData',rotateY+clawPos(2)); set(drawLineHdl,'XData',[clawPos(1),ropePos(1)],'YData',[clawPos(2),ropePos(2)]); end
爪子與石頭和邊緣碰觸判斷
function n=touchThing(clawPos) n=0; if clawPos(1)<20||clawPos(1)>730||clawPos(2)>480 n=-1; end flagX=clawPos(1)>=stoneXrange(:,1)&clawPos(1)<=stoneXrange(:,2); flagY=clawPos(2)>=stoneYrange(:,1)&clawPos(2)<=stoneYrange(:,2); flagXY=flagX&flagY; if any(flagXY) n=find(flagXY); end end
抓取石塊和顯示金錢
holdOnType=0; drawHoldOnHdl=image([0,1],[0,1],ones(1,1),'AlphaData',zeros(1,1)); text(10,40,'Money:','FontSize',20,'Color',[1 1 1],'FontName','Cambria','FontWeight','bold') money=0; moneyStrHdl=text(110,40,'$0','FontSize',20,'Color',[0.5137 0.7882 0.2157],'FontName','Cambria','FontWeight','bold'); function minergame(~,~) if ~grabbing switch 1 case thetaIndex==1,dir=1; case thetaIndex==50,dir=-1; end thetaIndex=thetaIndex+dir; theta=thetaList(thetaIndex); cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; else cost=cos(theta); sint=sin(theta); clawPos=clawPos+[sint,cost].*v; n=touchThing(clawPos+5.*[sint,cost]); if n==-1 v=-abs(v); elseif n>0 delete(findobj('UserData',stoneTag(n))); v=stoneV(stoneType(n)); holdOnType=stoneType(n); stonePos(n,:)=[]; stoneType(n)=[]; stoneTag(n)=[]; stoneXrange(n,:)=[]; stoneYrange(n,:)=[]; set(drawHoldOnHdl,... 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,... 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost,... 'CData',stonePic{holdOnType},'AlphaData',stoneAlp{holdOnType}); end if clawPos(2)<=ropePos(2) clawPos=ropePos; grabbing=false; if holdOnType>0 money=money+stonePrice(holdOnType); set(moneyStrHdl,'String',['$',num2str(money)]) end holdOnType=0; set(drawHoldOnHdl,'XData',[0,1],... 'YData',[0,1],... 'CData',ones(1,1),... 'AlphaData',zeros(1,1)); end if holdOnType~=0 set(drawHoldOnHdl,... 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,... 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost); end end set(drawClawHdl,'XData',rotateX+clawPos(1),'YData',rotateY+clawPos(2)); set(drawLineHdl,'XData',[clawPos(1),ropePos(1)],'YData',[clawPos(2),ropePos(2)]); end
完整代碼
function goldMiner Mainfig=figure('units','pixels','position',[50 100 750 500],... 'Numbertitle','off','menubar','none','resize','off',... 'name','goldMiner'); axes('parent',Mainfig,'position',[0 0 1 1],... 'XLim', [0 750],... 'YLim', [0 500],... 'NextPlot','add',... 'layer','bottom',... 'Visible','on',... 'YDir','reverse',... 'XTick',[], ... 'YTick',[]); bkgPic=imread('.\pic\bkg.png'); image([0,750],[0,500],bkgPic) [manPic,~,manAlp]=imread('.\pic\man.png'); image([400-60,400+60],[49.5-45,49.5+45],manPic,'AlphaData',manAlp) [clawPic,~,clawAlp]=imread('.\Pic\claw.png'); clawPic=double(clawPic)./255; clawPicR=clawPic(:,:,1); clawPicG=clawPic(:,:,2); clawPicB=clawPic(:,:,3); clawPicR(clawAlp<1)=nan; clawPicG(clawAlp<1)=nan; clawPicB(clawAlp<1)=nan; clawPic(:,:,1)=clawPicR; clawPic(:,:,2)=clawPicG; clawPic(:,:,3)=clawPicB; clawPos=[380,75]; ropePos=[380,75]; [xgrid,ygrid]=meshgrid((1:size(clawAlp,2))./2,(1:size(clawAlp,1))./2); xgrid=xgrid-size(clawAlp,2)/4; thetaList=linspace(-2*pi/5,2*pi/5,50); thetaIndex=1; theta=thetaList(thetaIndex);v=0; dir=1;grabbing=false; cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; drawClawHdl=surface(rotateX+clawPos(1),rotateY+clawPos(2),... zeros(size(clawAlp)),clawPic,... 'EdgeColor','none'); drawLineHdl=plot([clawPos(1),ropePos(1)],[clawPos(2),ropePos(2)],'k','LineWidth',2); %stone part====================================================== stoneName={'gold','gold','stone1','stone2','diamond'}; stonePic{length(stoneName)}=[]; stoneAlp{length(stoneName)}=[]; for i=1:length(stoneName) [C,~,Alp]=imread(['.\pic\',stoneName{i},'.png']); stonePic{i}=C; stoneAlp{i}=Alp; end stoneV=[-2,-3,-3,-3,-5]; stonePrice=[800,500,200,100,1000]; stoneSize=[50,50;30,30;24,20;15,12;8,8]; stonePos=[200,300;400,350;500,200;50,240;50,300; 700,420;170,180]; stoneType=[1,2,3,4,5,1,2]; stoneTag=1:length(stoneType); stoneXrange=[stonePos(:,1)-stoneSize(stoneType',1),stonePos(:,1)+stoneSize(stoneType',1)]; stoneYrange=[stonePos(:,2)-stoneSize(stoneType',2),stonePos(:,2)+stoneSize(stoneType',2)]; for i=1:length(stoneTag) drawStone(stonePos(i,:),stoneType(i),stoneTag(i)) end function drawStone(pos,i,j) image([-stoneSize(i,1),stoneSize(i,1)]+pos(1),... [-stoneSize(i,2),stoneSize(i,2)]+pos(2),... stonePic{i},... 'AlphaData',stoneAlp{i},... 'UserData',j) end holdOnType=0; drawHoldOnHdl=image([0,1],[0,1],ones(1,1),'AlphaData',zeros(1,1)); text(10,40,'Money:','FontSize',20,'Color',[1 1 1],'FontName','Cambria','FontWeight','bold') money=0; moneyStrHdl=text(110,40,'$0','FontSize',20,'Color',[0.5137 0.7882 0.2157],'FontName','Cambria','FontWeight','bold'); %========================================================================== set(gcf, 'KeyPressFcn', @key) fps=20; game=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @minergame); start(game) function minergame(~,~) if ~grabbing switch 1 case thetaIndex==1,dir=1; case thetaIndex==50,dir=-1; end thetaIndex=thetaIndex+dir; theta=thetaList(thetaIndex); cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; else cost=cos(theta); sint=sin(theta); clawPos=clawPos+[sint,cost].*v; n=touchThing(clawPos+5.*[sint,cost]); if n==-1 v=-abs(v); elseif n>0 delete(findobj('UserData',stoneTag(n))); v=stoneV(stoneType(n)); holdOnType=stoneType(n); stonePos(n,:)=[]; stoneType(n)=[]; stoneTag(n)=[]; stoneXrange(n,:)=[]; stoneYrange(n,:)=[]; set(drawHoldOnHdl,... 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,... 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost,... 'CData',stonePic{holdOnType},'AlphaData',stoneAlp{holdOnType}); end if clawPos(2)<=ropePos(2) clawPos=ropePos; grabbing=false; if holdOnType>0 money=money+stonePrice(holdOnType); set(moneyStrHdl,'String',['$',num2str(money)]) end holdOnType=0; set(drawHoldOnHdl,'XData',[0,1],... 'YData',[0,1],... 'CData',ones(1,1),... 'AlphaData',zeros(1,1)); end if holdOnType~=0 set(drawHoldOnHdl,... 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,... 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost); end end set(drawClawHdl,'XData',rotateX+clawPos(1),'YData',rotateY+clawPos(2)); set(drawLineHdl,'XData',[clawPos(1),ropePos(1)],'YData',[clawPos(2),ropePos(2)]); end function n=touchThing(clawPos) n=0; if clawPos(1)<20||clawPos(1)>730||clawPos(2)>480 n=-1; end flagX=clawPos(1)>=stoneXrange(:,1)&clawPos(1)<=stoneXrange(:,2); flagY=clawPos(2)>=stoneYrange(:,1)&clawPos(2)<=stoneYrange(:,2); flagXY=flagX&flagY; if any(flagXY) n=find(flagXY); end end function key(~,event) switch event.Key case 'downarrow' grabbing=true;v=4; end end end
以上就是教你用Matlab制作黃金礦工小游戲的詳細(xì)內(nèi)容,更多關(guān)于Matlab黃金礦工游戲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
實(shí)例分享cmake編譯一個(gè)簡單c++項(xiàng)目(demo)
下面通過一個(gè)小例子來說明cmake編譯一個(gè)c++項(xiàng)目,生成可執(zhí)行文件,需要的朋友可以參考下2020-02-02求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
本篇文章是對求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05VC中CWinThread類以及和createthread API的區(qū)別分析
這篇文章主要介紹了VC中CWinThread類以及和createthread API的區(qū)別分析,較為詳細(xì)的講述了CWinThread類的原理,并以實(shí)例形式對AfxBeginThread函數(shù)的內(nèi)部實(shí)現(xiàn)進(jìn)行了解釋說明,需要的朋友可以參考下2014-10-10UE4 Unlua 調(diào)用異步藍(lán)圖節(jié)點(diǎn)AIMoveTo函數(shù)示例詳解
這篇文章主要為大家介紹了UE4 Unlua 調(diào)用AIMoveTo函數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09C語言課程設(shè)計(jì)之抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言課程設(shè)計(jì)之抽獎(jiǎng)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12C語言實(shí)現(xiàn)頁面置換算法(FIFO、LRU)
這篇文章主要介紹了通過C語言實(shí)現(xiàn)的兩種頁面置換算法:先進(jìn)先出(FIFO)頁面置換算法和最近最久未使用(LRU)頁面置換算法。文中的代碼具有一定的學(xué)習(xí)或工作價(jià)值,快來跟隨小編學(xué)習(xí)一下吧2021-12-12