SIKI学习——贪吃蛇案例07
作者:互联网
01-对于用户设置的储存
补充:在main场景中给Home绑定方法。
在Start中将Go更名为Start
在start中新建空物体ScriptsHolder,并挂载StartUIController脚本,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
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("lastl", 0) + ",分数" + PlayerPrefs.GetInt("lasts", 0);
bestText.text = "最好:长度" + PlayerPrefs.GetInt("bestl", 0) + ",分数" + PlayerPrefs.GetInt("bests", 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);
}
}
02-完成换肤与其他配置读取
新建Resources文件夹,将原本的蛇的图片放置在Resources文件夹下。
将图片置空
在main场景中的main Camera上添加AudioSource
using System.Collections;
using System.Collections.Generic;
//using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class SnakeHead : MonoBehaviour
{
public List<Transform> bodyList = new List<Transform>();//存储身体的位置
public float velocity=0.35f;//每隔多久要调用,实际就相当于速度
public int step;//小蛇每一步要走的路
private int x;//x和Y都是移动的增量
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;
public AudioClip eatClip;
public AudioClip dieClip;
private void Awake()
{
canvas = GameObject.Find("Canvas").transform;
//通过Resources.Load(string path)方法加载资源,path的书写不需要加Resources/以及文件扩展名
//Resources.Load("");
gameObject.GetComponent<Image>().sprite = Resources.Load<Sprite>(PlayerPrefs.GetString("sh", "sh02"));
bodySprites[0] = Resources.Load<Sprite>(PlayerPrefs.GetString("sb01", "sb0201"));
bodySprites[1] = Resources.Load<Sprite>(PlayerPrefs.GetString("sb02", "sb0202"));
}
void Start()
{
InvokeRepeating("Move",0,velocity);
x = 0;//刚开始的时候,贪吃蛇就会往上走
y =step;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)&&MainUI.Instance.isPause==false&&isDie==false)//CancelInvoke()取消当前的Invoke
{
CancelInvoke();
InvokeRepeating("Move", 0, velocity-0.2f);//跑快
}
if (Input.GetKeyUp(KeyCode.Space) && MainUI.Instance.isPause == false && isDie == false)
{
CancelInvoke();
InvokeRepeating("Move", 0, velocity);//减速就是回到正常速度
}
if (Input.GetKey(KeyCode.W)&&y!=-step && MainUI.Instance.isPause == false && isDie == false)//y!=-step加这句是蛇头向下的时候不是直接向下的
{
gameObject.transform.localRotation = Quaternion.Euler(0,0,0);//将Float值转换成四元数
x = 0;
y = step;
}
if (Input.GetKey(KeyCode.S)&&y != step && MainUI.Instance.isPause == false && isDie == false)
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, 180);//将Float值转换成四元数
x = 0;
y = -step;
}
if (Input.GetKey(KeyCode.A) && x != step && MainUI.Instance.isPause == false && isDie == false)
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, 90);//将Float值转换成四元数
x = -step;
y = 0;
}
if (Input.GetKey(KeyCode.D) && x != -step && MainUI.Instance.isPause == false && isDie == false)
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, -90);//将Float值转换成四元数
x = step;
y = 0;
}
}
/// <summary>
/// 头部的移动
/// </summary>
void Move()
{
HeadPos = gameObject.transform.localPosition;//保存下来蛇头移动前的位置
gameObject.transform.localPosition = new Vector3(HeadPos.x + x,HeadPos.y+y,HeadPos.z);//蛇头向期望位置移动
//Grow();//如果放在这里还来不及显示,就被下面的代码排了位置,但是要列出标识位,管一下开关
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--)//是从后往前开始移动的蛇身 i = bodyList.Count-2
{
bodyList[i + 1].localPosition = bodyList[i].localPosition;//每一个蛇身都移动到他前面一个节点的位置
}
bodyList[0].localPosition = HeadPos;//第一个蛇身移动到蛇头移动前的位置
}
}
/// <summary>
/// 生成蛇身
/// </summary>
void Grow()
{
AudioSource.PlayClipAtPoint(eatClip,Vector3.zero);
int index=(bodyList.Count%2==0)?0:1;
GameObject body = Instantiate(bodyPrefab,new Vector3(2000,2000,0),Quaternion.identity);//new Vector3(2000,2000,0)生成的时候放在看不到的位置,这样就不会显示在中间了
body.GetComponent<Image>().sprite = bodySprites[index];
body.transform.SetParent(canvas,false);
bodyList.Add(body.transform);
}
void Die()
{
AudioSource.PlayClipAtPoint(dieClip, Vector3.zero);
CancelInvoke();
isDie = true;
Instantiate(dieEffect);
PlayerPrefs.SetInt("lastl",MainUI.Instance.length);
PlayerPrefs.SetInt("lasts", MainUI.Instance.score);
if (PlayerPrefs.GetInt("bests",0)<MainUI.Instance.score)
{
PlayerPrefs.SetInt("bestl", MainUI.Instance.length);
PlayerPrefs.SetInt("bests", MainUI.Instance.score);
}
StartCoroutine(GameOver(1.5f));
}
IEnumerator GameOver(float t)
{
yield return new WaitForSeconds(t);
UnityEngine.SceneManagement.SceneManager.LoadScene(1);
}
private void OnTriggerEnter2D(Collider2D collision)
{
//食物
if (collision.gameObject.CompareTag("Food"))//也可以写成collision.tag == "Food"
{
Destroy(collision.gameObject);
MainUI.Instance.UpdateUI();
Grow();//吃了食物之后立马调用
FoodMaker.Instance.MakeFood((Random.Range(0, 100) < 20) ? true : false);
}
else if (collision.gameObject.CompareTag("Reward"))
{
Destroy(collision.gameObject);
MainUI.Instance.UpdateUI(Random.Range(5,10)*50);
Grow();
}
else if (collision.gameObject.CompareTag("Body"))
{
Die();
}
else
{
if (MainUI.Instance.hasBorder)
{
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);//因为左右不对称,这样加180才能从右边边缘出来
break;
case "Right":
transform.localPosition = new Vector3(-transform.localPosition.x + 240, transform.localPosition.y, transform.localPosition.z);
break;
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MainUI : MonoBehaviour
{
private static MainUI _instance;
public static MainUI Instance
{
get
{
return _instance;
}
}
public bool hasBorder = true;
public int score = 0;
public int length = 0;
public Text scoreText;
public Text msgText;
public Text lengthText;
public Image bgImage;
private Color tempColor;
public bool isPause = false;//默认为false就是没有暂停
public Image pauseImage;
public Sprite[] pauseSprites;
private void Awake()
{
_instance = this;
}
void Start()
{
if (PlayerPrefs.GetInt("border",1)==0)
{
hasBorder = false;
foreach (Transform t in bgImage.gameObject.transform)
{
t.gameObject.GetComponent<Image>().enabled = false;
}
}
}
void Update()
{
switch (score/100)
{//标签设置之后就不会出现分数达不到但是加一部分之后,就跳过标签的情况
case 0:
case 1:
case 2:
break;
case 3:
case 4:
ColorUtility.TryParseHtmlString("#CCEEFFFF",out tempColor);
bgImage.color = tempColor;
msgText.text = "阶段" + 2;
Debug.Log("阶段2转换");
break;
case 5:
case 6:
ColorUtility.TryParseHtmlString("#CCFFDBFF", out tempColor);
bgImage.color = tempColor;
msgText.text = "阶段" + 3;
break;
case 7:
case 8:
ColorUtility.TryParseHtmlString("#EBFFCCFF", out tempColor);
bgImage.color = tempColor;
msgText.text = "阶段" + 4;
break;
case 9:
case 10:
ColorUtility.TryParseHtmlString("#FFF3CCFF", out tempColor);
bgImage.color = tempColor;
msgText.text = "阶段" + 5;
break;
default:
ColorUtility.TryParseHtmlString("#FFDACCFF", out tempColor);
bgImage.color = tempColor;
msgText.text = "无尽阶段";
break;
}
}
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 = pauseSprites[1];
}
else
{
Time.timeScale = 1;
pauseImage.sprite = pauseSprites[0];
}
}
public void Home()
{
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
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("lastl", 0) + ",分数" + PlayerPrefs.GetInt("lasts", 0);
bestText.text = "最好:长度" + PlayerPrefs.GetInt("bestl", 0) + ",分数" + PlayerPrefs.GetInt("bests", 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);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FoodMaker : MonoBehaviour
{
private static FoodMaker _instance;
public static FoodMaker Instance
{
get
{
return _instance;
}
}
public int xlimit=21;
public int ylimit = 11;
public int xoffset = 7;
public GameObject foodPrefab;
public Sprite[] foodSprites;
private Transform foodHolder;
public GameObject rewardPrefab;
private void Awake()
{
_instance = this;
}
private void Start()
{
foodHolder = GameObject.Find("FoodHolder").transform;
MakeFood(false);
}
/// <summary>
/// 生成食物
/// </summary>
public void MakeFood(bool isReward)
{
int index = Random.Range(0,foodSprites.Length);
GameObject food = Instantiate(foodPrefab);
food.GetComponent<Image>().sprite = foodSprites[index];
food.transform.SetParent(foodHolder,false);//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);
reward.transform.SetParent(foodHolder, false);//false是否保持世界坐标,不转换坐标
x = Random.Range(-xlimit + xoffset, xlimit);
y = Random.Range(-ylimit, ylimit);
reward.transform.localPosition = new Vector3(x * 30, y * 30, 0);
}
}
}
标签:false,07,void,isOn,贪吃蛇,SetString,PlayerPrefs,SIKI,public 来源: https://blog.csdn.net/qq_42983775/article/details/90312132