最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

unity實現(xiàn)簡單貪吃蛇游戲

 更新時間:2020年04月17日 08:30:45   作者:EmberWn  
這篇文章主要為大家詳細介紹了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;//身體預設體
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;//食物預設體
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);
  }
}

將該腳本掛載在頭部對象上然后添加身體和食物預設體,再添加邊界即可。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#調(diào)用dos窗口獲取相關信息的方法

    C#調(diào)用dos窗口獲取相關信息的方法

    這篇文章主要介紹了C#調(diào)用dos窗口獲取相關信息的方法,涉及C#調(diào)用dos窗口及進程操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • c# 并行的實現(xiàn)示例

    c# 并行的實現(xiàn)示例

    本文主要介紹了c# 并行的實現(xiàn)示例,我們使用?Parallel.ForEach?方法并結合?File.ReadAllLines?來提高讀取速度,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • Unity UI實現(xiàn)循環(huán)播放序列圖

    Unity UI實現(xiàn)循環(huán)播放序列圖

    這篇文章主要為大家詳細介紹了Unity UI實現(xiàn)循環(huán)播放序列圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C#使用TcpListener及TcpClient開發(fā)一個簡單的Chat工具實例

    C#使用TcpListener及TcpClient開發(fā)一個簡單的Chat工具實例

    下面小編就為大家分享一篇C#使用TcpListener及TcpClient開發(fā)一個簡單的Chat工具實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 深入理解C#中常見的委托

    深入理解C#中常見的委托

    這篇文章主要介紹了C# 委托(Delegate)的相關資料,文中講解非常詳細,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下,希望能夠幫助到你
    2021-07-07
  • 時間戳與時間相互轉(zhuǎn)換(php .net精確到毫秒)

    時間戳與時間相互轉(zhuǎn)換(php .net精確到毫秒)

    本文給大家分享的時間戳與時間相互轉(zhuǎn)換(php .net精確到毫秒) ,感興趣的朋友一起學習吧
    2015-09-09
  • C#在驗證文件共享模式下實現(xiàn)多線程文件寫入

    C#在驗證文件共享模式下實現(xiàn)多線程文件寫入

    這篇文章主要為大家詳細介紹了C#在驗證文件共享模式下實現(xiàn)多線程文件寫入的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下
    2024-01-01
  • C#匿名方法與Delegate類型轉(zhuǎn)換錯誤分析

    C#匿名方法與Delegate類型轉(zhuǎn)換錯誤分析

    這篇文章主要介紹了C#匿名方法與Delegate類型轉(zhuǎn)換錯誤,較為詳細的分析了C#匿名方法的用法及Delegate類型轉(zhuǎn)換錯誤問題解決方法,具有一定的實用價值,需要的朋友可以參考下
    2014-11-11
  • C#中的IEnumerable簡介及簡單實現(xiàn)實例

    C#中的IEnumerable簡介及簡單實現(xiàn)實例

    這篇文章主要介紹了C#中的IEnumerable簡介及簡單實現(xiàn)實例,本文講解了IEnumerable一些知識并給出了一個簡單的實現(xiàn),需要的朋友可以參考下
    2015-03-03
  • C#檢查遠程或本地磁盤使用率

    C#檢查遠程或本地磁盤使用率

    要檢查磁盤的使用情況確定程序放哪個服務器和清理垃圾,所以寫個小程序幫忙檢查。本文給大家介紹C#檢查遠程或本地磁盤使用率的相關知識,感興趣的朋友一起學習吧
    2016-04-04

最新評論

定南县| 临颍县| 佛冈县| 开封县| 鸡东县| 潞城市| 定襄县| 山西省| 金寨县| 武宁县| 安图县| 伊川县| 万盛区| 紫金县| 营山县| 景宁| 龙南县| 连江县| 通山县| 孝感市| 甘泉县| 西贡区| 宜宾市| 偏关县| 会东县| 武穴市| 泸定县| 大英县| 闸北区| 米林县| 夏河县| 清河县| 香港| 山阴县| 曲麻莱县| 十堰市| 军事| 搜索| 井陉县| 马边| 灵石县|