亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述[Unity3D+算法]一小时做个2048相关的知识,希望能为你提供帮助。
2048是继FlappyBird之后另一个比较热的轻量级的手游,简单易玩。最近要离职原先的公司——因为我想做游戏,虽然玩游戏不是很多,但还是热爱开发游戏,因此就想去一家游戏公司,感觉对老板有一点愧疚和感激,愿原公司发展越来越好,用灰太狼的话讲,我还会回来的,哈哈!即将入职新公司,听说压力会很大,加班无止境,加班其实我到不怕,乘年轻,还有拼劲,加班算什么,其实只要自己能做出东西,感觉有成就感,倒还是喜欢花更多的时间去做东西,最近处于过渡期,写写之前公司的工作小结,还不是很忙,今天花了一个多小时,自己想了一下2048的算法,然后将其实现,可能算法不是那么优,还望批评交流!
效果图【[Unity3D+算法]一小时做个2048】
文章图片
实现的还比较粗糙,贴出主要逻辑代码,仅供参考,欢迎给出更优算法!
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public UILabel valueLabel; bool gameover = false; void Start() { gameover = false; valueLabel = GameObject.Find("ValueLabel").GetComponentInChildren< UILabel> (); valueLabel.text = "Game Start"; valueLabel.color = Color.green; } // Update is called once per frame void Update() { if (!gameover) { if (Input.GetKeyDown(KeyCode.D)) { moveR(); CreateNumber(); } else if (Input.GetKeyDown(KeyCode.A)) { moveL(); CreateNumber(); } else if (Input.GetKeyDown(KeyCode.W)) { moveU(); CreateNumber(); } else if (Input.GetKeyDown(KeyCode.S)) { moveD(); CreateNumber(); } } } void moveU() { for (int i = 1; i < = 4; i++) { bool flag = false; for (int j = 2; j < = 4; j++) { for (int k = j - 1; k > = 1; k--) { //获取当前元素 GameObject go = GameObject.Find("L" + (k + 1).ToString() + i.ToString()); print("当前对象" + go.name); UILabel I = go.GetComponentInChildren< UILabel> (); //获取下一个元素 GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString()); print("下一个对象" + goNext.name); UILabel INext = goNext.GetComponentInChildren< UILabel> (); //比较代码 if (I.text != "") { if (INext.text == "") { INext.text = I.text; I.text = ""; } else if (I.text == INext.text) { if (!flag) { int a = int.Parse(INext.text) + int.Parse(I.text); INext.text = a.ToString(); I.text = ""; flag = true; } } } } } } } void moveD() { for (int i = 1; i < = 4; i++) { bool flag = false; for (int j = 3; j > = 1; j--) { for (int k = j + 1; k < = 4; k++) { //获取当前元素 GameObject go = GameObject.Find("L" + (k-1).ToString() + i.ToString()); print("当前对象" + go.name); UILabel I = go.GetComponentInChildren< UILabel> (); //获取下一个元素 GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString()); print("下一个对象" + goNext.name); UILabel INext = goNext.GetComponentInChildren< UILabel> (); //比较代码 if (I.text != "") { if (INext.text == "") { INext.text = I.text; I.text = ""; } else if (I.text == INext.text) { if (!flag) { int a = int.Parse(INext.text) + int.Parse(I.text); INext.text = a.ToString(); I.text = ""; flag = true; } } } } } } } void moveL() { for (int i = 1; i < = 4; i++) { bool flag = false; for (int j = 2; j < = 4; j++) { for (int k = j - 1; k > =1 ; k--) { //获取当前元素 GameObject go = GameObject.Find("L" + i.ToString() + (k + 1).ToString()); print("当前对象" + go.name); UILabel I = go.GetComponentInChildren< UILabel> (); //获取下一个元素 GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString()); print("下一个对象" + goNext.name); UILabel INext = goNext.GetComponentInChildren< UILabel> (); //比较代码 if (I.text != "") { if (INext.text == "") { INext.text = I.text; I.text = ""; } else if (I.text == INext.text) { if (!flag) { int a = int.Parse(INext.text) + int.Parse(I.text); INext.text = a.ToString(); I.text = ""; flag = true; } } } } } } } void moveR() { for (int i = 1; i < = 4; i++) { bool flag = false; for (int j = 3; j > = 1; j--) { for (int k = j + 1; k < = 4; k++) { //获取当前元素 GameObject go = GameObject.Find("L" + i.ToString() + (k - 1).ToString()); print("当前对象" + go.name); UILabel I = go.GetComponentInChildren< UILabel> (); //获取下一个元素 GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString()); print("下一个对象" + goNext.name); UILabel INext = goNext.GetComponentInChildren< UILabel> (); //比较代码 if (I.text != "") { if (INext.text == "") { INext.text = I.text; I.text = ""; } else if (I.text == INext.text) { if (!flag) { int a = int.Parse(INext.text) + int.Parse(I.text); INext.text = a.ToString(); I.text = ""; flag = true; } } } } } } } void CreateNumber() { int count = 0; bool flag = false; for (int i = 1; i < = 4; i++) { if (!flag) { for (int j = 1; j < = 4; j++) { GameObject go = GameObject.Find("L" + i.ToString() + j.ToString()); UILabel label = go.GetComponentInChildren< UILabel> (); if (label.text == "") { int r = Random.Range(1, 10); if (r < = 5) { int value = Random.Range(1, 5); if (value < 4) label.text = "2"; else label.text = "4"; flag = true; break; } } else { if (++count == 16) { valueLabel.text = "Game Over"; valueLabel.color = Color.red; gameover = true; } } } } else break; } }}
欢迎关注我的微博
推荐阅读
- 挨踢人物传关东升(成功的路上很寂寞,贵在坚持(第20期))
- Cocos2d-x 3.8.1+Cocos Studio 2.3.2捉虫记之控制场景文件中的骨骼动画
- Java如何使用Tessdata做OCR图片文字说明,看这篇就够了~
- 如何在JavaScript中自定义Google Maps颜色和样式
- 如何使用JavaScript(WebRTC)在浏览器中录制带音频的视频
- 如何使用JavaScript将二进制字符串转换为可读字符串,反之亦然
- 如何区分HTML并使用JavaScript在HTML中生成输出
- 如何使用js-imagediff使用JavaScript区分2张图片
- 如何在局域网连接的设备上使用Artyom.js沙箱