贪吃蛇c语言彩色如何判定死亡,求教!蛇头碰到自己的身体显示游戏死亡要怎么写?...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public enum Direction{

Right,

Left,

Front,

Back

};

public class Snake : MonoBehaviour {

private LinkedList snakeList = null;

public GameObject foodGo;

float timer=0;

float tempX=1;

float tempZ=0;

Direction dir=Direction.Right;

bool isFood=false;

private Transform foodtrans;

// Use this for initialization

void Start () {

CreatSnakeList ();

}

// Update is called once per frame

void Update () {

SnakeMove ();

CreatFood ();

SnakeEatFood ();

}

//生成食物

void CreatFood(){

if (isFood == true) {

return;

} else {

float foodx = Random.Range (-15f,15f);

float foodz = Random.Range (-15f,15f);

float foody = 1f;

GameObject food= Instantiate (foodGo,new Vector3(foodx,foody,foodz),foodGo.transform.rotation);

foodtrans = food.transform;

isFood=true;

}

}

//吃掉食物

void SnakeEatFood()

{

LinkedListNode head = snakeList.First;

float distance = Vector3.Distance (head.Value.position,foodtrans.position);

print (distance);

if (distance < 1) {

Destroy (foodtrans.gameObject);

snakeList.AddLast(foodtrans);

foodtrans.SetParent(transform);

isFood = false;

}

}

//移动

//控制头的移动,来控制尾巴的位置

void SnakeMove()

{

if (Input.GetKeyDown (KeyCode.A)&&dir!=Direction.Right) {

tempX = 1;

tempZ = 0;

dir = Direction.Left;

} else if (Input.GetKeyDown (KeyCode.D)&&dir!=Direction.Left) {

tempX = -1;

tempZ = 0;

dir = Direction.Right;

}else if (Input.GetKeyDown (KeyCode.W)&&dir!=Direction.Front) {

tempX = 0;

tempZ = -1;

dir = Direction.Back;

}else if (Input.GetKeyDown (KeyCode.S)&&dir!=Direction.Back) {

tempX = 0;

tempZ = 1;

dir = Direction.Front;

}

timer += Time.deltaTime;

if (timer >= 0.5f) {

timer = 0;

LinkedListNode head = snakeList.First;

Vector3 savePos = head.Value.localPosition;

Vector3 tempPos = head.Value.localPosition;

tempPos.x += tempX;

tempPos.z += tempZ;

head.Value.localPosition = tempPos;

while (head.Next != null) {

Vector3 sPos = head.Next.Value.localPosition;

head.Next.Value.localPosition = savePos;

head = head.Next;

savePos = sPos;

}

}

}

//创建蛇的链表

void CreatSnakeList()

{

snakeList = new LinkedList ();

Transform[] trans = this.GetComponentsInChildren();

foreach (Transform t in trans) {

if (t.gameObject.name == "Snake")

continue;

snakeList.AddLast (t);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值