首页 技术 正文
技术 2022年11月15日
0 收藏 752 点赞 5,058 浏览 4203 个字

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

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

效果图

[Unity3D+算法]一小时做个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. }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903