01-处理蛇身的生成
将Sh贪吃蛇头更名为SnakeHead,新建Image更名为SnakeBody
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SnakeHead : MonoBehaviour
{
public List<Transform> bodyList = new List<Transform>();//存储身体的位置
public float velocity=0.35f;//每隔多久要调用,实际就相当于速度
public int step;//小蛇每一步要走的路
private int x;//x和Y都是移动的增量
private int y;
private Vector3 HeadPos;//记录头的位置
private Transform canvas;
public GameObject bodyPrefab;//蛇身预制体
public Sprite[] bodySprites = new Sprite[2];
void Start()
{
InvokeRepeating("Move",0,velocity);
x = 0;//刚开始的时候,贪吃蛇就会往上走
y =step;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))//CancelInvoke()取消当前的Invoke
{
CancelInvoke();
InvokeRepeating("Move", 0, velocity-0.2f);//跑快
}
if (Input.GetKeyUp(KeyCode.Space))
{
CancelInvoke();
InvokeRepeating("Move", 0, velocity);//减速就是回到正常速度
}
if (Input.GetKey(KeyCode.W)&&y!=-step)//y!=-step加这句是蛇头向下的时候不是直接向下的
{
gameObject.transform.localRotation = Quaternion.Euler(0,0,0);//将Float值转换成四元数
x = 0;
y = step;
}
if (Input.GetKey(KeyCode.S)&&y != step)
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, 180);//将Float值转换成四元数
x = 0;
y = -step;
}
if (Input.GetKey(KeyCode.A) && x != step)
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, 90);//将Float值转换成四元数
x = -step;
y = 0;
}
if (Input.GetKey(KeyCode.D) && x != -step)
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, -90);//将Float值转换成四元数
x = step;
y = 0;
}
}
/// <summary>
/// 头部的移动
/// </summary>
void Move()
{
HeadPos = gameObject.transform.localPosition;
gameObject.transform.localPosition = new Vector3(HeadPos.x + x,HeadPos.y+y,HeadPos.z);
}
/// <summary>
/// 生成蛇身
/// </summary>
void Grow()
{
int index=(bodyList.Count%2==0)?0:1;
GameObject body = Instantiate(bodyPrefab);
body.GetComponent<Image>().sprite = bodySprites[index];
body.transform.SetParent(canvas,false);
bodyList.Add(body.transform);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Food"))//也可以写成collision.tag == "Food"
{
Destroy(collision.gameObject);
FoodMaker.Instance.MakeFood();
}
}
}
02-处理蛇身的移动之方法一
用移动方法二,这个不适合双色蛇身
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class SnakeHead : MonoBehaviour
{
public List<Transform> bodyList = new List<Transform>();//存储身体的位置
public float velocity=0.35f;//每隔多久要调用,实际就相当于速度
public int step;//小蛇每一步要走的路
private int x;//x和Y都是移动的增量
private int y;
private Vector3 HeadPos;//记录头的位置
private Transform canvas;
public GameObject bodyPrefab;//蛇身预制体
public Sprite[] bodySprites = new Sprite[2];
private void Awake()
{
canvas = GameObject.Find("Canvas").transform;
}
void Start()
{
InvokeRepeating("Move",0,velocity);
x = 0;//刚开始的时候,贪吃蛇就会往上走
y =step;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))//CancelInvoke()取消当前的Invoke
{
CancelInvoke();
InvokeRepeating("Move", 0, velocity-0.2f);//跑快
}
if (Input.GetKeyUp(KeyCode.Space))
{
CancelInvoke();
InvokeRepeating("Move", 0, velocity);//减速就是回到正常速度
}
if (Input.GetKey(KeyCode.W)&&y!=-step)//y!=-step加这句是蛇头向下的时候不是直接向下的
{
gameObject.transform.localRotation = Quaternion.Euler(0,0,0);//将Float值转换成四元数
x = 0;
y = step;
}