亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Unity游戲開發(fā)之2048游戲的實(shí)現(xiàn)

 更新時(shí)間:2022年03月22日 14:21:03   作者:恬靜的小魔龍  
2048是一款數(shù)字益智游戲,初始數(shù)字則是由2+2組成的基數(shù)4。在操作方面的不同則表現(xiàn)為一步一格的移動(dòng),變成更為爽快的一次到底。相同數(shù)字的方?jīng)r在靠攏、相撞時(shí)會(huì)相加。本文將通過Unity3D實(shí)現(xiàn)這一游戲,需要的可以參考一下

一、前言

寫今天這篇文章的緣由,其實(shí)是來自于前段時(shí)間和粉絲的一個(gè)聊天,最近他打算參加游戲創(chuàng)作大賽,問我需要準(zhǔn)備學(xué)習(xí)什么知識(shí),以及參加比賽的注意事項(xiàng)一類:

我相信因?yàn)闊釔塾螒蚨皝硗渡碛赨3D學(xué)習(xí)的粉絲想來不是少數(shù),興趣可以驅(qū)動(dòng)學(xué)習(xí),在完善自己心愛游戲的過程中,要不斷的去學(xué)習(xí),不斷的提高自己。

而參與游戲設(shè)計(jì)比賽,更是提高自身技術(shù)實(shí)力、增長(zhǎng)眼界見識(shí)的優(yōu)秀途徑,在此途中也能遇見不少志同道合的好朋友。

那今天就借此簡(jiǎn)單的聊一下,制作一個(gè)簡(jiǎn)單游戲需要具備哪些技術(shù)棧,以及參加這類比賽需要的知識(shí)基礎(chǔ)。

并且,還有休閑類游戲《2048》的實(shí)戰(zhàn)開發(fā)內(nèi)容。

二、游戲開發(fā)知識(shí)儲(chǔ)備

接下來就聊一下做一個(gè)游戲需要具備哪些技術(shù)棧, 因?yàn)橹皇敲鎸?duì)“做出游戲”這個(gè)特定需求,所以有些技術(shù)沒有列舉出來,在開發(fā)中遇到可以再學(xué)習(xí)。

2-1技術(shù)棧

Unity3D安裝

首先,學(xué)習(xí)Unity3D你要安裝Unity3D編輯器吧,激活Unity Hub許可證。

分享 幾個(gè)下載鏈接:

Unity3D中國版官網(wǎng)

Unity3D國際版官網(wǎng)

Unity3D下載鏈接

當(dāng)然,比較推薦是用Unity Hub下載安裝:

安裝后激活許可證,這個(gè)網(wǎng)上教程比較多,這里就不贅述了:

Unity3D編輯器布局學(xué)習(xí)

學(xué)習(xí)Unity3D編輯器,要先熟悉Unity編輯器的布局,比如菜單欄,工具欄在什么位置:

Unity入門組件學(xué)習(xí)

Unity的特色就是面向組件編程,所以對(duì)于組件的熟悉是基本的要求。

比較常用的組件有:

Audio組

  • AudioSource
  • AudioListener

Effects組

  • ParticleSystem
  • LineRenderer
  • TrailRenderer

Event組

  • EventSystem
  • EventTrigger
  • GraphicRaycaster

Layout組

  • RectTransform
  • Canvas
  • CanvasGroup
  • CanvasScaler
  • AspectRatioFitter
  • ContentSizeFitter
  • GridLayoutGroup
  • HorizontalLayoutGroup
  • VerticleLayoutGroup
  • LayoutElement

Mesh組

  • MeshFilter
  • MeshRenderer
  • SkinnedMeshRenderer
  • TextMeshPro-Text

Miscellaneous組

  • Animator
  • Animation

Nevigation組

  • NavMeshAgent
  • NavMeshObstacle

Physics2D組

  • Rigidbody2D
  • BoxCollider2D
  • CapsuleCollider2D
  • CircleCollider2D
  • CompositeCollider2D
  • EdgeCollider2D

Physics組

  • Rigidbody
  • BoxCollider
  • CapsuleCollider
  • SphereCollider
  • CharacterController
  • TerrainCollider

Rendering組

  • Camera
  • CanvasRenderer
  • SpriteRenderer
  • Skybox
  • SortingGroup
  • Light

組件比較多,但是不需要全部學(xué)習(xí),只需要在用到的時(shí)候查看一下Unity3D手冊(cè)即可:

Unity3D中文手冊(cè)https://docs.unity.cn/cn/current/Manual/index.html

C#語言基礎(chǔ)

Unity3D的腳本語言是C#,隨意要做好Unity3D開發(fā),C#編程語言肯定要學(xué)的。

最基本的面向?qū)ο笏枷?,?shù)據(jù)類型、流程控制、數(shù)組、函數(shù)都要有一定的了解。

Unity3D-UGUI

UGUI是Unity3D的UI系統(tǒng),可以用UGUi搭建界面,然后很多的用戶交互邏輯都是在UI上完成的,所以學(xué)好UGUI是很有必要的,下面就來看一下UGUI的基本組件:

  • Text
  • Image
  • RawImage
  • Button
  • Toggle
  • Slider
  • Scrollbar
  • Dropdown
  • InputField
  • Canvas
  • Panel
  • ScrollView

數(shù)據(jù)結(jié)構(gòu)

數(shù)據(jù)結(jié)構(gòu)是一種具有一定邏輯關(guān)系,在計(jì)算機(jī)中應(yīng)用某種存儲(chǔ)結(jié)構(gòu),并且封裝了相應(yīng)操作的數(shù)據(jù)元素的集合。

簡(jiǎn)單點(diǎn)說就是專門用于數(shù)據(jù)存儲(chǔ)和檢索的類。

因?yàn)樽鲇螒蜷_發(fā)或者其他項(xiàng)目都會(huì)用到對(duì)數(shù)據(jù)的操作,而數(shù)據(jù)結(jié)構(gòu)就是專門用于數(shù)據(jù)存儲(chǔ)和檢索的類。

常見的數(shù)據(jù)結(jié)構(gòu)有:

  • 列表List
  • 數(shù)組Array
  • 字典Dictionary
  • 棧Stack
  • 隊(duì)列Queue

總結(jié)的技術(shù)??粗譀]有那么多,但是內(nèi)容還是很多的,比如說Unity3D的組件、C#腳本語言、UGUI系統(tǒng),每一塊拿出來都是很多的內(nèi)容。

三、休閑類游戲《2048》開發(fā)實(shí)戰(zhàn)

3-1玩法概述

開局是一個(gè)4X4棋盤,棋盤中隨機(jī)出現(xiàn)兩個(gè)數(shù)字,出現(xiàn)的數(shù)字為2或4。

玩家通過選擇上下左右四個(gè)反向,若玩家選擇的方向上有相同的數(shù)字則合并,若選擇的反向上沒有相同的數(shù)字但是有空位則移動(dòng),可以移動(dòng)的同時(shí)合并,但是不可以連續(xù)合并。

若棋盤被數(shù)字填滿,無法進(jìn)行移動(dòng)或合并,則游戲失敗。

如果棋盤出現(xiàn)2048,則游戲勝利:

3-2實(shí)現(xiàn)分析

根據(jù)規(guī)則,來分析一下游戲的實(shí)現(xiàn)。

以向左移動(dòng)為例:

可以看到移動(dòng)后的規(guī)則:

相鄰的元素值相同則合并,然后末尾補(bǔ)上空元素

開頭或中間有空元素就移動(dòng)到末尾

只合并一次,不會(huì)多次合并

3-3搭建場(chǎng)景

(1)新建項(xiàng)目我用的Unity 2019.4.7f1版本,模板選擇2D,設(shè)置項(xiàng)目名稱和位置:

(2)將圖片另存為,然后導(dǎo)入到項(xiàng)目中:

(3)在Inspector視圖中導(dǎo)入設(shè)置:

(4)分割圖片,點(diǎn)擊Sprite Editor后,會(huì)出現(xiàn)分割窗口,然后點(diǎn)擊Slice,選擇Grid By Cell Count,點(diǎn)擊Apply:

分割后:

(5)切完后,把圖集放到Resources文件夾中,沒有就新建一個(gè),Resources文件夾在Unity中有特殊作用,可以通過腳本Resources類進(jìn)行訪問。

PS:注意圖集的名字2048,稍后代碼會(huì)用到。

接著將2048_0拖入場(chǎng)景中,重命名為BG,設(shè)置排序?qū)覱rder inLayer為-1,這樣背景就總是顯示在最下面了:

將BG拖入Project視圖,做成預(yù)制體:

同樣的操作,將2048_1拖入場(chǎng)景,然后重命名為Card,設(shè)置排序?qū)訛?,然后做成預(yù)制體:

將場(chǎng)景中的BG和Card對(duì)象刪除,我們將在后面使用實(shí)例化新建出來。

至此,我們的場(chǎng)景就搭建完成了,下面進(jìn)行代碼的編寫。

3-4實(shí)現(xiàn)代碼

(1)首先,我們需要?jiǎng)?chuàng)建16個(gè)小方格作為背景,新建腳本GameManager.cs,雙擊打開腳本,編寫代碼:

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

public class GameManager : MonoBehaviour
{
    public GameObject bgSprite;//背景卡片
    private Vector2 BeginPos = new Vector2(-1.5f, 1.5f);//差不多在屏幕中間
    private float OffsetX = 1.1f;//xy 加個(gè)0.1 有個(gè)間隙
    private float OffsetY = 1.1f;

    void Start()
    {
        CreateBG();
    }

    void CreateBG()
    {
        GameObject BG = new GameObject("BG");//創(chuàng)建空游戲?qū)ο笞鳛楸尘邦A(yù)制體
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                Vector2 newPos = new Vector2(BeginPos.x + j * OffsetX, BeginPos.y - i * OffsetY);
                Instantiate(bgSprite, newPos, Quaternion.identity, BG.transform);
            }
        }
    }
}

將腳本附給Main Camera對(duì)象,然后將預(yù)制體BG拖到Card腳本組件的BgSprite卡槽中:

運(yùn)行程序:

(2)接下來創(chuàng)建一個(gè)Card.cs腳本,主要用來管理創(chuàng)建的Card對(duì)象,找到Project視圖中的Card對(duì)象,將Card.cs腳本附加上去:

修改Card.cs腳本:

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

public class Card : MonoBehaviour
{
    public Sprite[] CardSprites;//讀取圖集中所有切片
    private string fileName = "2048";//圖集的名字
    public int _currentIndex = 0;//當(dāng)前卡片的顯示編號(hào)

    void Awake()
    {
        CardSprites = Resources.LoadAll<Sprite>(fileName);
    }


    //根據(jù)卡片編號(hào)修改SpriteRendere,從而改變卡片的數(shù)值
    public void Generate(int index)
    {
        _currentIndex = index;
        GetComponent<SpriteRenderer>().sprite = CardSprites[_currentIndex];
    }

    //合并卡片的邏輯
    public void Merge()
    {
        _currentIndex++;
        GetComponent<SpriteRenderer>().sprite = CardSprites[_currentIndex];
    }
}

(3)接下來修改GameManager.cs腳本,雙擊打開腳本,修改代碼:

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

public class GameManager : MonoBehaviour
{
    public GameObject card;//卡片對(duì)象
    private GameObject[,] cardList = new GameObject[4, 4];//卡片游戲?qū)ο髮?duì)應(yīng)的棋盤格子
    private int CardNum = 0;//棋盤格子的卡片計(jì)數(shù),用于滿格后重新開始游戲

    void Start()
    {
        CreateBG();
        CreateCard();
    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))//上
        {
            MoveUp();
            CreateCard();
        }
        if (Input.GetKeyDown(KeyCode.S))//下
        {
            MoveUp();
            MoveDown();
        }
        if (Input.GetKeyDown(KeyCode.A))//左
        {
            MoveLeft();
            CreateCard();
        }
        if (Input.GetKeyDown(KeyCode.D))//右
        {
            MoveRight();
            CreateCard();
        }
    }
    
	void CreateBG()
    {
        GameObject BG = new GameObject("BG");//創(chuàng)建空游戲?qū)ο笞鳛楸尘邦A(yù)制體
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                Vector2 newPos = new Vector2(BeginPos.x + j * OffsetX, BeginPos.y - i * OffsetY);
                Instantiate(bgSprite, newPos, Quaternion.identity, BG.transform);
            }
        }
    }

    void CreateCard()
    {

    }

    void MoveUp()
    {

    }

    void MoveDown()
    {

    }

    void MoveLeft()
    {

    }

    void MoveRight()
    {

    }
}

CreateCard函數(shù)的主要實(shí)現(xiàn)思路是:

  • 每次申城一個(gè)卡片時(shí)計(jì)算當(dāng)前數(shù)組中的卡牌數(shù),如果大于16則游戲重置
  • 否則隨機(jī)生成坐標(biāo)點(diǎn),判斷當(dāng)前當(dāng)前坐標(biāo)是否有卡片,直到找到空余位置,實(shí)例化卡片
  • 生成對(duì)應(yīng)數(shù)字的卡片
	void CreateCard()
    {
        CardNum = 0;
        foreach (var item in cardList)
        {
            if (item)
            {
                CardNum++;
            }
        }
        if (CardNum >= 16)
        {
            ResetGame();
            return;
        }

        int X_index, Y_index = 0;
        do
        {
            X_index = Random.Range(0, 4);
            Y_index = Random.Range(0, 4);
        } while (cardList[X_index, Y_index]);
        Vector2 newPos = GetPosVector2(X_index, Y_index);

        cardList[X_index, Y_index] = Instantiate(_card, newPos, Quaternion.identity);
        if (Random.Range(0.0f,1.0f)>0.5f)
        {
            cardList[X_index, Y_index].GetComponent<Card>().Generate(1);
        }
        else
        {
            cardList[X_index, Y_index].GetComponent<Card>().Generate(2);
        }
    }

    public Vector2 GetPosVector2(int x, int y)
    {
        return new Vector2(BeginPos.x + y * OffsetX, BeginPos.y - x * OffsetY);
    }


    void ResetGame()
    {
        foreach (var card in cardList)
        {
            if (card != null)
            {
                Destroy(card);
            }
            cardList = new GameObject[4, 4];
        }
    }

(4)接下來就是響應(yīng)鼠標(biāo)輸入,然后移動(dòng)卡片了,以向上移動(dòng)為例,主要邏輯思路如下:

遍歷卡片數(shù)組,尋找存在的卡片

尋找到一個(gè)存在的卡片,沿著移動(dòng)方向上每一個(gè)格子做判斷,看前面是否有卡片,找到離自己最近的卡片做判斷

如果與本身卡片數(shù)值一直,則進(jìn)行Card類中的Merge方法進(jìn)行合并

否則,將卡片移動(dòng)到該格子的臨近格子

向上移動(dòng)代碼實(shí)現(xiàn)如下:

	void MoveUp()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (cardList[i, j] != null)//當(dāng)找到其中的卡片后
                {
                    GameObject temp = cardList[i, j];//保留該卡片的引用
                    int x = i;
                    int y = j;
                    bool isFind = false;//設(shè)置查找標(biāo)識(shí)
                    while (!isFind)
                    {
                        x--;//根據(jù)方向的不同  x--是向上 x++是向下
                        if (x < 0 || cardList[x, y])//達(dá)到邊界或找到卡片后
                        {
                            isFind = true;
                            //判斷值是否相同,相同的話合并操作
                            if (x >= 0 && cardList[x, y].GetComponent<Card>()._currentIndex == cardList[i, j].GetComponent<Card>()._currentIndex)
                            {
                                cardList[x, y].GetComponent<Card>().Merge();
                                Destroy(cardList[i, j]);
                                cardList[i, j] = null;
                            }
                            else//否則移動(dòng)即可
                            {
                                cardList[i, j] = null;
                                cardList[x + 1, y] = temp;
                                cardList[x + 1, y].transform.position = GetPosVector2(x + 1, y);
                            }
                        }
                    }
                }
            }
        }
    }

向下移動(dòng):

	void MoveDown()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (cardList[i, j] != null)//當(dāng)找到其中的卡片后
                {
                    GameObject temp = cardList[i, j];//保留該卡片的引用
                    int x = i;
                    int y = j;
                    bool isFind = false;//設(shè)置查找標(biāo)識(shí)
                    while (!isFind)
                    {
                        x++;//根據(jù)方向的不同  x--是向上 x++是向下
                        if (x > 3 || cardList[x, y])//達(dá)到邊界或找到卡片后
                        {
                            isFind = true;
                            //判斷值是否相同,相同的話合并操作
                            if (x <= 3 && cardList[x, y].GetComponent<Card>()._currentIndex == cardList[i, j].GetComponent<Card>()._currentIndex)
                            {
                                cardList[x, y].GetComponent<Card>().Merge();
                                Destroy(cardList[i, j]);
                                cardList[i, j] = null;
                            }
                            else//否則移動(dòng)即可
                            {
                                cardList[i, j] = null;
                                cardList[x - 1, y] = temp;
                                cardList[x - 1, y].transform.position = GetPosVector2(x - 1, y);
                            }
                        }
                    }
                }
            }
        }
    }

向左移動(dòng):

	void MoveLeft()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (cardList[i, j] != null)//當(dāng)找到其中的卡片后
                {
                    GameObject temp = cardList[i, j];//保留該卡片的引用
                    int x = i;
                    int y = j;
                    bool isFind = false;//設(shè)置查找標(biāo)識(shí)
                    while (!isFind)
                    {
                        y--;//根據(jù)方向的不同  x--是向上 x++是向下
                        if (y < 0 || cardList[x, y])//達(dá)到邊界或找到卡片后
                        {
                            isFind = true;
                            //判斷值是否相同,相同的話合并操作
                            if (y >= 0 && cardList[x, y].GetComponent<Card>()._currentIndex == cardList[i, j].GetComponent<Card>()._currentIndex)
                            {
                                cardList[x, y].GetComponent<Card>().Merge();
                                Destroy(cardList[i, j]);
                                cardList[i, j] = null;
                            }
                            else//否則移動(dòng)即可
                            {
                                cardList[i, j] = null;
                                cardList[x, y + 1] = temp;
                                cardList[x, y + 1].transform.position = GetPosVector2(x, y + 1);
                            }
                        }
                    }
                }
            }
        }
    }

向右移動(dòng):

	void MoveRight()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (cardList[i, j] != null)//當(dāng)找到其中的卡片后
                {
                    GameObject temp = cardList[i, j];//保留該卡片的引用
                    int x = i;
                    int y = j;
                    bool isFind = false;//設(shè)置查找標(biāo)識(shí)
                    while (!isFind)
                    {
                        y++;//根據(jù)方向的不同  x--是向上 x++是向下
                        if (y > 3 || cardList[x, y])//達(dá)到邊界或找到卡片后
                        {
                            isFind = true;
                            //判斷值是否相同,相同的話合并操作
                            if (y <= 3 && cardList[x, y].GetComponent<Card>()._currentIndex == cardList[i, j].GetComponent<Card>()._currentIndex)
                            {
                                cardList[x, y].GetComponent<Card>().Merge();
                                Destroy(cardList[i, j]);
                                cardList[i, j] = null;
                            }
                            else//否則移動(dòng)即可
                            {
                                cardList[i, j] = null;
                                cardList[x, y - 1] = temp;
                                cardList[x, y - 1].transform.position = GetPosVector2(x, y - 1);
                            }
                        }
                    }
                }
            }
        }
    }

運(yùn)行游戲,就可以愉快的玩耍了:

到這一步,我們就完成了《2048》游戲的核心代碼實(shí)現(xiàn),但是我們的代碼只有滿格后重新開始游戲的判斷,沒有游戲完成的判斷,下面加一個(gè)游戲成功的代碼,繼續(xù)修改GameManager.cs腳本:

	void CreateCard()
    {
        CardNum = 0;
        foreach (var item in cardList)
        {
            if (item)
            {
                CardNum++;
            }
        }
        if (CardNum >= 16)
        {
            ResetGame();
            return;
        }

        int X_index, Y_index = 0;
        do
        {
            X_index = Random.Range(0, 4);
            Y_index = Random.Range(0, 4);
        } while (cardList[X_index, Y_index]);
        Vector2 newPos = GetPosVector2(X_index, Y_index);

        cardList[X_index, Y_index] = Instantiate(_card, newPos, Quaternion.identity);
        if (Random.Range(0.0f,1.0f)>0.5f)
        {
            cardList[X_index, Y_index].GetComponent<Card>().Generate(1);
        }
        else
        {
            cardList[X_index, Y_index].GetComponent<Card>().Generate(2);
        }
        SucceedGame();
    }
	void SucceedGame()
    {
        foreach (var item in cardList)
        {
            if (item.GetComponent<Card>()._currentIndex >= 11)
            {
                Debug.Log("游戲成功");
                break;
            }
        }
        ResetGame();
    }

整體GameManager.cs腳本代碼:

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

public class GameManager : MonoBehaviour
{
    public GameObject bgSprite;//背景卡片
    private Vector2 BeginPos = new Vector2(-1.5f, 1.5f);//差不多在屏幕中間
    private float OffsetX = 1.1f;//xy 加個(gè)0.1 有個(gè)間隙
    private float OffsetY = 1.1f;

    public GameObject _card;//卡片對(duì)象
    private GameObject[,] cardList = new GameObject[4, 4];//卡片游戲?qū)ο髮?duì)應(yīng)的棋盤格子
    private int CardNum = 0;//棋盤格子的卡片計(jì)數(shù),用于滿格后重新開始游戲

    void Start()
    {
        CreateBG();
        CreateCard();
        CreateCard();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))//上
        {
            MoveUp();
            CreateCard();
        }
        if (Input.GetKeyDown(KeyCode.S))//下
        {
            MoveDown();
            CreateCard();
        }
        if (Input.GetKeyDown(KeyCode.A))//左
        {
            MoveLeft();
            CreateCard();
        }
        if (Input.GetKeyDown(KeyCode.D))//右
        {
            MoveRight();
            CreateCard();
        }
    }

    void CreateBG()
    {
        GameObject BG = new GameObject("BG");//創(chuàng)建空游戲?qū)ο笞鳛楸尘邦A(yù)制體
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                Vector2 newPos = new Vector2(BeginPos.x + j * OffsetX, BeginPos.y - i * OffsetY);
                Instantiate(bgSprite, newPos, Quaternion.identity, BG.transform);
            }
        }
    }

    void CreateCard()
    {
        CardNum = 0;
        foreach (var item in cardList)
        {
            if (item)
            {
                CardNum++;
            }
        }
        if (CardNum >= 16)
        {
            ResetGame();
            return;
        }

        int X_index, Y_index = 0;
        do
        {
            X_index = Random.Range(0, 4);
            Y_index = Random.Range(0, 4);
        } while (cardList[X_index, Y_index]);
        Vector2 newPos = GetPosVector2(X_index, Y_index);

        cardList[X_index, Y_index] = Instantiate(_card, newPos, Quaternion.identity);
        if (Random.Range(0.0f,1.0f)>0.5f)
        {
            cardList[X_index, Y_index].GetComponent<Card>().Generate(1);
        }
        else
        {
            cardList[X_index, Y_index].GetComponent<Card>().Generate(2);
        }
        SucceedGame();
    }

    public Vector2 GetPosVector2(int x, int y)
    {
        return new Vector2(BeginPos.x + y * OffsetX, BeginPos.y - x * OffsetY);
    }


    void ResetGame()
    {
        foreach (var item in cardList)
        {
            if (item != null)
            {
                Destroy(item);
            }
            cardList = new GameObject[4, 4];
        }
    }

    void SucceedGame()
    {
        foreach (var item in cardList)
        {
            if (item.GetComponent<Card>()._currentIndex >= 11)
            {
                Debug.Log("游戲成功");
                break;
            }
        }
        ResetGame();
    }

    void MoveUp()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (cardList[i, j] != null)//當(dāng)找到其中的卡片后
                {
                    GameObject temp = cardList[i, j];//保留該卡片的引用
                    int x = i;
                    int y = j;
                    bool isFind = false;//設(shè)置查找標(biāo)識(shí)
                    while (!isFind)
                    {
                        x--;//根據(jù)方向的不同  x--是向上 x++是向下
                        if (x < 0 || cardList[x, y])//達(dá)到邊界或找到卡片后
                        {
                            isFind = true;
                            //判斷值是否相同,相同的話合并操作
                            if (x >= 0 && cardList[x, y].GetComponent<Card>()._currentIndex == cardList[i, j].GetComponent<Card>()._currentIndex)
                            {
                                cardList[x, y].GetComponent<Card>().Merge();
                                Destroy(cardList[i, j]);
                                cardList[i, j] = null;
                            }
                            else//否則移動(dòng)即可
                            {
                                cardList[i, j] = null;
                                cardList[x + 1, y] = temp;
                                cardList[x + 1, y].transform.position = GetPosVector2(x + 1, y);
                            }
                        }
                    }
                }
            }
        }
    }

    void MoveDown()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (cardList[i, j] != null)//當(dāng)找到其中的卡片后
                {
                    GameObject temp = cardList[i, j];//保留該卡片的引用
                    int x = i;
                    int y = j;
                    bool isFind = false;//設(shè)置查找標(biāo)識(shí)
                    while (!isFind)
                    {
                        x++;//根據(jù)方向的不同  x--是向上 x++是向下
                        if (x > 3 || cardList[x, y])//達(dá)到邊界或找到卡片后
                        {
                            isFind = true;
                            //判斷值是否相同,相同的話合并操作
                            if (x <= 3 && cardList[x, y].GetComponent<Card>()._currentIndex == cardList[i, j].GetComponent<Card>()._currentIndex)
                            {
                                cardList[x, y].GetComponent<Card>().Merge();
                                Destroy(cardList[i, j]);
                                cardList[i, j] = null;
                            }
                            else//否則移動(dòng)即可
                            {
                                cardList[i, j] = null;
                                cardList[x - 1, y] = temp;
                                cardList[x - 1, y].transform.position = GetPosVector2(x - 1, y);
                            }
                        }
                    }
                }
            }
        }
    }

    void MoveLeft()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (cardList[i, j] != null)//當(dāng)找到其中的卡片后
                {
                    GameObject temp = cardList[i, j];//保留該卡片的引用
                    int x = i;
                    int y = j;
                    bool isFind = false;//設(shè)置查找標(biāo)識(shí)
                    while (!isFind)
                    {
                        y--;//根據(jù)方向的不同  x--是向上 x++是向下
                        if (y < 0 || cardList[x, y])//達(dá)到邊界或找到卡片后
                        {
                            isFind = true;
                            //判斷值是否相同,相同的話合并操作
                            if (y >= 0 && cardList[x, y].GetComponent<Card>()._currentIndex == cardList[i, j].GetComponent<Card>()._currentIndex)
                            {
                                cardList[x, y].GetComponent<Card>().Merge();
                                Destroy(cardList[i, j]);
                                cardList[i, j] = null;
                            }
                            else//否則移動(dòng)即可
                            {
                                cardList[i, j] = null;
                                cardList[x, y + 1] = temp;
                                cardList[x, y + 1].transform.position = GetPosVector2(x, y + 1);
                            }
                        }
                    }
                }
            }
        }
    }

    void MoveRight()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (cardList[i, j] != null)//當(dāng)找到其中的卡片后
                {
                    GameObject temp = cardList[i, j];//保留該卡片的引用
                    int x = i;
                    int y = j;
                    bool isFind = false;//設(shè)置查找標(biāo)識(shí)
                    while (!isFind)
                    {
                        y++;//根據(jù)方向的不同  x--是向上 x++是向下
                        if (y > 3 || cardList[x, y])//達(dá)到邊界或找到卡片后
                        {
                            isFind = true;
                            //判斷值是否相同,相同的話合并操作
                            if (y <= 3 && cardList[x, y].GetComponent<Card>()._currentIndex == cardList[i, j].GetComponent<Card>()._currentIndex)
                            {
                                cardList[x, y].GetComponent<Card>().Merge();
                                Destroy(cardList[i, j]);
                                cardList[i, j] = null;
                            }
                            else//否則移動(dòng)即可
                            {
                                cardList[i, j] = null;
                                cardList[x, y - 1] = temp;
                                cardList[x, y - 1].transform.position = GetPosVector2(x, y - 1);
                            }
                        }
                    }
                }
            }
        }
    }
}

至此,我們的《2048》游戲就已經(jīng)完成了,愉快的玩耍吧。

以上就是Unity游戲開發(fā)之2048游戲的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Unity2048游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論