赞
踩
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Linq; public class SnakeHead : MonoBehaviour { public List<Transform> bodyList = new List<Transform>(); public float velocity = 0.35f; public int step; // 每次移动多少 private int x; //蛇下次将要去的增量 private int y; //蛇下次将要去的增量 private Vector3 headPos; //记录蛇头的位置 private Transform canvas; public GameObject bodyPrefab; public Sprite[] bodySprites = new Sprite[2]; private bool isDie = false; // 默认为 未死亡 public GameObject dieEffect; // 死亡特效 void Awake() { canvas = GameObject.Find("Canvas").transform; } void Start() { //InvokeRepeating(); 此方法作用重复调用 // 1. 方法名 2.执行完此方法后多久开始调用(0秒为立马调用) // 3.每隔多少时间调用一次 InvokeRepeating("Move",0, velocity); x = 0; y = step; } void Update() { // 加速 if (Input.GetKeyDown(KeyCode.Space) && MainUIController.Instance.isPause ==false && isDie==false) { // CancelInvoke(); 作用: 取消当前的 InvokeRepeating(); 方法所起的作用 CancelInvoke(); InvokeRepeating("Move", 0, velocity -0.3f); } if (Input.GetKeyUp(KeyCode.Space) && MainUIController.Instance.isPause == false && isDie == false) { CancelInvoke(); InvokeRepeating("Move", 0, velocity); } if (Input.GetKey(KeyCode.UpArrow) && y!= -step && MainUIController.Instance.isPause == false && isDie == false || Input.GetKey(KeyCode.W)&& y !=-step && MainUIController.Instance.isPause == false && isDie == false) { gameObject.transform.localRotation = Quaternion.Euler(0,0,0); x = 0; y = step; } if (Input.GetKey(KeyCode.DownArrow) && y != step && MainUIController.Instance.isPause == false && isDie == false || Input.GetKey(KeyCode.S) && y !=step && MainUIController.Instance.isPause == false && isDie == false) { gameObject.transform.localRotation = Quaternion.Euler(0, 0, -180); x = 0; y = -step; } if (Input.GetKey(KeyCode.LeftArrow) && x != step && MainUIController.Instance.isPause == false && isDie == false || Input.GetKey(KeyCode.A)&& x!= step && MainUIController.Instance.isPause == false && isDie == false) { gameObject.transform.localRotation = Quaternion.Euler(0, 0, 90); x = -step; y = 0; } if (Input.GetKey(KeyCode.RightArrow) && x != -step && MainUIController.Instance.isPause == false && isDie == false || Input.GetKey(KeyCode.D) && x != -step && MainUIController.Instance.isPause == false && isDie == false) { gameObject.transform.localRotation = Quaternion.Euler(0, 0, -90); x = step; y = 0; } } void Move() { headPos = gameObject.transform.localPosition; // 保存下来蛇头移动前的位置 gameObject.transform.localPosition = new Vector3(headPos.x + x, headPos.y + y,headPos.z);//蛇头向期望位置移动 if (bodyList.Count>0) { // 由于我们是双色蛇身 , 此方法弃用 //bodyList.Last().localPosition = headPos; //将蛇尾移动到蛇头移动前的位置 //bodyList.Insert(0, bodyList.Last()); //将蛇尾在List中的位置更新到最前 //bodyList.RemoveAt(bodyList.Count - 1); //移除List最末尾的蛇尾引用 // 由于我们是双色蛇身,使用此方法达到显示目的 for ( int i = bodyList.Count - 2; i >= 0;i--) //从后往前开始移动蛇身 { bodyList[i + 1].localPosition = bodyList[i].localPosition; //每一个蛇身都移动到它前一个节点的位置, } bodyList[0].localPosition = headPos; //第一个蛇身移动到蛇头移动前的位置 } } void Grow() { int index = (bodyList.Count % 2 == 0) ? 0 : 1; GameObject body = Instantiate(bodyPrefab,new Vector3(2000,2000,0),Quaternion.identity); body.GetComponent<Image>().sprite = bodySprites[index]; body.transform.SetParent(canvas,false); bodyList.Add(body.transform); } /// <summary> /// 死亡方法 /// </summary> void Die() { CancelInvoke(); // 玩家禁止移动 isDie = true; Instantiate(dieEffect); StartCoroutine(GameOver(2f)); } IEnumerator GameOver(float t) { yield return new WaitForSeconds(t); UnityEngine.SceneManagement.SceneManager.LoadScene(1); } private void OnTriggerEnter2D(Collider2D collision) { // 也可以写成 collision.gameObject.CompareTag("Food") if (collision.tag == "Food") { Destroy(collision.gameObject); MainUIController.Instance.UpdateUI(); Grow(); // 生成 问号道具的概率为 20%, 当概率达到时,生成问号和食物;未达到时,只生成食物 FoodMake.Instance.MakeFood((Random.Range(0, 100) < 20)?true:false); //if (Random.Range(0,100)<20) //{ // FoodMake.Instance.MakeFood(true); //} //else //{ // FoodMake.Instance.MakeFood(false); //} } else if (collision.tag == "Reward") { Destroy(collision.gameObject); MainUIController.Instance.UpdateUI(Random.Range(5,15)*10); Grow(); } else if (collision.gameObject.CompareTag("Body")) { Die(); } else { switch (collision.gameObject.name) { case "Up": transform.localPosition = new Vector3(transform.localPosition.x,-transform.localPosition.y + 30,transform.localPosition.z); break; case "Down": transform.localPosition = new Vector3(transform.localPosition.x, -transform.localPosition.y - 30, transform.localPosition.z); break; case "Left": transform.localPosition = new Vector3(-transform.localPosition.x + 180, transform.localPosition.y , transform.localPosition.z); break; case "Right": transform.localPosition = new Vector3(-transform.localPosition.x +240, transform.localPosition.y , transform.localPosition.z); break; } Debug.Log("Die"); } } }
一
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class MainUIController : MonoBehaviour { // 单例模式 private static MainUIController _instance; public static MainUIController Instance { get { return _instance; } } public void Awake() { _instance = this; } void Update() { switch (score/100) { case 0: case 1: ColorUtility.TryParseHtmlString("#CCEEFFFF",out tempColor); BgImage.color = tempColor; msgText.text = "阶段"+1; break; case 2: case 3: case 4: case 5: ColorUtility.TryParseHtmlString("#CCFFDBFF", out tempColor); BgImage.color = tempColor; msgText.text = "阶段" + 2; break; case 6: case 7: ColorUtility.TryParseHtmlString("#EBFFCCFF", out tempColor); BgImage.color = tempColor; msgText.text = "阶段" + 3; break; case 8: case 9: ColorUtility.TryParseHtmlString("#FFF3CCFF", out tempColor); BgImage.color = tempColor; msgText.text = "阶段" + 4; break; default: ColorUtility.TryParseHtmlString("#FFDACCFF", out tempColor); BgImage.color = tempColor; msgText.text = "无尽阶段"; break; } } public int score = 0; public int length = 0; public Text scoreText; public Text lengthText; public Image pauseImage; public Sprite[] pauseSprite; public Text msgText; // 阶段显示 public Image BgImage; //阶段背景 private Color tempColor; //阶段背景的颜色 public bool isPause ; //游戏暂停 public void UpdateUI(int s = 5, int l=1) { score += s; length += l; scoreText.text = "得分:\n" + score; lengthText.text = "长度:\n" +length; } public void Pause() { isPause = !isPause; if (isPause) { Time.timeScale = 0; pauseImage.sprite = pauseSprite[1]; } else { Time.timeScale = 1; pauseImage.sprite = pauseSprite[0]; } } public void Home() { UnityEngine.SceneManagement.SceneManager.LoadScene(0); } }
二
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class FoodMake : MonoBehaviour { private static FoodMake _instance; public static FoodMake Instance { get { return _instance; } } public int xlimit = 21; public int ylimit = 11; public int xoffset = 7; public GameObject foodPrefab; private Transform foodHolder; public Sprite[] foodSprites; // 存放食物的数组 public GameObject rewardPrefab; void Awake() { _instance = this; } void Start() { foodHolder = GameObject.Find("FoodHolder").transform; MakeFood(false); } public void MakeFood(bool isReward) { int index = Random.Range(0, foodSprites.Length); GameObject food = Instantiate(foodPrefab); food.GetComponent<Image>().sprite = foodSprites[index]; // 是否转换为世界坐标 false food.transform.SetParent(foodHolder,false); int x = Random.Range(-xlimit+xoffset,xlimit); int y = Random.Range(-ylimit , ylimit); food.transform.localPosition = new Vector3(x*30,y*30,0); if (isReward) { GameObject reward = Instantiate(rewardPrefab); // 是否转换为世界坐标 false reward.transform.SetParent(foodHolder, false); x = Random.Range(-xlimit + xoffset, xlimit); y = Random.Range(-ylimit, ylimit); reward.transform.localPosition = new Vector3(x * 30, y * 30, 0); } } }
三
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class StartUIController : MonoBehaviour { public Text lastText; // 上次得分 public Text bestText; // 最好得分 public Toggle blue; public Toggle yellow; public Toggle border; public Toggle noBorder; void Awake() { lastText.text = "上次:长度" + PlayerPrefs.GetInt("Last", 0)+",分数"+PlayerPrefs.GetInt("Last", 0); bestText.text = "最好:长度" + PlayerPrefs.GetInt("Best", 0)+",分数"+PlayerPrefs.GetInt("Best", 0); } void Start() { if (PlayerPrefs.GetString("sh","sh01")=="sh01") { blue.isOn = true; PlayerPrefs.SetString("sh", "sh01"); PlayerPrefs.SetString("sb01", "sb0101"); PlayerPrefs.SetString("sb02", "sb0102"); } else { yellow.isOn = true; PlayerPrefs.SetString("sh", "sh02"); PlayerPrefs.SetString("sb01", "sb0201"); PlayerPrefs.SetString("sb02", "sb0202"); } if (PlayerPrefs.GetInt("border",1) == 1) { border.isOn = true; PlayerPrefs.SetInt("border", 1); } else { noBorder.isOn = true; PlayerPrefs.SetInt("border", 0); } } public void BlueSelected(bool isOn) { if (isOn) { PlayerPrefs.SetString("sh","sh01"); PlayerPrefs.SetString("sb01","sb0101"); PlayerPrefs.SetString("sb02","sb0102"); } } public void YellowSelected(bool isOn) { if (isOn) { PlayerPrefs.SetString("sh", "sh02"); PlayerPrefs.SetString("sb01", "sb0201"); PlayerPrefs.SetString("sb02", "sb0202"); } } public void BorderSelected(bool isOn) { if (isOn) { PlayerPrefs.SetInt("border",1); } } public void NoborderSelected(bool isOn) { if (isOn) { PlayerPrefs.SetInt("border", 0); } } public void StartGame() { UnityEngine.SceneManagement.SceneManager.LoadScene(1); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。