贪吃蛇 学习
1 using UnityEngine; 2 using System.Collections; 3 4 /// <summary> 5 /// Flash 6 /// </summary> 7 public class Flash : MonoBehaviour { 8 9 /// <summary> 10 /// 闪烁间隔 11 /// </summary> 12 public float interval; 13 14 void Start() 15 { 16 InvokeRepeating("FlashLabel", 0, interval); 17 } 18 19 /// <summary> 20 /// Label闪烁 21 /// </summary> 22 void FlashLabel() 23 { 24 if (gameObject.activeSelf) 25 gameObject.SetActive(false); 26 else 27 gameObject.SetActive(true); 28 } 29 30 public void Stop() 31 { 32 gameObject.SetActive(false); 33 CancelInvoke("FlashLabel"); 34 } 35 }
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 4 5 /// <summary> 6 /// 游戏主逻辑 7 /// </summary> 8 public class Game : MonoBehaviour { 9 10 /// <summary> 11 /// 生成食物脚本 12 /// </summary> 13 public SpawnFood sf; 14 /// <summary> 15 /// 蛇 16 /// </summary> 17 public Snake snake; 18 19 /// <summary> 20 /// 连续生成食物开关 21 /// </summary> 22 public Toggle toggle; 23 /// <summary> 24 /// 游戏失败文本 25 /// </summary> 26 public Text textLose; 27 /// <summary> 28 /// Flash 29 /// </summary> 30 public Flash flash; 31 32 void Update () { 33 34 if (Input.GetKeyUp(KeyCode.Space)) 35 { 36 sf.Continuous = toggle.isOn; 37 38 sf.enabled = true; 39 snake.enabled = true; 40 snake.gameObject.SetActive(true); 41 42 textLose.gameObject.SetActive(false); 43 toggle.gameObject.SetActive(false); 44 flash.Stop(); 45 46 //侦听 47 snake.OnLose += OnLose; 48 } 49 50 } 51 52 void OnLose() 53 { 54 textLose.text = "You lose."; 55 textLose.gameObject.SetActive(true); 56 Time.timeScale = 0; 57 } 58 }
1 using UnityEngine; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using UnityEngine.UI; 6 using System; 7 8 /// <summary> 9 /// 蛇 10 /// </summary> 11 public class Snake : MonoBehaviour 12 { 13 /// <summary> 14 /// 默认移动方向,右 15 /// </summary> 16 Vector2 dir = Vector2.right; 17 18 /// <summary> 19 /// 尾巴列表 20 /// </summary> 21 List<Transform> tail = new List<Transform>(); 22 23 /// <summary> 24 /// 是否吃了食物 25 /// </summary> 26 bool ate = false; 27 28 /// <summary> 29 /// 尾巴预设 30 /// </summary> 31 public GameObject tailPrefab; 32 33 /// <summary> 34 /// 游戏失败委托 35 /// </summary> 36 public Action OnLose; 37 38 void Start() 39 { 40 InvokeRepeating("Move", 0.3f, 0.3f); 41 } 42 43 44 void OnTriggerEnter2D(Collider2D coll) 45 { 46 //如果碰到食物 47 if (coll.name.StartsWith("food")) 48 { 49 //修改标志位 50 ate = true; 51 //删除食物 52 Destroy(coll.gameObject); 53 } 54 //碰到别的,游戏失败 55 else 56 { 57 OnLose(); 58 } 59 } 60 61 void Update() 62 { 63 //根据按键修改方向 64 if (Input.GetKey(KeyCode.RightArrow)) 65 dir = Vector2.right; 66 else if (Input.GetKey(KeyCode.DownArrow)) 67 dir = -Vector2.up; 68 else if (Input.GetKey(KeyCode.LeftArrow)) 69 dir = -Vector2.right; 70 else if (Input.GetKey(KeyCode.UpArrow)) 71 dir = Vector2.up; 72 } 73 74 void Move() 75 { 76 //在指定方向上移动 77 Vector2 v = transform.position; 78 transform.Translate(dir); 79 80 //如果吃了食物 81 if (ate) 82 { 83 //生成并插入尾巴 84 GameObject g = (GameObject)Instantiate(tailPrefab,v,Quaternion.identity); 85 tail.Insert(0, g.transform); 86 87 //重置标志位 88 ate = false; 89 } 90 //如果有尾巴 91 else if (tail.Count > 0) 92 { 93 94 tail.Last().position = v; 95 tail.Insert(0, tail.Last()); 96 tail.RemoveAt(tail.Count - 1); 97 } 98 } 99 }
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 4 5 /// <summary> 6 /// 生成食物 7 /// </summary> 8 public class SpawnFood:MonoBehaviour { 9 /// <summary> 10 /// 食物预设 11 /// </summary> 12 public GameObject foodPrefab; 13 14 /// <summary> 15 /// 上边框 16 /// </summary> 17 public Transform borderTop; 18 /// <summary> 19 /// 下边框 20 /// </summary> 21 public Transform borderBottom; 22 /// <summary> 23 /// 左边框 24 /// </summary> 25 public Transform borderLeft; 26 /// <summary> 27 /// 右边框 28 /// </summary> 29 public Transform borderRight; 30 31 /// <summary> 32 /// 是否连续生成食物 33 /// </summary> 34 public bool Continuous = true; 35 36 void Start() { 37 //每4秒生成一个食物 38 InvokeRepeating("Spawn",3,4); 39 } 40 41 /// <summary> 42 /// 生成食物 43 /// </summary> 44 void Spawn() { 45 //连续模式 46 if(Continuous) { 47 48 //限制食物生成的位置 49 int x = (int)Random.Range(borderLeft.position.x,borderRight.position.x); 50 int y = (int)Random.Range(borderBottom.position.y,borderTop.position.y); 51 52 //生成食物 53 Instantiate(foodPrefab,new Vector2(x,y),Quaternion.identity); 54 //非连续模式 55 } else { 56 //没有食物时,才生成下一个食物 57 GameObject food = GameObject.Find("food(Clone)"); 58 if(food == null) { 59 int x = (int)Random.Range(borderLeft.position.x,borderRight.position.x); 60 int y = (int)Random.Range(borderBottom.position.y,borderTop.position.y); 61 Instantiate(foodPrefab,new Vector2(x,y),Quaternion.identity); 62 } 63 } 64 } 65 }
视频:https://pan.baidu.com/s/1sl8kJTj
项目:https://pan.baidu.com/s/1mhPagqO