码迷,mamicode.com
首页 > 其他好文 > 详细

2048

时间:2016-10-20 23:56:45      阅读:545      评论:0      收藏:0      [点我收藏+]

标签:

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;
                        }                        
                    }
                    //如果没有执行过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 = 0; j < 3; 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;
                        }
                    }
                    //如果没有执行过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]);
                    }
                }
            }
        }
    }
    //随机显示一张卡牌
    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();
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
          //  MoveDown();
        }

    }
}

 

 

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);
    }

}

 

2048

标签:

原文地址:http://www.cnblogs.com/ningyongbin/p/5982922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!