最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

教你用Matlab制作黃金礦工小游戲

 更新時(shí)間:2022年03月02日 14:32:41   作者:slandarer  
黃金礦工作為經(jīng)典的單機(jī)小游戲,一直深受大家的喜愛。本文將用Matlab制作這一款經(jīng)典的游戲,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的可以了解一下

效果

步驟

圖片準(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)文章

  • 用C++實(shí)現(xiàn)推箱子小游戲

    用C++實(shí)現(xiàn)推箱子小游戲

    這篇文章主要為大家詳細(xì)介紹了用C++實(shí)現(xiàn)推箱子小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • STL 的string類怎么啦

    STL 的string類怎么啦

    在我們研究string類犯了什么毛病之前,還讓我先說一下如何了解一個(gè)C++的類。我們要了解一個(gè)C++的類,一般來說,要從三個(gè)方面入手
    2013-11-11
  • C語言實(shí)現(xiàn)猜數(shù)字游戲的兩種方法

    C語言實(shí)現(xiàn)猜數(shù)字游戲的兩種方法

    猜數(shù)字小游戲是我們大多數(shù)人學(xué)習(xí)C語言時(shí)都會(huì)了解到的一個(gè)有趣的C語言小游戲,本文就詳細(xì)的介紹一下,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C語言如何在字符數(shù)組中插入一個(gè)字符

    C語言如何在字符數(shù)組中插入一個(gè)字符

    這篇文章主要介紹了C語言如何在字符數(shù)組中插入一個(gè)字符,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 淺談C++類型轉(zhuǎn)換幾種情況

    淺談C++類型轉(zhuǎn)換幾種情況

    本文主要介紹了幾種C++類型轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 詳解C++中的一維數(shù)組和二維數(shù)組

    詳解C++中的一維數(shù)組和二維數(shù)組

    這篇文章主要介紹了詳解C++中的一維數(shù)組和二維數(shù)組,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • C語言實(shí)現(xiàn)大數(shù)值金額大寫轉(zhuǎn)換的方法詳解

    C語言實(shí)現(xiàn)大數(shù)值金額大寫轉(zhuǎn)換的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C語言實(shí)現(xiàn)大數(shù)值金額大寫轉(zhuǎn)換的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-03-03
  • 基于Qt實(shí)現(xiàn)圖片播放器的示例代碼

    基于Qt實(shí)現(xiàn)圖片播放器的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何使用qt制作了一個(gè)簡單的圖片播放器,可以播放gif、png等格式圖片。文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-12-12
  • C++中靜態(tài)數(shù)據(jù)成員使用示例

    C++中靜態(tài)數(shù)據(jù)成員使用示例

    大家好,本篇文章主要講的是C++中靜態(tài)數(shù)據(jù)成員使用示例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • 詳解C++ 引用

    詳解C++ 引用

    這篇文章主要介紹了C++ 引用的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07

最新評論

合江县| 察雅县| 水富县| 恩平市| 东城区| 台山市| 文水县| 吉木萨尔县| 酉阳| 长垣县| 甘肃省| 上高县| 丹寨县| 娄烦县| 许昌市| 乐山市| 德阳市| 贡觉县| 渭源县| 潞城市| 南京市| 乌兰察布市| 蒲城县| 青浦区| 黄浦区| 平顶山市| 桂东县| 五莲县| 呼和浩特市| 韶关市| 兴仁县| 波密县| 墨竹工卡县| 广安市| 菏泽市| 财经| 平泉县| 乌苏市| 岚皋县| 洪江市| 瓦房店市|