unity实现简单贪吃蛇游戏
本⽂实例为⼤家分享了unity实现贪吃蛇游戏的具体代码,供⼤家参考,具体内容如下
⾸先创建⼀个头部,编写脚本利⽤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;
}
}
然后就是创建初始⾝体,实现⾝体跟随头部。采⽤的⽅法是将⾝体放进⼀个数组,然后下标0的⾝体移动到头部之前的位置,然后下标 i 的⾝体移动到 i-1 的position。
创建初始⾝体,并放⼊数组。
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);
}
}
实现跟随
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];
}
}
}
初始蛇创建好后,就开始添加⾷物,和增长蛇的⾝体。还有检测游戏失败,即撞到⾝体或者边界,采⽤事件触发检测完成。创建⾷物
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);
}
触发检测
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 ();
}
float upvoid 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);
}
}
将该脚本挂载在头部对象上然后添加⾝体和⾷物预设体,再添加边界即可。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论