找回密码
 立即注册
首页 业界区 业界 C语言版2048小游戏

C语言版2048小游戏

辖瑁地 3 小时前
一、游戏特点

2048是一款数字益智类游戏,玩家需要使用键盘控制数字方块的移动,合并相同数字的方块,最终达到数字方块上出现“2048”的目标。
为用C语言实现2048的小游戏项目,我们需先观察2048小游戏的特点和我们需要实现的一些功能:
1.需要一个棋盘来储存数字,用什么来表示棋盘?
2.需要控制数字的移动,怎样控制数字的移动?
3.需要在数字移动的过程中产生新的随机数,怎么生成新的随机数?
4.需要控制数字是否需要合成,怎么判断数字是否需要合成和怎么合成?
5.需要判断判定游戏是否成功,怎么判断游戏是否结束?
6.需要丰富游戏的界面,怎样使游戏界面好看一点?
二、游戏运行示意图

1.png

三、工具准备

1.代码运行平台:Visual Studio 2022
官网地址:https://visualstudio.microsoft.com/zh-hans/
2.图形库:Easyx
官网地址:https://easyx.cn/
四、代码实现流程

一、核心逻辑

1、棋盘制作
用二维数组来存储数字,制作与显示棋盘
  1. //用整型数组表示矩阵方格
  2. int arr[4][4] = { 0 };
  3. //打印矩阵方格的数据
  4. void PrintArr()
  5. {
  6.         for (int row = 0; row < 4; row++)
  7.         {
  8.                 for (int column = 0; column < 4; column++)
  9.                 {
  10.                         printf("%d\t", arr[row][column]);
  11.                 }
  12.                 printf("\n");
  13.         }
  14. }
复制代码
2、控制数字的移动与合成
  1. //向左边移动
  2. void Left()
  3. {
  4.         //遍历行
  5.         for (int row = 0; row < 4; row++)
  6.         {
  7.                 for (int column = 0; column < 4; column++)
  8.                 {
  9.             //如果当前格子为空,则向右移动
  10.                         if (arr[row][column] == 0)
  11.                         {
  12.                                 //从后面找一个不为空的格子,如果找到,则交换位置
  13.                                 for (int i = column + 1; i < 4; i++)
  14.                                 {
  15.                                         if (arr[row][i]!= 0)
  16.                                         {
  17.                                                 arr[row][column] = arr[row][i];
  18.                                                 arr[row][i] = 0;
  19.                                                 //处理一个非空位置后,条件不成立,不能往后走了
  20.                                                 break;
  21.                                         }
  22.                                 }
  23.                         }
  24.                         //如果不为0,表示要合并
  25.                         if (arr[row][column] != 0)
  26.                         {
  27.                                 //从后面找一个和当前数据相同的数据,遇到0就跳过,遇到不同的,退出
  28.                                 for (int i = column + 1; i < 4; i++)
  29.                                 {
  30.                                         if (arr[row][i] == arr[row][column])
  31.                                         {
  32.                                                 //数据合并
  33.                                                 arr[row][column] *= 2;
  34.                                                 arr[row][i] = 0;
  35.                                                 //合并完成就结束查找
  36.                                                 break;
  37.                                         }
  38.                                         else if (arr[row][i] == 0)
  39.                                                 continue;
  40.                                         else
  41.                                                 break;
  42.                                 }
  43.                         }
  44.                 }
  45.         }
  46.         CreateRandData(1);
  47. }
  48. //向右边移动
  49. void Right()
  50. {
  51.         for (int row = 0; row < 4; row++)
  52.         {
  53.                 for (int column = 3; column >= 0; column--)
  54.                 {
  55.                         //如果当前格子为空,则向左移动
  56.                         if (arr[row][column] == 0)
  57.                         {
  58.                                 //从后面找一个不为空的格子,如果找到,则交换位置
  59.                                 for (int i = column - 1; i >= 0; i--)
  60.                                 {
  61.                                         if (arr[row][i] != 0)
  62.                                         {
  63.                                                 arr[row][column] = arr[row][i];
  64.                                                 arr[row][i] = 0;
  65.                                                 //处理一个非空位置后,条件不成立,不能往后走了
  66.                                                 break;
  67.                                         }
  68.                                 }
  69.                         }
  70.                         //如果不为0,表示要合并
  71.                         if (arr[row][column] != 0)
  72.                         {
  73.                                 //从后面找一个和当前数据相同的数据,遇到0就跳过,遇到不同的,退出
  74.                                 for (int i = column - 1; i >=0; i--)
  75.                                 {
  76.                                         if (arr[row][i] == arr[row][column])
  77.                                         {
  78.                                                 //数据合并
  79.                                                 arr[row][column] *= 2;
  80.                                                 arr[row][i] = 0;
  81.                                                 //合并完成就结束查找
  82.                                                 break;
  83.                                         }
  84.                                         else if (arr[row][i] == 0)
  85.                                                 continue;
  86.                                         else
  87.                                                 break;
  88.                                 }
  89.                         }
  90.                 }
  91.         }
  92.         CreateRandData(1);
  93. }
  94. //向上边移动
  95. void Up()
  96. {
  97.         for (int column = 0; column < 4; column++)
  98.         {
  99.                 for (int row = 0; row < 4; row++)
  100.                 {
  101.                         //如果当前格子为空,则向下移动
  102.                         if (arr[row][column] == 0)
  103.                         {
  104.                                 //从后面找一个不为空的格子,如果找到,则交换位置
  105.                                 for (int i = row + 1; i < 4; i++)
  106.                                 {
  107.                                         if (arr[i][column] != 0)
  108.                                         {
  109.                                                 arr[row][column] = arr[i][column];
  110.                                                 arr[i][column] = 0;
  111.                                                 //处理一个非空位置后,条件不成立,不能往后走了
  112.                                                 break;
  113.                                         }
  114.                                 }
  115.                         }
  116.                         //如果不为0,表示要合并
  117.                         if (arr[row][column] != 0)
  118.                         {
  119.                                 //从后面找一个和当前数据相同的数据,遇到0就跳过,遇到不同的,退出
  120.                                 for (int i = row + 1; i < 4; i++)
  121.                                 {
  122.                                         if (arr[i][column] == arr[row][column])
  123.                                         {
  124.                                                 //数据合并
  125.                                                 arr[row][column] *= 2;
  126.                                                 arr[i][column] = 0;
  127.                                                 //合并完成就结束查找
  128.                                                 break;
  129.                                         }
  130.                                         else if (arr[i][column] == 0)
  131.                                                 continue;
  132.                                         else
  133.                                                 break;
  134.                                 }
  135.                         }
  136.                 }
  137.         }
  138.         CreateRandData(1);
  139. }
  140. //向下边移动
  141. void Down()
  142. {
  143.         for (int column = 0; column < 4; column++)
  144.         {
  145.                 for (int row = 3; row >= 0; row--)
  146.                 {
  147.                         //如果当前格子为空,则向上移动
  148.                         if (arr[row][column] == 0)
  149.                         {
  150.                                 //从后面找一个不为空的格子,如果找到,则交换位置
  151.                                 for (int i = row - 1; i >= 0; i--)
  152.                                 {
  153.                                         if (arr[i][column] != 0)
  154.                                         {
  155.                                                 arr[row][column] = arr[i][column];
  156.                                                 arr[i][column] = 0;
  157.                                                 //处理一个非空位置后,条件不成立,不能往后走了
  158.                                                 break;
  159.                                         }
  160.                                 }
  161.                         }
  162.                         //如果不为0,表示要合并
  163.                         if (arr[row][column] != 0)
  164.                         {
  165.                                 //从后面找一个和当前数据相同的数据,遇到0就跳过,遇到不同的,退出
  166.                                 for (int i = row - 1; i >= 0; i--)
  167.                                 {
  168.                                         if (arr[i][column] == arr[row][column])
  169.                                         {
  170.                                                 //数据合并
  171.                                                 arr[row][column] *= 2;
  172.                                                 arr[i][column] = 0;
  173.                                                 //合并完成就结束查找
  174.                                                 break;
  175.                                         }
  176.                                         else if (arr[i][column] == 0)
  177.                                                 continue;
  178.                                         else
  179.                                                 break;
  180.                                 }
  181.                         }
  182.                 }
  183.         }
  184.         CreateRandData(1);
  185. }
复制代码
3、判断是否可以合成
[code]//是否可以合成int CamMerge(){        for (int row = 0; row < 4; row++)        {                for (int column = 0; column < 4; column++)                {                        if (column+1

相关推荐

您需要登录后才可以回帖 登录 | 立即注册