贪吃蛇移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class snak : MonoBehaviour
{
//move函数
int step;//每次移动的距离
int x, y;//蛇横纵需要移动的距离
Vector3 HeadPosition;//储存蛇头坐标
List bodylist = new List();//定义一个链表储存蛇身
public static float speed;

//grow函数
public Transform bodyPrefab;//身体的预制体
public Sprite[] bodySprite = new Sprite[2];//蛇身体的图片
Transform canvas;//获取画布以便设置父对象

//death函数
bool isDie;//用于判断蛇是否死亡

private void Start()
{
    speed = 0.5f;
    step = 20;
    InvokeRepeating("Move", 0, 0.3f);
    x = step;
    canvas = GameObject.Find("Canvas").transform;

    isDie = false;
}
private void Update()
{
    if (Input.GetKey(KeyCode.W) && y != -step && !isDie)//蛇的移动不能反向,所以需要y != -step的判断
    {
        transform.rotation = Quaternion.Euler(0, 0, 0);//按w键蛇头应该要旋转的角度
        y = step;
        x = 0;
    }
    else if (Input.GetKey(KeyCode.S) && y != step && !isDie)
    {

        transform.rotation = Quaternion.Euler(0, 0, 180);//按s键蛇头应该要旋转的角度
        y = -step;
        x = 0;
    }
    else if (Input.GetKey(KeyCode.A) && x != step && !isDie)
    {
        transform.rotation = Quaternion.Euler(0, 0, -90);//按a键蛇头应该要旋转的角度
        y = 0;
        x = -step;
    }
    else if (Input.GetKey(KeyCode.D) && x != -step && !isDie)
    {
        transform.rotation = Quaternion.Euler(0, 0, 90);//按d键蛇头应该要旋转的角度
        y = 0;
        x = step;
    }


    if (Input.GetKeyDown(KeyCode.Space) && !isDie)
    {
        CancelInvoke();
        InvokeRepeating("Move", 0, speed - 0.4f);
    }
    if (Input.GetKeyUp(KeyCode.Space) && !isDie)
    {
        CancelInvoke();
        InvokeRepeating("Move", 0, speed);
    }
}
/// <summary>
/// 生长
/// </summary>
/// <param name="collision"></param>
void Grow()
{
    Transform newbody = Instantiate(bodyPrefab);
    newbody.transform.SetParent(canvas, false);
    int index = bodylist.Count % 2 == 0 ? 1 : 0;
    newbody.GetComponent<Image>().sprite = bodySprite[index];//当蛇身为奇数和偶数这两种不同情况时需要设置不同的照片
    newbody.localPosition = new Vector3(2000, 2000, 0);//先把新生成的蛇身块的坐标设置为屏幕外,Move移动时自动添加到相应位置
    bodylist.Add(newbody);

}

void Move()
{
    Debug.Log(transform.position);
    HeadPosition = transform.localPosition;
    transform.localPosition = new Vector3(HeadPosition.x + x, HeadPosition.y + y, HeadPosition.z);

    if (bodylist.Count > 0)
    {
        for (int i = bodylist.Count - 2; i >= 0; i--)//从后往前,依次把上一个蛇身的坐标赋值下一个
        {
            bodylist[i + 1].localPosition = bodylist[i].localPosition;
        }
        bodylist[0].localPosition = HeadPosition;//把还未移动前的蛇头坐标赋值给第一个蛇身

    }

}

public void death()
{
    isDie = true;
    CancelInvoke();//取消对move函数的调用
}
public void OnTriggerEnter2D(Collider2D collision)
{

    if (collision.tag == "food")
    {
        Destroy(collision.gameObject);
        Grow();

        foodmake.Instant.CreatFood();

    }
    else if (collision.tag == "reward")
    {
        Destroy(collision.gameObject);
        Grow();
    }
    else if (collision.tag == "body")
    {
        death();
    }
    else
    {
        death();

    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值