原地址: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. (转)RabbitMQ消息队列(三):任务分发机制

    在上篇文章中,我们解决了从发送端(Producer)向接收端(Consumer)发送“Hello World”的问题.在实际的应用场景中,这是远远不够的.从本篇文章开始,我们将结合更加实际的应用场景来 ...

  2. Mybatis-Generator插件自动生成Dao、Model、Mapping相关文件

    最近做项目,mapping 有点多而且容易写错,于是试着用了Mybatis-Generator 插件自动生成 dao, domain  mapping 文件.感觉还挺好用.把相关配置分享,一边以后做项 ...

  3. Java线程面试题 Top 50(转载)

    原文链接:http://www.importnew.com/12773.html 本文由 ImportNew - 李 广 翻译自 javarevisited.欢迎加入Java小组.转载请参见文章末尾的 ...

  4. go开发环境配置

  5. 安装php-posix

      1.安装php-posix 1 yum -y install php-process 2.验证是否安装上了 1 php -m|grep posix 1 posix  

  6. [转]Android在eclipse中的快捷键

    1.选中你要加注释的区域,用ctrl+shift+C 会加上//注释2.先把你要注释的东西选中,用shit+ctrl+/ 会加上/*    */注释3.要修改在eclispe中的命令的快捷键方式我们只 ...

  7. Messagebox.Show()常用参数设置

    private void button1_Click(object sender, EventArgs e) { MessageBox.Show(" 1 个参数 " ); } pr ...

  8. java.lang.NoClassDefFoundError Adding a jar to an RCP application

    给RCP中加入jar包与一般的java工程是有些个区别的,否则会出现"java.lang.NoClassDefFoundError" Open plug-in.xmlGo to R ...

  9. protobuf编译报错

    在下载protobuf进行编译的时候会出现如图所示的错误 修改 C:\protobuf-2.4.1\gtest\include\gtest\internal\gtest-tuple.h(C:是我解压p ...

  10. python:执行一个命令行N次

    经常希望可以执行一个命令行N次...windows下没有现成的工具(有?推荐给我!) 用python写一个... #!/usr/bin/evn python #coding: utf-8 " ...