• [Unity3D+算法]一小时做个2048


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

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

    效果图

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

    [csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
     
    1. using UnityEngine;  
    2. using System.Collections;  
    3.   
    4. public class NewBehaviourScript : MonoBehaviour  
    5. {  
    6.     public UILabel valueLabel;  
    7.     bool gameover = false;  
    8.     void Start()  
    9.     {  
    10.         gameover = false;  
    11.         valueLabel = GameObject.Find("ValueLabel").GetComponentInChildren<UILabel>();  
    12.         valueLabel.text = "Game Start";  
    13.         valueLabel.color = Color.green;  
    14.     }  
    15.   
    16.     // Update is called once per frame  
    17.     void Update()  
    18.     {  
    19.         if (!gameover)  
    20.         {  
    21.             if (Input.GetKeyDown(KeyCode.D))  
    22.             {  
    23.                 moveR();  
    24.                 CreateNumber();  
    25.             }  
    26.             else if (Input.GetKeyDown(KeyCode.A))  
    27.             {  
    28.                 moveL();  
    29.                 CreateNumber();  
    30.             }  
    31.             else if (Input.GetKeyDown(KeyCode.W))  
    32.             {  
    33.                 moveU();  
    34.                 CreateNumber();  
    35.             }  
    36.             else if (Input.GetKeyDown(KeyCode.S))  
    37.             {  
    38.                 moveD();  
    39.                 CreateNumber();  
    40.             }  
    41.         }  
    42.     }  
    43.   
    44.   
    45.     void moveU()  
    46.     {  
    47.         for (int i = 1; i <= 4; i++)  
    48.         {  
    49.             bool flag = false;  
    50.             for (int j = 2; j <= 4; j++)  
    51.             {  
    52.                 for (int k = j - 1; k >= 1; k--)  
    53.                 {  
    54.                     //获取当前元素  
    55.                     GameObject go = GameObject.Find("L" + (k + 1).ToString() + i.ToString());  
    56.                     print("当前对象" + go.name);  
    57.                     UILabel I = go.GetComponentInChildren<UILabel>();  
    58.   
    59.                     //获取下一个元素  
    60.                     GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString());  
    61.                     print("下一个对象" + goNext.name);  
    62.                     UILabel INext = goNext.GetComponentInChildren<UILabel>();  
    63.   
    64.                     //比较代码  
    65.                     if (I.text != "")  
    66.                     {  
    67.                         if (INext.text == "")  
    68.                         {  
    69.                             INext.text = I.text;  
    70.                             I.text = "";  
    71.                         }  
    72.                         else if (I.text == INext.text)  
    73.                         {  
    74.                             if (!flag)  
    75.                             {  
    76.                                 int a = int.Parse(INext.text) + int.Parse(I.text);  
    77.                                 INext.text = a.ToString();  
    78.                                 I.text = "";  
    79.                                 flag = true;  
    80.                             }  
    81.                         }  
    82.                     }  
    83.                 }  
    84.             }  
    85.         }  
    86.     }  
    87.   
    88.     void moveD()  
    89.     {  
    90.         for (int i = 1; i <= 4; i++)  
    91.         {  
    92.             bool flag = false;  
    93.             for (int j = 3; j >= 1; j--)  
    94.             {  
    95.                 for (int k = j + 1; k <= 4; k++)  
    96.                 {  
    97.                     //获取当前元素  
    98.                     GameObject go = GameObject.Find("L" + (k-1).ToString() + i.ToString());  
    99.                     print("当前对象" + go.name);  
    100.                     UILabel I = go.GetComponentInChildren<UILabel>();  
    101.   
    102.                     //获取下一个元素  
    103.                     GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString());  
    104.                     print("下一个对象" + goNext.name);  
    105.                     UILabel INext = goNext.GetComponentInChildren<UILabel>();  
    106.   
    107.                     //比较代码  
    108.                     if (I.text != "")  
    109.                     {  
    110.                         if (INext.text == "")  
    111.                         {  
    112.                             INext.text = I.text;  
    113.                             I.text = "";  
    114.                         }  
    115.                         else if (I.text == INext.text)  
    116.                         {  
    117.                             if (!flag)  
    118.                             {  
    119.                                 int a = int.Parse(INext.text) + int.Parse(I.text);  
    120.                                 INext.text = a.ToString();  
    121.                                 I.text = "";  
    122.                                 flag = true;  
    123.                             }  
    124.                         }  
    125.                     }  
    126.                 }  
    127.             }  
    128.         }  
    129.     }  
    130.   
    131.   
    132.   
    133.     void moveL()  
    134.     {  
    135.         for (int i = 1; i <= 4; i++)  
    136.         {  
    137.             bool flag = false;  
    138.             for (int j = 2; j <= 4; j++)  
    139.             {  
    140.                   
    141.                 for (int k = j - 1; k >=1 ; k--)  
    142.                 {  
    143.                     //获取当前元素  
    144.                     GameObject go = GameObject.Find("L" + i.ToString() + (k + 1).ToString());  
    145.                     print("当前对象" + go.name);  
    146.                     UILabel I = go.GetComponentInChildren<UILabel>();  
    147.   
    148.                     //获取下一个元素  
    149.                     GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString());  
    150.                     print("下一个对象" + goNext.name);  
    151.                     UILabel INext = goNext.GetComponentInChildren<UILabel>();  
    152.   
    153.                     //比较代码  
    154.                     if (I.text != "")  
    155.                     {  
    156.                         if (INext.text == "")  
    157.                         {  
    158.                             INext.text = I.text;  
    159.                             I.text = "";  
    160.                         }  
    161.                         else if (I.text == INext.text)  
    162.                         {  
    163.                             if (!flag)  
    164.                             {  
    165.                                 int a = int.Parse(INext.text) + int.Parse(I.text);  
    166.                                 INext.text = a.ToString();  
    167.                                 I.text = "";  
    168.                                 flag = true;  
    169.                             }  
    170.                         }  
    171.                     }  
    172.                 }  
    173.             }  
    174.         }  
    175.     }  
    176.   
    177.     void moveR()  
    178.     {  
    179.         for (int i = 1; i <= 4; i++)  
    180.         {  
    181.             bool flag = false;  
    182.             for (int j = 3; j >= 1; j--)  
    183.             {  
    184.                   
    185.                 for (int k = j + 1; k <= 4; k++)  
    186.                 {  
    187.                     //获取当前元素  
    188.                     GameObject go = GameObject.Find("L" + i.ToString() + (k - 1).ToString());  
    189.                     print("当前对象" + go.name);  
    190.                     UILabel I = go.GetComponentInChildren<UILabel>();  
    191.   
    192.                     //获取下一个元素  
    193.                     GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString());  
    194.                     print("下一个对象" + goNext.name);  
    195.                     UILabel INext = goNext.GetComponentInChildren<UILabel>();  
    196.   
    197.                     //比较代码  
    198.                     if (I.text != "")  
    199.                     {  
    200.                         if (INext.text == "")  
    201.                         {  
    202.                             INext.text = I.text;  
    203.                             I.text = "";  
    204.                         }  
    205.                         else if (I.text == INext.text)  
    206.                         {  
    207.                             if (!flag)  
    208.                             {  
    209.                                 int a = int.Parse(INext.text) + int.Parse(I.text);  
    210.                                 INext.text = a.ToString();  
    211.                                 I.text = "";  
    212.                                 flag = true;  
    213.                             }  
    214.                         }  
    215.                     }  
    216.                 }  
    217.             }  
    218.         }  
    219.     }  
    220.   
    221.   
    222.   
    223.     void CreateNumber()  
    224.     {  
    225.         int count = 0;  
    226.         bool flag = false;  
    227.         for (int i = 1; i <= 4; i++)  
    228.         {  
    229.             if (!flag)  
    230.             {  
    231.                 for (int j = 1; j <= 4; j++)  
    232.                 {  
    233.                     GameObject go = GameObject.Find("L" + i.ToString() + j.ToString());  
    234.                     UILabel label = go.GetComponentInChildren<UILabel>();  
    235.                     if (label.text == "")  
    236.                     {  
    237.                         int r = Random.Range(1, 10);  
    238.                         if (r <= 5)  
    239.                         {  
    240.                             int value = Random.Range(1, 5);  
    241.                             if (value < 4)  
    242.                                 label.text = "2";  
    243.                             else  
    244.                                 label.text = "4";  
    245.                             flag = true;  
    246.                             break;  
    247.                         }  
    248.                     }  
    249.                     else  
    250.                     {  
    251.                         if (++count == 16)  
    252.                         {  
    253.                             valueLabel.text = "Game Over";  
    254.                             valueLabel.color = Color.red;  
    255.                             gameover = true;  
    256.                         }  
    257.                     }  
    258.                 }  
    259.             }  
    260.             else  
    261.                 break;  
    262.         }  
    263.     }  
    264.   
    265.   
    266. }  
  • 相关阅读:
    爬虫笔记:使用python生成词云(八)
    31丨2内核剖析
    六飞翔篇(4讲)30 丨 2特性概览
    29 丨 我应该迁移到HTTPS吗?
    28 丨 连接太慢该怎么办:HTTPS的优化
    27丨更好更快的握手:TLS1.3特性解析
    26丨信任始于握手:TLS1.2连接过程解析
    Python全栈工程师 (exercises)
    Python全栈工程师(每周总结:2)
    Python全栈工程师(函数嵌套、变量作用域)
  • 原文地址:https://www.cnblogs.com/123ing/p/3823011.html
Copyright © 2020-2023  润新知