主要視圖
Materials
Prefabs
Scenes
Scripts
Texture
關鍵代碼
FoodSnake
public class FoodSnake : MonoBehaviour {
//定義預置體,方便實例化
public GameObject foodSanke;
//公開食物生成位置
public int xLimit = 30;
public int yLimit = 22;
void Start () {
//調(diào)用方法的函數(shù)
InvokeRepeating("CreateFood",1,3);
}
void Update () {
}
void CreateFood() {
//實例化食物 游戲對象的自身方法
// GameObject.Instantiate(foodSanke,new Vector2(1,1),Quaternion.identity);
//讓位置隨機 x=61 y=45;
//GameObject.Instantiate(foodSanke,new Vector2(Random.Range(-30,30),Random.Range(-22,22)),Quaternion.identity);
//方便修改食物生成范圍缠借,將位置公開
GameObject.Instantiate(foodSanke,new Vector2(Random.Range(-xLimit,xLimit),Random.Range(-yLimit,yLimit)),Quaternion.identity);
}
}
MoveSnake
using UnityEngine;
using System.Collections;
using System.Collections.Generic;//引用 泛型集合 命名空間
using System.Linq;//引用铅匹,命名空間
using UnityEngine.SceneManagement;//轉換場景需要的引用
public class MoveSnake : MonoBehaviour {
//定義一個transform類型的變量
private Transform m_Transform;
//定義一個開始的方向 默認向上移動
Vector2 direction = Vector2.up;
//定義一個公開的速度 為之后游戲速度改變做準備
public float velocitytime = 0.5f;
//定義一個公開的body變量高职,用來存放cube body預置體
public GameObject body;
//定義一個開關,用來檢測食物
private bool Open = false;
//將蛇身存入一個集合里去 定義集合 的變量
List<Transform> snakeBody = new List<Transform>();
void Start () {
//賦值
m_Transform=gameObject.GetComponent<Transform>();
//重復調(diào)用方法的函數(shù)
//InvokeRepeating("Move",0.5f,0.5f);//參數(shù)1:移動的方法 參數(shù)2:第一次調(diào)用的時間 參數(shù)3:以后每個0.5f秒調(diào)用一次导饲;
InvokeRepeating("Move", 0.5f, velocitytime);
}
void Update () {
//控制方向
if (Input.GetKey(KeyCode.A))
{
direction = Vector2.left;
}
if (Input.GetKey(KeyCode.D))
{
direction = Vector2.right;
}
if (Input.GetKey(KeyCode.W))
{
direction = Vector2.up;
}
if (Input.GetKey(KeyCode.S))
{
direction = Vector2.down;
}
}
/// <summary>
/// 移動的方法
/// </summary>
void Move() {
//在移動過程中朵锣,判斷開關是否打開谬盐,打開就創(chuàng)建蛇身;(實例化)位置為本次蛇頭的位置
Vector3 VPosition = m_Transform.position; //先執(zhí)行這一句诚些,保存起來位置设褐,再執(zhí)行下一句,讓它移動
//默認向上移動
m_Transform.Translate(direction);
if (Open)
{
//實例化 蛇身
// GameObject.Instantiate(body,VPosition,Quaternion.identity);
//把實例化的蛇身存起來泣刹;
GameObject bodyYuzhi = GameObject.Instantiate(body,VPosition,Quaternion.identity) as GameObject;
//集合插入方法 吃掉食物后在定義的蛇身集合0 號位前面插入實例化后的蛇身
snakeBody.Insert(0,bodyYuzhi.transform);
//檢查是否插入成功
// Debug.Log(snakeBody.Count);
//實力一次之后把開關 關掉
// Open = false;
Open = !Open;
}else if (snakeBody.Count > 0)//判斷集合里有元素(蛇身)的話 將蛇身跟隨蛇頭移動的方法
{
//蛇身的位置等于蛇頭的位置
snakeBody.Last().position = VPosition;
// Debug.Log(snakeBody.Last().position);
//把最后一個位置從零號位插入助析;
snakeBody.Insert(0, snakeBody.Last());
//把最后一個元素刪除掉;
snakeBody.RemoveAt(snakeBody.Count - 1);//參數(shù):表示固定的下標移除元素椅您;
}
}
//沒有剛體 又想觸發(fā)事件 有兩個碰撞器前提下外冀,將蛇改成觸發(fā)器,添加剛體掀泳,不使用重力
//食物是碰撞體就行 ,并且將碰撞體的size改成0.5雪隧,否則側面也會吃掉食物的;
//蛇在移動過程中员舵,檢測下觸發(fā)了誰脑沿,就讓說消失 把食物的便簽改成Food;
/// <summary>
/// 檢測觸發(fā)方法
/// </summary>
/// <param name="coll">觸發(fā)的對象</param>
void OnTriggerEnter(Collider coll) {
if (coll.gameObject.tag == "Food")
{
//觸發(fā)食物马僻,將開關打開庄拇;
// Open = true;
Open = !Open;
//銷毀食物
Destroy(coll.gameObject);
}
else {
//如果不是觸發(fā)食物韭邓,觸發(fā)其他物體措近,游戲結束;
//切換場景
// SceneManager.LoadScene(0);//場景管理女淑,加載到另一場景 場景就是設置的第一個 在邊框加上4個碰撞器 觸發(fā)其他即游戲重新開始瞭郑;
//切換第二場景
SceneManager.LoadScene(0);
}
}
}
StartUI
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;//場景管理命名空間
public class StartUI : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
//點擊回到第一個場景;
SceneManager.LoadScene(1);
}
}
}