using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
//using UnityEngine.SceneManagement;
public class SnakeMove : MonoBehaviour {
List<Transform> Body = new List<Transform>();
Vector2 direction = Vector2.up;
public float velocityTime = 0.1f;
public GameObject gameObjecgtBody;
private bool flag = false;
// Use this for initialization
void Start ()
{
//
InvokeRepeating("Move", 0.5f, velocityTime);
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.W)||Input.GetKey("up")&&direction!= Vector2.down)
{
direction = Vector2.up;
}
if (Input.GetKey(KeyCode.S) || Input.GetKey("down") && direction != Vector2.up)
{
direction = Vector2.down;
}
if (Input.GetKey(KeyCode.A) || Input.GetKey("left") && direction != Vector2.right)
{
direction = Vector2.left;
}
if (Input.GetKey(KeyCode.D) || Input.GetKey("right") && direction != Vector2.left)
{
direction = Vector2.right;
}
}
void Move()
{
Vector3 VPosition = transform.position;
transform.Translate(direction);
if (flag)
{
GameObject bodyPrefab = (GameObject)Instantiate(gameObjecgtBody, VPosition, Quaternion.identity);
Body.Insert(0, bodyPrefab.transform);
flag = false;
}
else if (Body.Count > 0)
{
Body.Last().position = VPosition;
Body.Insert(0, Body.Last());
Body.RemoveAt(Body.Count - 1);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Food"))
{
Debug.Log("撞上了!");
Destroy(other.gameObject);
flag = true;
}
else
{
//SceneManager.LoadScene(0)
//Application.LoadLevel(1);
}
}
}
Unity 贪吃蛇游戏(一个脚本搞定)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 这两天在网上看到一张让人涨姿势的图片,图片中展示的是贪吃蛇游戏, 估计大部分人都玩过。但如果仅仅是贪吃蛇游戏...