这个贪吃蛇借鉴了网上的一个算法,其他代码部分就是自己写的了。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
public class SnakeMove : MonoBehaviour
{
public GameObject scene;//绑定场景
public GameObject SSFood;//绑定食物
public GameObject gameObjecgtBody;//绑定身体
public float velocityTime = 0.1f;//执行延时时间
public Text Score;//UI分数
Vector3 direction = Vector3.up;//初始移动方向
List<Transform> Body = new List<Transform>();//存储身体的列表
private bool flag = false; //判断是否撞上去
private int score = 0;//计算分数
void Start()
{
//延时执行 并且每个velocityTime在执行一次
InvokeRepeating("Move", 5f, velocityTime);//检查是否吃了食物
InvokeRepeating("ShowFood", 1, 4);//生成食物
}
void Update()
{
Score.text = score.ToString();
//按下WASD或者光标键移动 且不能向反方向移动
if ((Input.GetKey(KeyCode.W) || Input.GetKey("up")) && direction != Vector3.down)
{
direction = Vector3.up;
}
if ((Input.GetKey(KeyCode.S) || Input.GetKey("down")) && direction != Vector3.up)
{
direction = Vector3.down;
}
if ((Input.GetKey(KeyCode.D) || Input.GetKey("left")) && direction != Vector3.right)
{
direction = Vector3.left;
}
if ((Input.GetKey(KeyCode.A) || Input.GetKey("right")) && direction != Vector3.left)
{
direction = Vector3.right;
}
//头部不能超出棋盘范围
if (transform.position.z > 270 || transform.position.z < -17 || transform.position.y > 1013 || transform.position.y < 706)
{
Destroy(scene);
}
}
void Move()
{
//为新生成的身体提供位置
Vector3 VPosition = transform.position;
transform.Translate(direction * Time.deltaTime * 2000);
print(VPosition);
if (flag)//条件成立生成身体
{
GameObject bodyPrefab = (GameObject)Instantiate(gameObjecgtBody, VPosition, gameObjecgtBody.transform.rotation);
Body.Insert(0, bodyPrefab.transform);
flag = false;
score += 100;
}
else if (Body.Count > 0)//不生成身体时 将列表中最后一个身体挪到最前面去
{
Body.Last().position = VPosition;
Body.Insert(0, Body.Last());
Body.RemoveAt(Body.Count - 1);
}
}
void ShowFood()
{
//限制食物生成的范围
int z = Random.Range(290, -17);
int y = Random.Range(1013, 706);
Instantiate(SSFood, new Vector3(-408f, y, z), SSFood.transform.rotation);
}
void OnTriggerEnter(Collider other)
{
//吃到食物 则会修改flag值
if (other.gameObject.CompareTag("Food"))
{
Debug.Log("撞上了!");
Destroy(other.gameObject);
flag = true;
}
//碰到身体的话消除身体
//if (other.gameObject.tag == "Body")
//{
// Destroy(other.gameObject);
//}
}
}
算法很简单,预测即将移动的位置,将蛇尾最后一个组成部分移到最前面去,就完成了,代码都有注释就不啰嗦了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnakeAnimation : MonoBehaviour
{
public GameObject Plane;//绑定棋盘
public GameObject Snake;//绑定贪吃蛇
private float X;//棋盘的X轴倍数
private float Y;//棋盘的Y轴倍数
// Update is called once per frame
void Update()
{
//每帧刷新X Y的值
X = Plane.transform.localScale.x;
Y = Plane.transform.localScale.y;
if (Plane.transform.position.y < 800)
{
transform.Translate(Vector3.up);//上升阶段
}
else
{
if (X <= 0.15)
{
Plane.transform.localScale += new Vector3(0.001f, 0f, 0f);//X轴放大阶段
}
else if (Y <= 0.15)
{
Plane.transform.localScale += new Vector3(0f, 0.001f, 0f);//Y轴放大阶段
}
else
{
Snake.SetActive(true);//召唤贪吃蛇
Plane.SetActive(false);
}
}
}
}
这是一个游戏弹出的小动画,不是必须的。