您的位置:首页 > 移动开发 > Unity3D

Unity NGUI实现2048(一)源代码

2015-08-08 15:35 399 查看

Unity NGUI实现2048(一)源代码

Manager.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Manager : MonoBehaviour
{

    public static Manager _instance;   //单例
    public GameObject WelcomePage;     //欢迎界面
    public GameObject gameWinPage;     //游戏胜利界面
    public GameObject gameLosePage;    //游戏失败界面
    public Numbers[,] numbers = new Numbers[4, 4];    //代表16个位置
    public GameObject numberPrefab;                   //每个小方块

    public List<Numbers> isMovingNumbers = new List<Numbers>();      //正在移动的方块列表

    public bool hasMove = false;           //已经移动

    private bool playMusic = true;          //播放音乐

    public UITexture music;               //背景音乐控制
    public Texture musicOff;              
    public Texture musicOn;

    public bool control=false;            //是否可以控制移动

    public GameObject ExitMassage;        //退出信息
    public GameObject ExitMessagePrefab;
    // Use this for initialization

    void Awake()
    {
        _instance = this;          //单例
    }
    void Start()
    {

        Instantiate(numberPrefab);          
        Instantiate(numberPrefab);           //创建初始的两个小方块

        playMusic = PlayerPrefs.GetInt("playMusic",1)==1?true:false; //读取背景音乐设置存档
        if (playMusic)
        {
            audio.Play();
            music.mainTexture = musicOn;
            
        }
        else
        {
           
            audio.Stop();
            music.mainTexture = musicOff;
            
        }

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))          //双击Esc退出应用
        {
            if (ExitMassage == null)
            {
                ExitMassage = Instantiate(ExitMessagePrefab) as GameObject;
                StartCoroutine("resetExitMessage");

            }
            else
            {
                Application.Quit();                         //退出应用
            }
        }
        if (control && isMovingNumbers.Count == 0)          //可控&&移动结束
        {
            int dirX = 0;
            int dirY = 0;
            if (Input.GetKeyDown("up"))
            {
                dirY = 1;
            }
            else if (Input.GetKeyDown("down"))
            {
                dirY = -1;
            }
            else if (Input.GetKeyDown("left"))
            {
                dirX = -1;
            }
            else if (Input.GetKeyDown("right"))
            {
                dirX = 1;
            }

            //手机触屏控制
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
                if (Mathf.Abs(touchDeltaPosition.x) > Mathf.Abs(touchDeltaPosition.y))
                {
                    if (touchDeltaPosition.x > 4)
                    {
                        dirX = 1;
                    }
                    else if(touchDeltaPosition.x<-4)
                    {
                        dirX = -1;
                    }
                }
                else 
                {
                    if (touchDeltaPosition.y > 4)
                    {
                        dirY = 1;
                    }
                    else if (touchDeltaPosition.y < -4)
                    {
                        dirY = -1;
                    }
                }
            }

            MoveNumbers(dirX, dirY);      //------>>>1
        }
        if (hasMove && isMovingNumbers.Count ==0)    //移动结束
        {
            Instantiate(numberPrefab);
            hasMove = false;
            for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 4; y++)
                {
                    if (numbers[x, y] != null)
                    {
                        numbers[x, y].hasMixed = false;
                    }
                }
            }
        }
    }

    public void CloseWelcomePage()
    {
        WelcomePage.SetActive(false);
        control = true;
    }

    public void MoveNumbers(int directionX, int directionY)   //------>>>2
    {
        if (directionX == 1)
        {
            if (directionX == 1)
            {
                for (int y = 0; y < 4; y++)
                {
                    for (int x = 2; x >= 0; x--)
                    {
                        if (numbers[x, y] != null)
                        {
                            numbers[x, y].Move(directionX, directionY);   //------>>>3
                        }
                    }
                }
            }
        }
        else if (directionX == -1)
        {
            for (int y = 0; y < 4; y++)
            {
                for (int x = 1; x < 4; x++)
                {
                    if (numbers[x, y] != null)
                    {
                        numbers[x, y].Move(directionX,  directionY);       //------>>>3
                    }
                }
            }
        }
        else if (directionY == 1)
        {
            for (int x = 0; x < 4; x++)
            {
                for (int y = 2; y >= 0; y--)
                {
                    if (numbers[x, y] != null)
                    {
                        numbers[x, y].Move( directionX, directionY);        //------>>>3
                    }
                }
            }
        }
        else if (directionY == -1)
        {
            for (int x = 0; x < 4; x++)
            {
                for (int y = 1; y < 4; y++)
                {
                    if (numbers[x, y] != null)
                    {
                        numbers[x, y].Move(directionX,  directionY);        //------>>>3
                    }
                }
            }

        }

    }

    public bool isEmpty(int x,int y)
    {
        if (x < 0 || x >= 4 || y < 0 || y >= 4)
        {
            return false;
        }
        else if (numbers[x, y] != null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public void isMusic()
    {
        if (playMusic)
        {
            playMusic = false;
            audio.Stop();
            music.mainTexture = musicOff;
            PlayerPrefs.SetInt("playMusic", 0);
        }
        else
        {
            playMusic = true;
            audio.Play();
            music.mainTexture = musicOn;
            PlayerPrefs.SetInt("playMusic", 1);
        }
    }

    public void Restart()   //重新开始
    {
        for (int x = 0; x < 4; x++)
        {
            for (int y = 0; y < 4; y++)
            {
                if (numbers[x, y] != null)
                {
                    Destroy(numbers[x, y].gameObject);
                    numbers[x, y] = null;
                }
            }
        }
        Instantiate(numberPrefab);
        Instantiate(numberPrefab);
    }

    public void gameWin()
    {
        control = false;
        gameWinPage.SetActive(true);
    }

    public void continueGame()
    {
        control = true;
        gameWinPage.SetActive(false);
        gameLosePage.SetActive(false);
        Restart();

    }

    public bool isDead()  //游戏失败判断
    {
        for (int x = 0; x < 4; x++)
        {
            for (int y = 0; y < 4; y++)
            {
                if (numbers[x, y] == null)
                {
                    return false;
                }
            }
        }
        
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 3; x++)
                {
                    if (numbers[x, y].value == numbers[x+1, y].value)
                {
                    return false;
                }
            }
        }
            for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    if (numbers[x, y].value == numbers[x , y+1].value)
                    {
                        return false;
                    }
                }
            }
            return true;
    }

    public void GameLose()
    {
        control = false;
        gameLosePage.SetActive(true);
    }

    IEnumerator resetExitMessage()
    {
        yield return new WaitForSeconds(0.5f);
        if (ExitMassage != null)
        {
            Destroy(ExitMassage);
        }
    }
}


Numbers.cs

using UnityEngine;
using System.Collections;

public class Numbers : MonoBehaviour {

	public int value;         //方块的值
	public int positionX;     //位置
	public int positionY;
    public UISprite mySprite;    //方块
    public TweenPosition tp;      //动画

    private bool isMoving = true;    

    private bool toDestroy = false;
    public bool hasMixed = false;     //是否已经合并过

    
	// Use this for initialization
	void Start () {

        value = Random.value > 0.2f ? 2 : 4;             //随机生成2或4
        mySprite.spriteName = value.ToString();          //随机位置
        do
        {
            positionX = Random.Range(0, 4);
            positionY = Random.Range(0, 4);
            
        } while (Manager._instance.numbers[positionX, positionY] != null);

        transform.localPosition = new Vector2(-150 + positionX * 100, -115 + positionY * 100);
        Manager._instance.numbers[positionX, positionY] = this;

        tp.from = new Vector2(-150 + positionX * 100, -115 + positionY * 100);
        tp.to = new Vector2(-150 + positionX * 100, -115 + positionY * 100);

        if (Manager._instance.isDead())                 //生成后判断是否游戏失败
        {
            Manager._instance.GameLose();
        }
	}
	
	// Update is called once per frame
	void Update () {
        if (!isMoving)
        {
        if (transform.localPosition != new Vector3(-150 + positionX * 100, -115 + positionY * 100, 0))
        {
            isMoving = true;
                tp.from = transform.localPosition;
                tp.to = new Vector3(-150 + positionX * 100, -115 + positionY * 100, 0);
                tp.ResetToBeginning();
                tp.PlayForward();
            }
        }
	
	}

public void SetIsMovingFalse(){   
    isMoving = false;
}

//移动逻辑 

public void Move(int directionX, int directionY)    //------>>>3
{
    if (directionX == 1)
        {
            Debug.Log("Right");
            int index = 1;
            while (Manager._instance.isEmpty(positionX + index, positionY)) //判断右边是否为空
            {
                index++;
            }
            if (index > 1)
            {
                if (!Manager._instance.isMovingNumbers.Contains(this))
                {
                    Manager._instance.isMovingNumbers.Add(this);
                }
                Manager._instance.hasMove = true;
                Manager._instance.numbers[positionX, positionY] = null;
                positionX = positionX + index - 1;
                Manager._instance.numbers[positionX, positionY] = this;
            }

            if (positionX < 3 
                && value == Manager._instance.numbers[positionX + 1, positionY].value 
                && !Manager._instance.numbers[positionX + 1, positionY].hasMixed)
            {
                Manager._instance.numbers[positionX + 1, positionY].hasMixed = true;
                if (!Manager._instance.isMovingNumbers.Contains(this))
                {
                    Manager._instance.isMovingNumbers.Add(this);
                }
                Manager._instance.hasMove = true;
                toDestroy = true;
                Manager._instance.numbers[positionX + 1, positionY].value *= 2;
                Manager._instance.numbers[positionX, positionY] = null;
                positionX += 1;
                
            }
           
        }
        else if (directionX == -1)
        {
            Debug.Log("Left");
            int index = 1;
            while (Manager._instance.isEmpty(positionX - index, positionY))
            {
                index++;
            }
            if (index > 1)
            {
                if (!Manager._instance.isMovingNumbers.Contains(this))
                {
                    Manager._instance.isMovingNumbers.Add(this);
                }
                Manager._instance.hasMove = true;
                Manager._instance.numbers[positionX, positionY] = null;
                positionX = positionX - index + 1;
                Manager._instance.numbers[positionX, positionY] = this;
            }

            if (positionX > 0 
                && value == Manager._instance.numbers[positionX - 1, positionY].value 
                && !Manager._instance.numbers[positionX - 1, positionY].hasMixed)
            {
                Manager._instance.numbers[positionX - 1, positionY].hasMixed = true;
                if (!Manager._instance.isMovingNumbers.Contains(this))
                {
                    Manager._instance.isMovingNumbers.Add(this);
                }
                Manager._instance.hasMove = true;
                toDestroy = true;
                Manager._instance.numbers[positionX - 1, positionY].value *= 2;
                Manager._instance.numbers[positionX, positionY] = null;
                positionX -= 1;

            }
            
        }
        else if (directionY == 1)
        {
            Debug.Log("Up");
            int index = 1;
            while (Manager._instance.isEmpty(positionX, positionY +index))
            {
                index++;
            }
            if (index > 1)
            {
                if (!Manager._instance.isMovingNumbers.Contains(this))
                {
                    Manager._instance.isMovingNumbers.Add(this);
                }
                Manager._instance.hasMove = true;
                Manager._instance.numbers[positionX, positionY] = null;
                positionY = positionY + index - 1;
                Manager._instance.numbers[positionX, positionY] = this;
            }

            if (positionY < 3 
                && value == Manager._instance.numbers[positionX, positionY + 1].value 
                && !Manager._instance.numbers[positionX, positionY + 1].hasMixed)
            {
                Manager._instance.numbers[positionX, positionY + 1].hasMixed = true;
                if (!Manager._instance.isMovingNumbers.Contains(this))
                {
                    Manager._instance.isMovingNumbers.Add(this);
                }
                Manager._instance.hasMove = true;
                toDestroy = true;
                Manager._instance.numbers[positionX, positionY+1].value *= 2;
                Manager._instance.numbers[positionX, positionY] = null;
                positionY += 1;
            }

           
        }
        else if (directionY == -1)
        {
            Debug.Log("Down");
            int index = 1;
            while (Manager._instance.isEmpty(positionX, positionY - index))
            {
                index++;
            }
            if (index > 1)
            {
                if (!Manager._instance.isMovingNumbers.Contains(this))
                {
                    Manager._instance.isMovingNumbers.Add(this);
                }
                Manager._instance.hasMove = true;
                Manager._instance.numbers[positionX, positionY] = null;
                positionY = positionY - index + 1;
                Manager._instance.numbers[positionX, positionY] = this;
            }

            if (positionY > 0 
                && value == Manager._instance.numbers[positionX, positionY - 1].value 
                && !Manager._instance.numbers[positionX, positionY - 1].hasMixed)
            {
                Manager._instance.numbers[positionX, positionY - 1].hasMixed = true;
                if (!Manager._instance.isMovingNumbers.Contains(this))
                {
                    Manager._instance.isMovingNumbers.Add(this);
                }
                Manager._instance.hasMove = true;
                toDestroy = true;
                Manager._instance.numbers[positionX, positionY - 1].value *= 2;
                Manager._instance.numbers[positionX, positionY] = null;
                positionY -= 1;

            }
           

        }

    }

public void MoveOver() //移动结束
{
    if (toDestroy)
    {
        Destroy(this.gameObject);
        Manager._instance.numbers[positionX, positionY].mySprite.spriteName =
            Manager._instance.numbers[positionX, positionY].value.ToString();
        if (Manager._instance.numbers[positionX, positionY].value == 2048)
        {
            Manager._instance.gameWin();
        }
    }
    Manager._instance.isMovingNumbers.Remove(this);
}
}




视频地址:http://v.qq.com/page/o/9/v/o016135zp9v.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: