原地址:http://blog.csdn.net/dingxiaowei2013/article/details/36462749

048是继FlappyBird之后另一个比较热的轻量级的手游,简单易玩。最近要离职原先的公司——因为我想做游戏,虽然玩游戏不是很多,但还是热爱开发游戏,因此就想去一家游戏公司,感觉对老板有一点愧疚和感激,愿原公司发展越来越好,用灰太狼的话讲,我还会回来的,哈哈!即将入职新公司,听说压力会很大,加班无止境,加班其实我到不怕,乘年轻,还有拼劲,加班算什么,其实只要自己能做出东西,感觉有成就感,倒还是喜欢花更多的时间去做东西,最近处于过渡期,写写之前公司的工作小结,还不是很忙,今天花了一个多小时,自己想了一下2048的算法,然后将其实现,可能算法不是那么优,还望批评交流!

效果图

实现的还比较粗糙,贴出主要逻辑代码,仅供参考,欢迎给出更优算法!

  1. using UnityEngine;
  2. using System.Collections;
  3. public class NewBehaviourScript : MonoBehaviour
  4. {
  5. public UILabel valueLabel;
  6. bool gameover = false;
  7. void Start()
  8. {
  9. gameover = false;
  10. valueLabel = GameObject.Find("ValueLabel").GetComponentInChildren<UILabel>();
  11. valueLabel.text = "Game Start";
  12. valueLabel.color = Color.green;
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. if (!gameover)
  18. {
  19. if (Input.GetKeyDown(KeyCode.D))
  20. {
  21. moveR();
  22. CreateNumber();
  23. }
  24. else if (Input.GetKeyDown(KeyCode.A))
  25. {
  26. moveL();
  27. CreateNumber();
  28. }
  29. else if (Input.GetKeyDown(KeyCode.W))
  30. {
  31. moveU();
  32. CreateNumber();
  33. }
  34. else if (Input.GetKeyDown(KeyCode.S))
  35. {
  36. moveD();
  37. CreateNumber();
  38. }
  39. }
  40. }
  41. void moveU()
  42. {
  43. for (int i = 1; i <= 4; i++)
  44. {
  45. bool flag = false;
  46. for (int j = 2; j <= 4; j++)
  47. {
  48. for (int k = j - 1; k >= 1; k--)
  49. {
  50. //获取当前元素
  51. GameObject go = GameObject.Find("L" + (k + 1).ToString() + i.ToString());
  52. print("当前对象" + go.name);
  53. UILabel I = go.GetComponentInChildren<UILabel>();
  54. //获取下一个元素
  55. GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString());
  56. print("下一个对象" + goNext.name);
  57. UILabel INext = goNext.GetComponentInChildren<UILabel>();
  58. //比较代码
  59. if (I.text != "")
  60. {
  61. if (INext.text == "")
  62. {
  63. INext.text = I.text;
  64. I.text = "";
  65. }
  66. else if (I.text == INext.text)
  67. {
  68. if (!flag)
  69. {
  70. int a = int.Parse(INext.text) + int.Parse(I.text);
  71. INext.text = a.ToString();
  72. I.text = "";
  73. flag = true;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. void moveD()
  82. {
  83. for (int i = 1; i <= 4; i++)
  84. {
  85. bool flag = false;
  86. for (int j = 3; j >= 1; j--)
  87. {
  88. for (int k = j + 1; k <= 4; k++)
  89. {
  90. //获取当前元素
  91. GameObject go = GameObject.Find("L" + (k-1).ToString() + i.ToString());
  92. print("当前对象" + go.name);
  93. UILabel I = go.GetComponentInChildren<UILabel>();
  94. //获取下一个元素
  95. GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString());
  96. print("下一个对象" + goNext.name);
  97. UILabel INext = goNext.GetComponentInChildren<UILabel>();
  98. //比较代码
  99. if (I.text != "")
  100. {
  101. if (INext.text == "")
  102. {
  103. INext.text = I.text;
  104. I.text = "";
  105. }
  106. else if (I.text == INext.text)
  107. {
  108. if (!flag)
  109. {
  110. int a = int.Parse(INext.text) + int.Parse(I.text);
  111. INext.text = a.ToString();
  112. I.text = "";
  113. flag = true;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. void moveL()
  122. {
  123. for (int i = 1; i <= 4; i++)
  124. {
  125. bool flag = false;
  126. for (int j = 2; j <= 4; j++)
  127. {
  128. for (int k = j - 1; k >=1 ; k--)
  129. {
  130. //获取当前元素
  131. GameObject go = GameObject.Find("L" + i.ToString() + (k + 1).ToString());
  132. print("当前对象" + go.name);
  133. UILabel I = go.GetComponentInChildren<UILabel>();
  134. //获取下一个元素
  135. GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString());
  136. print("下一个对象" + goNext.name);
  137. UILabel INext = goNext.GetComponentInChildren<UILabel>();
  138. //比较代码
  139. if (I.text != "")
  140. {
  141. if (INext.text == "")
  142. {
  143. INext.text = I.text;
  144. I.text = "";
  145. }
  146. else if (I.text == INext.text)
  147. {
  148. if (!flag)
  149. {
  150. int a = int.Parse(INext.text) + int.Parse(I.text);
  151. INext.text = a.ToString();
  152. I.text = "";
  153. flag = true;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. void moveR()
  162. {
  163. for (int i = 1; i <= 4; i++)
  164. {
  165. bool flag = false;
  166. for (int j = 3; j >= 1; j--)
  167. {
  168. for (int k = j + 1; k <= 4; k++)
  169. {
  170. //获取当前元素
  171. GameObject go = GameObject.Find("L" + i.ToString() + (k - 1).ToString());
  172. print("当前对象" + go.name);
  173. UILabel I = go.GetComponentInChildren<UILabel>();
  174. //获取下一个元素
  175. GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString());
  176. print("下一个对象" + goNext.name);
  177. UILabel INext = goNext.GetComponentInChildren<UILabel>();
  178. //比较代码
  179. if (I.text != "")
  180. {
  181. if (INext.text == "")
  182. {
  183. INext.text = I.text;
  184. I.text = "";
  185. }
  186. else if (I.text == INext.text)
  187. {
  188. if (!flag)
  189. {
  190. int a = int.Parse(INext.text) + int.Parse(I.text);
  191. INext.text = a.ToString();
  192. I.text = "";
  193. flag = true;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. void CreateNumber()
  202. {
  203. int count = 0;
  204. bool flag = false;
  205. for (int i = 1; i <= 4; i++)
  206. {
  207. if (!flag)
  208. {
  209. for (int j = 1; j <= 4; j++)
  210. {
  211. GameObject go = GameObject.Find("L" + i.ToString() + j.ToString());
  212. UILabel label = go.GetComponentInChildren<UILabel>();
  213. if (label.text == "")
  214. {
  215. int r = Random.Range(1, 10);
  216. if (r <= 5)
  217. {
  218. int value = Random.Range(1, 5);
  219. if (value < 4)
  220. label.text = "2";
  221. else
  222. label.text = "4";
  223. flag = true;
  224. break;
  225. }
  226. }
  227. else
  228. {
  229. if (++count == 16)
  230. {
  231. valueLabel.text = "Game Over";
  232. valueLabel.color = Color.red;
  233. gameover = true;
  234. }
  235. }
  236. }
  237. }
  238. else
  239. break;
  240. }
  241. }
  242. }

[Unity3D+算法]一小时做个2048的更多相关文章

  1. Unity3D使用碰撞体做触发器实现简单的自己主动开门

     在游戏制作中触发器的使用很的方便也很有用. 这一张我们简介一下怎样使用一个简单的触发器来实现自己主动开门关门的效果. 首先确保你已经对门进行了动画的设置. 详细流程例如以下. 选择Window- ...

  2. 分布式ID系列(5)——Twitter的雪法算法Snowflake适合做分布式ID吗

    介绍Snowflake算法 SnowFlake算法是国际大公司Twitter的采用的一种生成分布式自增id的策略,这个算法产生的分布式id是足够我们我们中小公司在日常里面的使用了.我也是比较推荐这一种 ...

  3. Recommending music on Spotify with deep learning 采用深度学习算法为Spotify做基于内容的音乐推荐

    本文参考http://blog.csdn.net/zdy0_2004/article/details/43896015译文以及原文file:///F:/%E6%9C%BA%E5%99%A8%E5%AD ...

  4. 编程算法 - 不用加减乘除做加法 代码(C)

    不用加减乘除做加法 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 写一个函数, 求两个整数之和, 要求在函数体内不得使用+, -, *, /四 ...

  5. Unity3d 子线程能做的事

    一,子线程中能做的事: 1,数据逻辑方面计算: 二,子线程中,不能: 1,加载场景相关事件: Application.LoadLevelAsync.Application.LoadLevel等: 2, ...

  6. ACM用到的算法。先做个笔记,记一下

    ACM 所有算法 数据结构 栈,队列,链表 哈希表,哈希数组 堆,优先队列 双端队列 可并堆 左偏堆 二叉查找树 Treap 伸展树 并查集 集合计数问题 二分图的识别 平衡二叉树 二叉排序树 线段树 ...

  7. 深入浅出游戏算法(4)-unity3d算法(1)-球转动

    球 转动 按以下布局放置好unity3d的各个组件.设置好渲染.位置.光源.大小等 麦好的AI乐园博客全部内容是原创,假设转载请注明来源 http://blog.csdn.net/myhaspl/ 编 ...

  8. 案例(一) 利用机器算法RFM模型做用户价值分析

      一.案例背景 在产品迭代过程中,通常需要根据用户的属性进行归类,也就是通过分析数据,对用户进行归类,以便于在推送及转化过程中获得更大的收益. 本案例是基于某互联网公司的实际用户购票数据为研究对象, ...

  9. Unity3d 用NGUI制作做新手引导的思路

    一.先看下效果 Prefab结构 二.实现思路: 1.prefab上的Panel层级设置成较高 2.背景由5个UISprite拼接起来的,4个(L,R,U,D)当作遮罩,1个镂空(Hollow)当作点 ...

随机推荐

  1. 解决Toad for Oracle显示乱码问题

    1.查看一下数据库字符集: 使用下面语句:Select userenv('language') from dual;或者:Select name, value$ from props$;查看. 查看完 ...

  2. JAVA:类,对象,成员属性,成员方法,构造方法,类变量,类方法<2>

    一.类的定义 一个全面的类定义是比较复杂的,  定义如下:

  3. Codevs 1105 过河

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond  题目描述 Description 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有 ...

  4. float闭合(清除浮动)和CSS HACK

    一.float 闭合(清除浮动) 将以下代码加入Global CSS 中,给需要闭合的div加上 class="clearfix" 即可,屡试不爽. <style>.c ...

  5. 使用Eclipse开发,Java Compiler中Annotation Processin不出现的解决方案

    第一步:在Eclipse菜单栏中点击Help,在点击inatall New Software 第二步:在Work with中找到 Juno - http://download.eclipse.org/ ...

  6. VS2010配色方案

    http://studiostyl.es/ 导入步骤:  工具------------导入和导出设置------------导入选定的环境设置------------否,仅导入新设置--------- ...

  7. Python解析HTML的开发库pyquery

    PyQuery是一个类似于jQuery的Python库,也可以说是jQuery在Python上的实现,能够以 jQuery 的语法来操作解析 HTML 文档,易用性和解析速度都很好. 例如,一段豆瓣h ...

  8. ORA-00054

    系统版本: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 数据库版本: Oracle Database ...

  9. iOS/Objective-C开发 字典NSDictionary的深复制(使用category)

    目标:把NSDictionary对象转换成NSMutableDictionary对象,对象内容是字符串数组,需要实现完全复制(深复制). 如果调用NSDictionary的mutableCopy方法, ...

  10. 查看MYSQL数据库中所有用户及拥有权限

    查看MYSQL数据库中所有用户 mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM m ...