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