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

利用Matlab制作一款3D版2048小游戲

 更新時間:2022年02月25日 11:29:04   作者:slandarer  
2048作為一款經(jīng)典的小游戲,相信很多人都玩過吧?但是3D版的2048不知道有沒有人玩過呢?本文將用Matlab制作一個3D版的2048小游戲,快跟隨小編一起動手試一試吧

其實(shí)邏輯和2維版本完全一樣,就不進(jìn)行詳細(xì)解說了,直接看效果:

效果:

目前界面還不咋好看,期待大家的優(yōu)化

還是鍵盤↑↓←→操作嗷

完整代碼:

function game20483D
global squaremap
global colorlist
global fontsizelist
global baseX baseY baseZ
global barHdl textHdl
global txtBest txtScore
global best


fig=figure('units','pixels');
fig.Position=[560 50 575,400];
fig.Color=[0.9804 0.9725 0.9373];
fig.NumberTitle='off';
fig.Name='2048Game3D';
fig.MenuBar='none';
fig.Resize='off';
fig.KeyPressFcn=@key;
%
ax=axes(fig);
hold(ax,'on');
ax.Position=[0.1 0 1 1];
ax.ZLim=[0,17];
ax.XLim=[0,4]+0.5;
ax.YLim=[0,4]+0.5;
ax.View=[60   30];
fill([0 4 4 0]+0.5,[0 0 4 4]+0.5,[0.7333 0.6784 0.6275],'EdgeColor','none');
ax.Color=[0.8039 0.7569 0.7059].*1.02;
ax.XTick=[];
ax.YTick=[];
ax.ZTick=[];
ax.Box='on';
ax.LineWidth=3;
ax.XColor=[0.7333 0.6784 0.6275];
ax.YColor=[0.7333 0.6784 0.6275];
ax.ZColor=[0.7333 0.6784 0.6275];
% for i=1:4
%     for j=1:4
%         fill((i-1)+0.5+[.1 .8 .8 .1],(j-1)+0.5+[.1 .1 .8 .8],...
%             [0.8039 0.7569 0.7059],'EdgeColor','none');
%         
%     end
% end
% ==========================================================================
% 方塊顏色表
colorlist=[ 0.8039    0.7569    0.7059
    0.9333    0.8941    0.8549
    0.9373    0.8784    0.8039
    0.9608    0.6863    0.4824
    0.9529    0.5922    0.4078
    0.9529    0.4902    0.3725
    0.9686    0.3686    0.2431
    0.9255    0.8118    0.4510
    0.9373    0.7882    0.3922
    0.9333    0.7804    0.3216
    0.9216    0.7686    0.2627
    0.9255    0.7608    0.1804
    0.9412    0.4078    0.4157
    0.9216    0.3137    0.3451
    0.9451    0.2549    0.2627
    0.4392    0.7020    0.8157
    0.3765    0.6353    0.8745
    0.0902    0.5098    0.7843];
% 數(shù)字大小表
fontsizelist=[18 24 24 24 24 24 24 24 24 24 22 22 22 22 20 20 20 16].*0.8;
% 立方體數(shù)據(jù)
baseX=[0 1 1 0 0 0;1 1 0 0 1 1;1 1 0 0 1 1;0 1 1 0 0 0].*0.7-0.35;
baseY=[0 0 1 0 0 0;0 1 1 1 0 0;0 1 1 1 1 1;0 0 1 0 1 1].*0.7-0.35;
baseZ=[0 0 0 0 0 1;0 0 0 0 0 1;1 1 1 1 0 1;1 1 1 1 0 1];

text(-0.6,0.75,17,'2048-3D GAME','HorizontalAlignment','left','Color',...
    [0.4667 0.4314 0.3961],'FontSize',15,'FontWeight','bold')
text(-0.8,0.75,-7,' BEST  ','HorizontalAlignment','left','Color',...
    [0.9333 0.8941 0.8549],'FontSize',14,'FontWeight','bold','BackgroundColor',[0.7333 0.6784 0.6275])
text(-0.8,0.75,-10,'SCORE','HorizontalAlignment','left','Color',...
    [0.9333 0.8941 0.8549],'FontSize',14,'FontWeight','bold','BackgroundColor',[0.7333 0.6784 0.6275])
txtBest=text(0.4,0.9,-4.7,'0','HorizontalAlignment','left','Color',...
    [0.4667 0.4314 0.3961],'FontSize',14,'FontWeight','bold');
txtScore=text(0.4,0.9,-7.7,'0','HorizontalAlignment','left','Color',...
    [0.4667 0.4314 0.3961],'FontSize',14,'FontWeight','bold');
% ==========================================================================


%按鍵函數(shù),通過moveevent調(diào)整矩陣
    function key(~,event)
        temp_map=squaremap;
        switch event.Key
            case 'uparrow'
                temp_map=moveevent(temp_map');
                temp_map=temp_map';
            case 'downarrow'
                temp_map=temp_map';
                temp_map=moveevent(temp_map(:,4:-1:1));
                temp_map=temp_map(:,4:-1:1);
                temp_map=temp_map';
            case 'rightarrow'
                temp_map=moveevent(temp_map(:,4:-1:1));
                temp_map=temp_map(:,4:-1:1);
            case 'leftarrow'
                temp_map=moveevent(temp_map);
        end
        score=sum(sum(squaremap));
        best=max([best,score]);
        save best.mat best -append
        
        %若新矩陣與原矩陣不同,則重新繪制方塊
        if any(any(squaremap~=temp_map))
            squaremap=temp_map;
            createNewNum()
            drawBlock()
        end
    end

    %主函數(shù)
    function temp_matrix=moveevent(temp_matrix)
        for ii = 1: 4
            temp_array=temp_matrix(ii,:);
            temp_array(temp_array==0)=[];

            for jj = 1: (length(temp_array)-1)
                if temp_array(jj)==temp_array(jj+1)
                    temp_array(jj)=temp_array(jj)+temp_array(jj+1);
                    temp_array(jj+1)=0;
                end
            end

            temp_array(temp_array==0)=[];
            temp_array((length(temp_array)+1):4)=0;
            temp_matrix(ii,:)=temp_array;
        end
    end
% =========================================================================
for i=1:4
    for j=1:4
        barHdl{i,j}=fill3(baseX+i,baseY+j,baseZ,'y','EdgeColor',[0.7333 0.6784 0.6275].*0.3);
        textHdl{i,j}=text(i,j,1.5,'0','Color',[0.7333 0.6784 0.6275].*0.4,...
            'FontWeight','bold','HorizontalAlignment','center');
    end
end
init()

    function init()
        %若沒有游戲記錄則最高分為0
        if ~exist('best.mat')
            best=0;
            save best.mat best;
        end
        data=load('best.mat');
        best=data.best;
        txtBest.String=num2str(best);
        
        squaremap=zeros(4,4);
        createNewNum()
        createNewNum()
        drawBlock()
    end


    function drawBlock(~,~)
        score=sum(sum(squaremap));
        txtScore.String=num2str(score);
        hmap=log(squaremap)/log(2);
        hmap(isinf(hmap))=0;
        for ii=1:4
            for jj=1:4
                tNum=squaremap(ii,jj);
                tH=hmap(ii,jj);
                for kk=1:6
                    tZ=barHdl{ii,jj}(kk).ZData;tZ(tZ>0)=tH+0.01;
                    barHdl{ii,jj}(kk).ZData=tZ;
                    barHdl{ii,jj}(kk).FaceColor=colorlist(tH+1,:);
                    if tNum~=0
                        barHdl{ii,jj}(kk).EdgeColor=[0.7333 0.6784 0.6275].*0.3;
                    else
                        barHdl{ii,jj}(kk).EdgeColor=[0.7333 0.6784 0.6275];
                    end
                end
                if tNum~=0
                    textHdl{ii,jj}.Position(3)=tH+1;
                    textHdl{ii,jj}.FontSize=fontsizelist(tH+1);
                    textHdl{ii,jj}.String=num2str(tNum);        
                else
                    textHdl{ii,jj}.String='';   
                end
            end
        end
        judge()
    end

% 在矩陣空白處創(chuàng)建新的數(shù)字2或4
    function createNewNum(~,~)
        zerospos=find(squaremap==0);
        temp_pos=zerospos(randi(length(zerospos)));
        temp_num=randi(2)*2;
        squaremap(temp_pos)=temp_num;
    end

% 判斷游戲結(jié)束函數(shù)
    function judge(~,~)
        temp_judge_zeros=sum(sum(squaremap==0));
        temp_judge_row=any(any(squaremap(1:3,:)==squaremap(2:4,:)));
        temp_judge_col=any(any(squaremap(:,1:3)==squaremap(:,2:4)));
        if temp_judge_row+temp_judge_col+temp_judge_zeros==0
            gameOver()
        end
    end

% gameOver
    function gameOver(~,~)
        answer = questdlg('GAME OVER, what would you like to do', ...
            '2048-3D-GAME', ...
            'restart','quit','restart');
        % Handle response
        switch answer
            case 'restart'
                init()
            case 'quit'
                close all
                clear
        end
    end
end

以上就是利用Matlab制作一款3D版2048小游戲的詳細(xì)內(nèi)容,更多關(guān)于Matlab 2048游戲的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C++ 編寫DLL文件給易語言調(diào)用方法

    C++ 編寫DLL文件給易語言調(diào)用方法

    在本文中我們給大家分享了C++ 編寫DLL文件給易語言調(diào)用的代碼和方法,需要的朋友們學(xué)習(xí)下。
    2019-01-01
  • 淺析C語言中的內(nèi)存布局

    淺析C語言中的內(nèi)存布局

    以下是對C語言中的內(nèi)存布局進(jìn)行了詳細(xì)的分析介紹。需要的朋友可以過來參考下
    2013-08-08
  • C++中remove與erase區(qū)別小結(jié)

    C++中remove與erase區(qū)別小結(jié)

    remove函數(shù)和 erase函數(shù)都可以實(shí)現(xiàn)元素的刪除,本文主要介紹了C++中remove與erase區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • C++實(shí)現(xiàn)反轉(zhuǎn)鏈表的兩種方法

    C++實(shí)現(xiàn)反轉(zhuǎn)鏈表的兩種方法

    本文主要介紹了C++實(shí)現(xiàn)反轉(zhuǎn)鏈表的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • C++初始化數(shù)組的幾種常見方法(簡單易懂)

    C++初始化數(shù)組的幾種常見方法(簡單易懂)

    本文介紹了C++中數(shù)組的初始化方法,包括一維數(shù)組和二維數(shù)組的初始化,以及用new動態(tài)初始化數(shù)組,在C++11及以上版本中,還提供了使用std::array和std::vector進(jìn)行靜態(tài)和動態(tài)初始化的方式,需要的朋友可以參考下
    2025-02-02
  • 使用Libmicrohttpd搭建內(nèi)嵌(本地)服務(wù)器的方法

    使用Libmicrohttpd搭建內(nèi)嵌(本地)服務(wù)器的方法

    下面小編就為大家?guī)硪黄褂肔ibmicrohttpd搭建內(nèi)嵌(本地)服務(wù)器的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • C語言軟件spi虛擬總線中間層設(shè)計(jì)詳解

    C語言軟件spi虛擬總線中間層設(shè)計(jì)詳解

    這篇文章主要為大家介紹了C語言軟件spi虛擬總線中間層設(shè)計(jì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • C語言的數(shù)字游戲算法效率問題探討實(shí)例

    C語言的數(shù)字游戲算法效率問題探討實(shí)例

    這篇文章主要介紹了C語言的數(shù)字游戲算法效率問題探討實(shí)例,需要的朋友可以參考下
    2014-04-04
  • C++命名空間實(shí)例解析

    C++命名空間實(shí)例解析

    這篇文章主要介紹了C++命名空間實(shí)例解析,對C++程序員來說是非常重要的知識點(diǎn),需要的朋友可以參考下
    2014-08-08
  • C語言數(shù)據(jù)結(jié)構(gòu)之?dāng)U展字符詳解

    C語言數(shù)據(jù)結(jié)構(gòu)之?dāng)U展字符詳解

    掌握C語言數(shù)據(jù)結(jié)構(gòu)的關(guān)鍵在于理解其核心概念,擴(kuò)展字符作為其中的重要一環(huán),對于編程人員來說至關(guān)重要,本指南將為您深入剖析擴(kuò)展字符的相關(guān)知識,帶您輕松掌握C語言數(shù)據(jù)結(jié)構(gòu),讓我們一起探索這個令人著迷的領(lǐng)域吧!
    2024-03-03

最新評論

永年县| 常山县| 新田县| 会宁县| 班戈县| 囊谦县| 仙桃市| 萝北县| 湘阴县| 岳普湖县| 玉田县| 乌拉特中旗| 虎林市| 辽宁省| 八宿县| 遂昌县| 涞水县| 双柏县| 永德县| 玛多县| 萨嘎县| 汝阳县| 来安县| 全南县| 石屏县| 三穗县| 西城区| 富锦市| 华宁县| 自治县| 文山县| 兴业县| 米泉市| 山东| 农安县| 石泉县| 辽阳县| 房山区| 孝昌县| 习水县| 湖口县|