服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 利用Matlab制作一款3D版2048小游戏

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

2022-10-07 16:18slandarer C/C++

2048作为一款经典的小游戏,相信很多人都玩过吧?但是3D版的2048不知道有没有人玩过呢?本文将用Matlab制作一个3D版的2048小游戏,快跟随小编一起动手试一试吧

其实逻辑和2维版本完全一样,就不进行详细解说了,直接看效果:

效果:

目前界面还不咋好看,期待大家的优化

还是键盘↑↓←→操作嗷

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

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

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

完整代码:

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];
% 数字大小表
fontsizelist=[18 24 24 24 24 24 24 24 24 24 22 22 22 22 20 20 20 16].*0.8;
% 立方体数据
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');
% ==========================================================================


%按键函数,通过moveevent调整矩阵
  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

  %主函数
  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

% 在矩阵空白处创建新的数字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

% 判断游戏结束函数
  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小游戏的详细内容,更多关于Matlab 2048游戏的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/slandarer/article/details/120353524

延伸 · 阅读

精彩推荐
  • C/C++C语言之直接插入排序算法的方法

    C语言之直接插入排序算法的方法

    这篇文章主要为大家介绍了C语言直接插入排序算法的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助...

    绅士·永5652022-07-24
  • C/C++C++中cin的用法详细

    C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来...

    大大大大大大旭11272021-08-06
  • C/C++浅析C语言中堆和栈的区别

    浅析C语言中堆和栈的区别

    堆和栈都是一种数据项按序排列的数据结构。在C语言中是非常重要的知识点,接下来通过本文给大家介绍C语言中堆和栈的区别,感兴趣的朋友一起看下吧...

    冀博10462021-04-07
  • C/C++C语言对堆排序一个算法思路和实现代码

    C语言对堆排序一个算法思路和实现代码

    这篇文章主要介绍了C语言对堆排序一个算法思路和实现代码,堆排序是一种树形选择排序,是对直接选择排序的有效改进,需要的朋友可以参考下...

    C语言程序设计4642021-01-20
  • C/C++C++实现模板方法模式的示例代码

    C++实现模板方法模式的示例代码

    这篇文章主要介绍了++实现模板方法模式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    鬼谷子com7222021-09-15
  • C/C++VC实现对话框窗口任意分割

    VC实现对话框窗口任意分割

    最近写MFC的程序,想在对话框里实现窗口的任意分割。现在网络资料一大抄,找个东西实在麻烦。总算这个很简单,很快就搞定了,写下来做个笔记。...

    C语言教程网9082021-02-28
  • C/C++C++读取带空格字符串的方法

    C++读取带空格字符串的方法

    今天小编就为大家分享一篇C++读取带空格字符串的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    good_luck5122336992021-06-27
  • C/C++运用指针在不用加号的情况进行加法运算的讲解

    运用指针在不用加号的情况进行加法运算的讲解

    今天小编就为大家分享一篇关于运用指针在不用加号的情况进行加法运算的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的...

    sdr_zd6532021-07-16