unity實現(xiàn)簡單貪吃蛇游戲
本文實例為大家分享了unity實現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
首先創(chuàng)建一個頭部,編寫腳本利用WASD控制頭部的移動。
Vector3 up=new Vector3(0,1,0); Vector3 down=new Vector3(0,-1,0); Vector3 left=new Vector3(-1,0,0); Vector3 right=new Vector3(1,0,0); Vector3 now;//頭部實際前進方向 float timer=0f; float timerGap=0.1f; void Start () { now = up; } void Update () { if (now!=up&&now!=down&&Input.GetKey (KeyCode.W)) { now = up; } if (now!=up&&now!=down&&Input.GetKey (KeyCode.S)) { now = down; } if (now!=left&&now!=right&&Input.GetKey (KeyCode.A)) { now=left; } if (now!=left&&now!=right&&Input.GetKey (KeyCode.D)) { now = right; } timer += Time.deltaTime; if (timer > timerGap) { //每隔0.1s向當前方向移動一個單位(0.5為頭部大小)。 timer = 0; transform.position = 0.5f * now + transform.position; } }
然后就是創(chuàng)建初始身體,實現(xiàn)身體跟隨頭部。采用的方法是將身體放進一個數(shù)組,然后下標0的身體移動到頭部之前的位置,然后下標 i 的身體移動到 i-1 的position。
創(chuàng)建初始身體,并放入數(shù)組。
public GameObject body;//身體預設(shè)體 List<GameObject> snakeBody = new List<GameObject>(); void Awake() { for (int i = 0; i < 3; ++i) { GameObject newbodynext=Instantiate (body, transform.position-(i+1)*new Vector3(0,0.5f,0), Quaternion.identity)as GameObject; snakeBody.Add (newbodynext); } }
實現(xiàn)跟隨
void Update () { if (now!=up&&now!=down&&Input.GetKey (KeyCode.W)) { now = up; } if (now!=up&&now!=down&&Input.GetKey (KeyCode.S)) { now = down; } if (now!=left&&now!=right&&Input.GetKey (KeyCode.A)) { now=left; } if (now!=left&&now!=right&&Input.GetKey (KeyCode.D)) { now = right; } timer += Time.deltaTime; if (timer > timerGap) { Vector3 tmpPosition = transform.position;//記錄頭部變化前的位置 List<Vector3> tmpList = new List<Vector3> ();//記錄身體變化前的位置 for (int i = 0; i < snakeBody.Count; ++i) { tmpList.Add (snakeBody [i].transform.position); } timer = 0; transform.position = 0.5f * now + transform.position; snakeBody [0].transform.position = tmpPosition;//將0移到頭部之前的位置 //依次前移身體的位置 for (int i = 1; i < snakeBody.Count; ++i) { snakeBody [i].transform.position = tmpList [i - 1]; } } }
初始蛇創(chuàng)建好后,就開始添加食物,和增長蛇的身體。還有檢測游戲失敗,即撞到身體或者邊界,采用事件觸發(fā)檢測完成。
創(chuàng)建食物
public GameObject foodPrefab;//食物預設(shè)體 void Start () { now = up; createFood (); } void createFood() { float x = Random.Range(-6.5f, 6.5f); float y = Random.Range(-4.5f, 4.5f); Instantiate(foodPrefab,new Vector3(x,y,0f),Quaternion.identity); }
觸發(fā)檢測
void OnTriggerEnter(Collider other) { //這個other就是被碰撞體 if (other.gameObject.tag.Equals("Food")) { Destroy(other.gameObject); GameObject newbodynext = Instantiate (body, snakeBody[snakeBody.Count-1].transform.position, Quaternion.identity)as GameObject; snakeBody.Add (newbodynext);//增加蛇的身體 createFood(); } else if(other.gameObject.tag.Equals("Body")) { SceneManager.LoadScene("Snake", LoadSceneMode.Single);//重新開始 } } void OnTriggerExit(Collider other) { if (other.gameObject.tag.Equals("Boundary")) SceneManager.LoadScene("Snake", LoadSceneMode.Single); }
完整代碼
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class HeadMove : MonoBehaviour { public GameObject body; public GameObject foodPrefab; Vector3 up=new Vector3(0,1,0); Vector3 down=new Vector3(0,-1,0); Vector3 left=new Vector3(-1,0,0); Vector3 right=new Vector3(1,0,0); Vector3 now; float timer=0f; float timerGap=0.1f; List<GameObject> snakeBody = new List<GameObject>(); // Use this for initialization void Awake() { for (int i = 0; i < 3; ++i) { GameObject newbodynext=Instantiate (body, transform.position-(i+1)*new Vector3(0,0.5f,0),Quaternion.identity)as GameObject; snakeBody.Add (newbodynext); } } void Start () { now = up; createFood (); } void createFood() { float x = Random.Range(-6.5f, 6.5f); float y = Random.Range(-4.5f, 4.5f); Instantiate(foodPrefab,new Vector3(x,y,0f),Quaternion.identity); } // Update is called once per frame void Update () { if (now!=up&&now!=down&&Input.GetKey (KeyCode.W)) { now = up; } if (now!=up&&now!=down&&Input.GetKey (KeyCode.S)) { now = down; } if (now!=left&&now!=right&&Input.GetKey (KeyCode.A)) { now=left; } if (now!=left&&now!=right&&Input.GetKey (KeyCode.D)) { now = right; } timer += Time.deltaTime; if (timer > timerGap) { Vector3 tmpPosition = transform.position; List<Vector3> tmpList = new List<Vector3> (); for (int i = 0; i < snakeBody.Count; ++i) { tmpList.Add (snakeBody [i].transform.position); } timer = 0; transform.position = 0.5f * now + transform.position; snakeBody [0].transform.position = tmpPosition; for (int i = 1; i < snakeBody.Count; ++i) { snakeBody [i].transform.position = tmpList [i - 1]; } } } void OnTriggerEnter(Collider other) { //這個other就是被碰撞體 if (other.gameObject.tag.Equals("Food")) { Destroy(other.gameObject); GameObject newbodynext = Instantiate (body,snakeBody[snakeBody.Count-1].transform.position,Quaternion.identity)as GameObject; snakeBody.Add (newbodynext); createFood(); } //由于身體和頭部一開始就接觸,所以將身體的碰撞半徑減小到0.4 else if(other.gameObject.tag.Equals("Body")) { SceneManager.LoadScene("Snake", LoadSceneMode.Single); } } void OnTriggerExit(Collider other) { if (other.gameObject.tag.Equals("Boundary")) SceneManager.LoadScene("Snake", LoadSceneMode.Single); } }
將該腳本掛載在頭部對象上然后添加身體和食物預設(shè)體,再添加邊界即可。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# 根據(jù)NPOI 讀取一個excel 文件的多個Sheet
這篇文章主要介紹了c# 根據(jù)NPOI 讀取一個excel 文件的多個Sheet,幫助大家更好的利用c#處理excel表格,感興趣的朋友可以了解下2020-12-12c#操作sqlserver數(shù)據(jù)庫的簡單示例
這篇文章主要介紹了c#操作sqlserver數(shù)據(jù)庫的簡單示例,需要的朋友可以參考下2014-04-04C#操作SQLite實現(xiàn)數(shù)據(jù)的增刪改查
SQLite是一個輕量級、跨平臺的關(guān)系型數(shù)據(jù)庫,在小型項目中,方便,易用,同時支持多種開發(fā)語言。本文將用C#語言對SQLite 的一個封裝,實現(xiàn)數(shù)據(jù)的增刪改查。需要的可以參考一下2022-01-01C#連接Mysql數(shù)據(jù)庫詳細教程(內(nèi)附Mysql及Navicat)
這篇文章主要給大家介紹了C#連接Mysql數(shù)據(jù)庫詳細教程(內(nèi)附Mysql及Navicat),文中通過代碼示例和圖文介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2023-10-10C# .net core HttpClientFactory用法及說明
這篇文章主要介紹了C# .net core HttpClientFactory用法及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11