• 2048


    2048

     using UnityEngine;
    using System.Collections.Generic;

    public class Game : MonoBehaviour {

        Map map;//地图类
        Card[,] mCards;//存放卡片的数组
        //存放隐藏的卡牌
        List<Card> mHideCards = new List<Card>();
        //存放所有的卡牌精灵
        public Sprite[] mSprites;

        public static Game Instance;

        void Awake()
        {
            Instance = this;
        }
        void Start ()
        {
           
            map = GetComponent<Map>();//地图就是当前物体身上的脚本
            map.InitGrid();//初始化格子
            mCards = map.InitCard();//初始化卡片并且拿到初始化的所有卡片的信息
            //将地图上的所有的卡片先放入隐藏卡片的集合里
            for (int i = 0; i < mCards.GetLength(0); i++)
            {
                for (int j = 0; j < mCards.GetLength(1); j++)
                {
                    mHideCards.Add(mCards[i,j]);
                }
            }
        
            //随机先生成两个卡片
            RandomCard();
            RandomCard();
          
        }
        //向上移动
        void MoveUp()
        {
            //行由上到下
            for (int j=2; j >= 0; j--)
            {
                for (int i = 0; i < 4; i++)
                {
                    //如果当前卡牌未隐藏
                    if (mCards[i,j].mUI.activeSelf)
                    {
                        //记录行号
                        int row = j;
                        // -1表示不向上移动,否则表示向上移动的位置
                        int up = -1;
                        bool isadd = false;
                        while (++row<4)
                        {
                            //向上移动
                          
                            //Double 如果与该位置卡牌跟数值相同
                             if (mCards[i, row].mNum == mCards[i, j].mNum)
                            {
                                mCards[i, row].DoAdd();//上面的图片数值加倍
                                mCards[i, j].Hide();//当前图片隐藏
                                mHideCards.Add(mCards[i, j]);//把当前图片加到隐藏集合中
                                isadd = true;//表示已经加倍过啦
                                break;//跳出本层内循环
                            }                     
                         if (!mCards[i, row].mUI.activeSelf)//如果上面的图片是激活状态
                            {
                                up = row;//记录位置
                                continue;
                            }
                            //不动
                            else
                            {
                                break;
                            }
                            //row++;                    
                        }
                        //如果没有执行过Add操作 并且可以向上移动
                        if (up!=-1&&!isadd)
                        {
                          
                            //该位置卡牌显示    
                            mCards[i, up].Show(mCards[i, j].mNum);
                            mHideCards.Remove(mCards[i, up]);//从隐藏列表中除去                   
                            //原位置卡牌隐藏
                            mCards[i, j].Hide();
                            mHideCards.Add(mCards[i, j]);
                           
                        }
                    }
                }
            }
        }
        void MoveDown()
        {
            //行由下到上
            for (int j = 1; j < 4; j++)
            {
                for (int i = 0; i < 4; i++)
                {
                    //如果卡牌未隐藏
                    if (mCards[i, j].mUI.activeSelf)
                    {
                        //记录行号
                        int row = j;
                        // -1表示不向上移动,否则表示向上移动的位置
                        int up = -1;
                        bool isadd = false;
                        while (--row >=0)
                        {
                            //向上移动

                            //Double 如果与该位置卡牌跟数值相同
                            if (mCards[i, row].mNum == mCards[i, j].mNum)
                            {

                                mCards[i, row].DoAdd();
                                mCards[i, j].Hide();
                                mHideCards.Add(mCards[i, j]);
                                isadd = true;
                                break;
                            }
                            if (!mCards[i, row].mUI.activeSelf)
                            {
                                up = row;//记录位置
                                continue;
                            }
                            //不动
                            else
                            {
                                break;
                            }
                           // row++;
                        }
                        //如果没有执行过Add操作 并且可以向上移动
                        if (up != -1 && !isadd)
                        {
                            //该位置卡牌显示    
                            mCards[i, up].Show(mCards[i, j].mNum);
                            mHideCards.Remove(mCards[i, up]);
                            //原位置卡牌隐藏
                            mCards[i, j].Hide();
                            mHideCards.Add(mCards[i, j]);
                        }
                    }
                }
            }
        }
        void MoveLeft()
        {
            //行由下到上
            for (int j = 0; j < 4; j++)
            {
                for (int i = 1; i < 4; i++)
                {
                    //如果卡牌未隐藏
                    if (mCards[i, j].mUI.activeSelf)
                    {
                        //记录行号
                        int row = i;
                        // -1表示不向上移动,否则表示向上移动的位置
                        int up = -1;
                        bool isadd = false;
                        while (--row >= 0)
                        {
                            //向上移动

                            //Double 如果与该位置卡牌跟数值相同
                            if (mCards[row, j].mNum == mCards[i, j].mNum)
                            {

                                mCards[row, j].DoAdd();
                                mCards[i, j].Hide();
                                mHideCards.Add(mCards[i, j]);
                                isadd = true;
                                break;
                            }
                            if (!mCards[row, j].mUI.activeSelf)
                            {
                                up = row;//记录位置
                                continue;
                            }
                            //不动
                            else
                            {
                                break;
                            }
                            // row++;
                        }
                        //如果没有执行过Add操作 并且可以向上移动
                        if (up != -1 && !isadd)
                        {
                            //该位置卡牌显示    
                            mCards[up, j].Show(mCards[i, j].mNum);
                            mHideCards.Remove(mCards[up, j]);
                            //原位置卡牌隐藏
                            mCards[i, j].Hide();
                            mHideCards.Add(mCards[i, j]);
                        }
                    }
                }
            }
        }
        void MoveRight()
        {
            //行由下到上
            for (int j = 0; j < 4; j++)
            {
                for (int i = 2; i >= 0; i--)
                {
                    //如果卡牌未隐藏
                    if (mCards[i, j].mUI.activeSelf)
                    {
                        //记录行号
                        int row = i;
                        // -1表示不向上移动,否则表示向上移动的位置
                        int up = -1;
                        bool isadd = false;
                        while (++row < 4)
                        {
                            //向上移动

                            //Double 如果与该位置卡牌跟数值相同
                            if (mCards[row, j].mNum == mCards[i, j].mNum)
                            {

                                mCards[row, j].DoAdd();
                                mCards[i, j].Hide();
                                mHideCards.Add(mCards[i, j]);
                                isadd = true;
                                break;
                            }
                            if (!mCards[row, j].mUI.activeSelf)
                            {
                                up = row;//记录位置
                                continue;
                            }
                            //不动
                            else
                            {
                                break;
                            }
                            // row++;
                        }
                        //如果没有执行过Add操作 并且可以向上移动
                        if (up != -1 && !isadd)
                        {
                            //该位置卡牌显示    
                            mCards[up, j].Show(mCards[i, j].mNum);
                            mHideCards.Remove(mCards[up, j]);
                            //原位置卡牌隐藏
                            mCards[i, j].Hide();
                            mHideCards.Add(mCards[i, j]);
                        }
                    }
                }
            }
        }
        //随机显示一张卡牌
        bool RandomCard()
        {
          
            int x;
            if (mHideCards.Count==0)
            {
              
                return false;
            }              
            x = Random.Range(0, mHideCards.Count);
            if (Random.Range(0, 4) == 3)//25%的几率是4
            {
                mHideCards[x].Show(4);//在相应的位置显示相应的卡牌(mHideCards其实存储的单位是二维坐标信息)
            }
            else//75%的机率是2
            {
                mHideCards[x].Show(2);
            }
           
            mHideCards.Remove(mHideCards[x]);   //从隐藏集合中去除相应元素                       
            return true;
          
        }
        //根据Num获取精灵图片的方法
        public Sprite GetImg(int num)
        {
            num = (int)Mathf.Log(num, 2);//当num=2时 经过计算 num新值为1,num=4,新值为2;
            return mSprites[num-1];//mSprites中的前两张图片为数字2和4
        }
        void Update ()
        {
            if (Input.GetKeyDown(KeyCode.W))
            {
                MoveUp();
                RandomCard();
            }
            if (Input.GetKeyDown(KeyCode.S))
            {
               MoveDown();
                RandomCard();
            }
            if (Input.GetKeyDown(KeyCode.A))
            {
                MoveLeft();
                RandomCard();
            }
            if (Input.GetKeyDown(KeyCode.D))
            {
                MoveRight();
                RandomCard();
            }

        }
    }






    using UnityEngine;
    using System.Collections;

    /// <summary>
    /// 卡片类
    /// </summary>
    public class Card
    {
        //卡片
        public GameObject mUI;
        //卡片数值 2,4,8,16,32...
        public int mNum;
        //卡片显示
        public void Show(int num)
        {
            mNum = num;
            mUI.GetComponent<SpriteRenderer>().sprite = Game.Instance.GetImg(num);//根据幂次值找到相应图片
            mUI.gameObject.SetActive(true);
            mUI.transform.localScale = Vector3.one;
            iTween.ScaleFrom(mUI.gameObject, Vector3.zero, 1.5f);
        }
        //卡片隐藏
        public void Hide()
        {
            mNum = 0;
            mUI.gameObject.SetActive(false);
        }
        //卡片叠加
        public void DoAdd()
        {
            mNum *= 2;
            mUI.GetComponent<SpriteRenderer>().sprite = Game.Instance.GetImg(mNum);
        }
       
    }












    using UnityEngine;
    using System.Collections;

    public class Map : MonoBehaviour
    {
        //地图4*4
        public const int SIZE = 4;
        //边长
        public const float MAPLENGTH = 6.25f;
        //格子边长
        public const float GRIDLENGTH = 1.3f;
        //间距
        public const float OFFSET = 0.21F;
        //格子预设体
        public GameObject mGridPrefab;
        //卡片预设体
        public GameObject mCardPrefab;
        private Card[,] mCards = new Card [ SIZE, SIZE];

      


        //生成格子
       public  void InitGrid()
        {
            Vector2 pos;
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    GameObject grid = Instantiate(mGridPrefab) as GameObject;
                    pos = CalPosByIndex(i, j);
                    grid.transform.position = pos;
                    grid.transform.SetParent(transform);
                }
            }


        }

        //初始化卡片,先随便在地图所有的位置生成一张一样的图片,并且隐藏
      public Card[,] InitCard()
        {
          
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    mCards[i, j] = new Card();
                    mCards[i, j].mUI = Instantiate(mCardPrefab);
                    mCards[i, j].mUI.transform.position= CalPosByIndex(i, j);
                    mCards[i, j].Hide();
                }
            }
            return mCards;
        }
        //根据坐标算出实际坐标
      public static Vector2 CalPosByIndex(int x, int y)
        {
            float _x = GRIDLENGTH / 2 - MAPLENGTH / 2 + (x + 1) * OFFSET + x * GRIDLENGTH;
            float _y = GRIDLENGTH / 2 - MAPLENGTH / 2 + (y + 1) * OFFSET + y * GRIDLENGTH;
            return new Vector2(_x, _y);
        }

    }

  • 相关阅读:
    21.面向对象:多态与多态性、面向对象进阶:内置方法,反射。异常处理
    mysql 复习
    Python之math模块
    mysql数据类型转换
    Remmina
    JWT校验流程源码
    JWT签发token
    elasticsearch使用
    Flask基础
    初识Flask
  • 原文地址:https://www.cnblogs.com/ningyongbin/p/5982922.html
Copyright © 2020-2023  润新知