unity制作2048
来源:互联网 发布:unity3d 模型加载 编辑:程序博客网 时间:2024/05/17 02:23
前段时间迷上玩2048,有空就2048一下,终于皇天不负有心人啊。在经过无数次loser之后,终于赢了一把。
各人感觉这游戏做起来还是挺简单的,不过是否真如此,一句话光说不练假把式,那就练练看实际检验一番。
该游戏难点在于
1.如何让相邻的相等的数字相加
2.如何确定数字移动的方向
3.如何将移动方向上无数字的数字移动到边框
4.在随机没数字的地方产生新的数字
总体做法,是声明一个4X4的二维数组来保存数字,值为0的地方的表示没数字,值为2,4,6…的地方显示数字
这样就可以方便的判断两个相邻位置的值是否相等了,如果相等就相加,
如果不相等且为空则直接在移动方向上移动该数字(即将移动方向上的为0的值简单改为该数字即可)
至于移动方向的确定可以通过手指前后两次点击的位置的偏移量确定。
好吧,大概就这个样子。下
using UnityEngine;using System.Collections;//数字移动的方向public enum Direction{ N, U, D, L, R,}public class Game2048 : MonoBehaviour{ //数字的位置 private int xPos; private int yPos; //数字的尺寸 private int xCell; private int yCell; //当前数字的坐标 private int iCur; private int jCur; //显示结果的字符串 private string result; //定义一个四乘四的二维数组保存数字 private int[,] nums = new int[4, 4]; //手指触摸的前一个位置 private Vector3 prePos; //手机触摸的下一个位置 private Vector3 nexPos; //数字移动方向 private Direction movDir; //游戏是否结束 private bool isOver; //是否移动数字 private bool isMove; // Use this for initialization //初始化游戏格局 void Start() { xPos = 0; yPos = 0; xCell = Screen.width / 4; yCell = xCell; isMove = false; isOver = false; result = "November"; CreateOneNumber(); CreateOneNumber(); } // Update is called once per frame void Update() { //游戏结束 if (isOver) { result = "YouLose"; Time.timeScale = 0; } //检测手指划动方向确定数字移动方向 if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) { Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition; if (Mathf.Abs(touchDeltaPosition.x) > Mathf.Abs(touchDeltaPosition.y)) { if (touchDeltaPosition.x < 0) { movDir = Direction.L; isMove = true; } else { movDir = Direction.R; isMove = true; } } else { if (touchDeltaPosition.y < 0) { movDir = Direction.D; isMove = true; } else { movDir = Direction.U; isMove = true; } } } //PC controller #region if (Input.GetMouseButtonDown(0)) { prePos = Input.mousePosition; //Debug.Log(prePos); } if (Input.GetMouseButtonUp(0)) { nexPos = Input.mousePosition; //Debug.Log(nexPos); CreateOneNumber(); } if (Mathf.Abs((nexPos - prePos).x) > Mathf.Abs((nexPos - prePos).y)) { if (nexPos.x - prePos.x < 0) { movDir = Direction.L; isMove = true; } else { movDir = Direction.R; isMove = true; } } else { if (nexPos.y - prePos.y < 0) { movDir = Direction.D; isMove = true; } else { movDir = Direction.U; isMove = true; } } #endregion //移动数字 if (isMove) { MoveNumbers(); isMove = false; } //Debug.Log(movDir); } //利用GUI显示游戏 void OnGUI() { GUI.Box(new Rect(xPos, yPos, Screen.width, Screen.height), ""); GUI.Label(new Rect(Screen.width/2-40, Screen.width+100, 100, 100), result); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { GUI.Box(new Rect(xPos + i * xCell, yPos + j * yCell, xCell-5, yCell -5), ""); if (nums[i, j] == 0) { GUI.Label(new Rect(xPos + i * xCell, yPos + j * yCell, xCell-10, yCell-10), ""); } else { GUI.Label(new Rect(xPos + i * xCell, yPos + j * yCell, xCell - 10, yCell - 10), nums[i, j].ToString()); } } } } void CreateOneNumber() { //检测二维数组上是否都有数字 //其实应该多检测一步所有数字能否再加 bool find = false; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (nums[i, j] == 0) { find = true; } } } //Debug.Log(find); if (!find) { isOver = true; return; } //随机位置产生2或4 int nCur = 2 * Random.Range(1, 3); do { iCur = Random.Range(0, 4); jCur = Random.Range(0, 4); } while (nums[iCur, jCur] != 0); nums[iCur, jCur] = nCur; isMove = false; } void MoveNumbers() { switch (movDir) { case Direction.U: for (int i = 0; i < 4; i++) { for (int j = 1; j < 4; j++) { if (nums[i, j] != 0) { //上面一个没数字,直接移动 if (nums[i, j - 1] == 0) { nums[i, j - 1] = nums[i, j]; nums[i, j] = 0; } //上面一个有数字,如果相等就相加 else if (nums[i, j - 1] == nums[i, j]) { nums[i, j - 1] += nums[i, j]; nums[i, j] = 0; } } } } break; case Direction.L: for (int j = 0; j < 4; j++) { for (int i = 1; i < 4; i++) { if (nums[i, j] != 0) { if (nums[i - 1, j] == 0) { nums[i - 1, j] = nums[i, j]; nums[i, j] = 0; } else if (nums[i - 1, j] == nums[i, j]) { nums[i - 1, j] += nums[i, j]; nums[i, j] = 0; } } } } break; case Direction.D: for (int i = 0; i < 4; i++) { for (int j = 2; j >= 0; j--) { if (nums[i, j] != 0) { if (nums[i, j + 1] == 0) { nums[i, j + 1] = nums[i, j]; nums[i, j] = 0; } else if (nums[i, j + 1] == nums[i, j]) { nums[i, j + 1] += nums[i, j]; nums[i, j] = 0; } } } } break; case Direction.R: for (int j = 0; j < 4; j++) { for (int i = 2; i >= 0; i--) { if (nums[i, j] != 0) { if (nums[i + 1, j] == 0) { nums[i + 1, j] = nums[i, j]; nums[i, j] = 0; } else if (nums[i + 1, j] == nums[i, j]) { nums[i + 1, j] += nums[i, j]; nums[i, j] = 0; } } } } break; } }}
附代码,当然代码不是很全的,只是简单的实现了2048的主体功能,还不能完整的称为2048游戏
0 0
- unity制作2048
- Unity 游戏2048:制作总结
- 【Unity】unity 日历制作
- Unity 精简字库制作
- unity制作直方图效果
- Unity小地图制作
- unity编辑器制作(一)
- Unity小地图制作
- Unity制作血条
- Unity小地图制作
- Unity弹痕制作
- unity制作全息投影
- Unity制作Loading详解
- unity 初次制作
- Unity圆环进度条制作
- unity制作贝赛尔曲线
- Unity 艺术字体制作
- unity制作时钟
- Android Fragment完全解析,关于碎片你所需知道的一切
- CentOS 6.4安装配置LAMP服务器(Apache+PHP5+MySQL)
- leetcode substring without repeating characters
- IE访问HTTPS链接下载文件,IE提示无法下载
- This Android SDK requires Andriod Developer Toolkit version...Location of the Android SDK has not be
- unity制作2048
- 三层架构与MVC
- 运用jsp简单实现 类似HTML框架结构的运用:
- java 装饰模式---java io使用的模式
- hdu1281棋盘游戏(二分匹配,最小顶点覆盖)
- mysql触发器
- 初识.NET
- Netty使用实例
- 给在IT路上前进的自己来点鼓励