Unity NGUI实现2048(一)源代码

来源:互联网 发布:推荐淘宝女装质量好的 编辑:程序博客网 时间:2024/06/03 22:41

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 initializationvoid 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 framevoid 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






0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝喜欢打人怎么办2岁 1岁宝宝动手打人怎么办 孩子总打人总哭怎么办 小孩出现夜惊家人怎么办 小孩不原跟家人沟通怎么办 孩子字写得难看怎么办 孩子上一年级不认识字怎么办 二年级孩子语文差怎么办 孩子二年级语文成绩差怎么办 孩子小学二年级语文差怎么办 二年级孩子语文理解能力差怎么办 深圳租房被坑了怎么办 小鸣单车押金退不了怎么办 联想台式一体机忘记密码怎么办 ps直线工具变成箭头了怎么办 笔记本图形处理速度慢怎么办 微信语音发不出去怎么办 ps里的图层锁定怎么办 ps图层丢失了怎么办 PS标题画面太小怎么办 轮胎蹭掉一块皮怎么办 吃香蕉吃的胃难受怎么办 qq糖卡在喉咙里怎么办 头发上粘到了qq糖怎么办 老房子土墙掉土怎么办 速写画的太慢怎么办 艺术生文化课没过线怎么办 5岁儿童坐飞机忘带证件怎么办 儿童坐飞机没带证件怎么办 儿童坐飞机没带户口本怎么办 儿童坐飞机没有带户口本怎么办 刚打蜡的车下雨怎么办 飞机票不能退票不能改签怎么办 深圳航空买机票姓名错了怎么办 大众cc打不着火怎么办 手上扎了仙人掌刺怎么办 pscs5界面字体太小怎么办 儿童做飞机没带户口本怎么办 黑户口想做飞机怎么办 宝宝坐飞机忘记带证件怎么办 值机柜台关闭了怎么办