Unity經典游戲教程之:貪吃蛇


版權聲明:

  • 本文原創發布於博客園"優夢創客"的博客空間(網址:http://www.cnblogs.com/raymondking123/)以及微信公眾號"優夢創客"(微信號:unitymaker)
  • 您可以自由轉載,但必須加入完整的版權聲明!

貪吃蛇與方塊

  • 主要玩法:貪吃蛇吃食物,吃到食物后根據相應數值增加身體長度,如果貪吃蛇碰到方塊后,根據方塊的數值逐漸減少貪吃蛇身體長度,如果貪吃蛇長度為0時,游戲結束,場景中會有一下牆壁阻擋貪吃蛇的左右移動。
  • 角色操作:點擊鼠標左鍵,並在屏幕上左右移動,可以控制貪吃蛇在場景中左右移動,貪吃蛇要吃食物,並且要躲避方塊,或者消除方塊。
  • 游戲計分規則:貪吃蛇要撞擊方塊(消除方塊),方塊上都有對應數值,貪吃蛇消除一個方塊,根據方塊上的數值增加分數。
  • 原版游戲畫面:

image
image
image
image
image

實際游戲運行畫面:

image
image
image

通過腳本固定窗口大小(此腳本可以掛在到任意的游戲對象上):

	void Start () 
    {
        Screen.SetResolution(768, 1024, false);
	}

貪吃蛇生成:

    public void SpawnBodyParts()
    {
        firstPart = true;

        //Add the initial BodyParts
        for (int i = 0; i < initialAmount; i++)
        {
            //Use invoke to avoid a weird bug where the snake goes down at the beginning.
            Invoke("AddBodyPart", 0.1f);
        }
    }

image
image

貪吃蛇移動:

  • 主要就是檢測鼠標是否在屏幕上,通過鼠標左鍵按下並左右移動來控制貪吃蛇的左右移動,並不用定位坐標,僅檢測鼠標左右移動來控制貪吃蛇的左右移動。
  • 貪吃蛇可以會自動向前移動,碰到方塊會停頓一下,方塊消除后並且貪吃蛇不為0,則貪吃蛇繼續向前移動,如果貪吃蛇為0時,游戲結束。
    float curSpeed = speed;
    
            //Always move the body Up
            if(BodyParts.Count > 0)
                BodyParts[0].Translate(Vector2.up * curSpeed * Time.smoothDeltaTime);
    
    
            //check if we are still on screen
            float maxX = Camera.main.orthographicSize * Screen.width / Screen.height;
    
            if (BodyParts.Count > 0)
            {
                if (BodyParts[0].position.x > maxX) //Right pos
                {
                    BodyParts[0].position = new Vector3(maxX - 0.01f, BodyParts[0].position.y, BodyParts[0].position.z);
                }
                else if (BodyParts[0].position.x < -maxX) //Left pos
                {
                    BodyParts[0].position = new Vector3(-maxX + 0.01f, BodyParts[0].position.y, BodyParts[0].position.z);
                }
            }
    
    

貪吃蛇吃食物:

  • 貪吃蛇吃到食物后,根據食物上的數值增加身體。

    public class FoodBehavior : MonoBehaviour 
    {
    
        [Header("Snake Manager")]
        SnakeMovement SM;
    
        [Header("Food Amount")]
        public int foodAmount;
    
        // Use this for initialization
        void Start () 
        {
            SM = GameObject.FindGameObjectWithTag("SnakeManager").GetComponent<SnakeMovement>();
    
            foodAmount = Random.Range(1, 10);
    
            transform.GetComponentInChildren<TextMesh>().text = "" + foodAmount;
        }
    
        // Update is called once per frame
        void Update () 
        {
            if (SM.transform.childCount > 0 && transform.position.y - SM.transform.GetChild(0).position.y < -10)
                Destroy(this.gameObject);
        }
    
        private void OnTriggerEnter2D(Collider2D collision)
        {
            Destroy(this.gameObject);
        }
    }
    

菜單系統:

image

計分:

```
	void Start () 
    {
        //Initially, set the menu and Score is null
        
        SetMenu();
        SCORE = 0;

        //Initialize some booleans
        speedAdded = false;

        //Load the best score
        BESTSCORE = PlayerPrefs.GetInt("BESTSCORE");
	}

```

粒子系統:

-貪吃蛇碰撞方塊就會觸發粒子系統。

image

攝像機控制:

```
public class CameraMovement : MonoBehaviour 
{

    [Header("Snake Container")]
    public Transform SnakeContainer;

    Vector3 initialCameraPos;

	// Use this for initialization
	void Start () 
    {

        initialCameraPos = transform.position;	
	}
	
	// Update is called once per frame
	void Update () 
    {
        if(SnakeContainer.childCount > 0)
            transform.position = Vector3.Slerp(transform.position, 
                (initialCameraPos + new Vector3(0,SnakeContainer.GetChild(0).position.y - Camera.main.orthographicSize/2,0)),
                0.1f);
	}
}
```

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM