首页 分享 利用unity3D制作贪吃蛇

利用unity3D制作贪吃蛇

来源:萌宠菠菠乐园 时间:2024-11-26 13:57

源码资源

https://download.csdn.net/download/it_create/10625733

或https://gitee.com/dxl96/qt_resource/blob/master/snakes.zip

开始界面

运行界面

MainUIController.cs

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 int score = 0;

public int length = 0;

public Text msgText;

public Text scoreText;

public Text lengthText;

public Image bgImage;

private Color tempColor;

public bool isPause = false;

public Image pauseImage;

public Sprite[] pauseSprites = new Sprite[2];

public bool hasboder = true;

void Awake()

{

_instance = this;

}

void Start()

{

if(PlayerPrefs.GetInt("border",1) == 0)

{

hasboder = 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;

break;

case 5:

case 6:

ColorUtility.TryParseHtmlString("#CDFFDBEE", out tempColor);

bgImage.color = tempColor;

msgText.text = "阶段" + 3;

break;

case 7:

case 8:

ColorUtility.TryParseHtmlString("#EBFFDBFF", 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);

}

}

StartUIController.cs

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 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);

}

}

void Awake()

{

lastText.text = "上次:长度" + PlayerPrefs.GetInt("lastl", 0) + ",分数" + PlayerPrefs.GetInt("lasts", 0);

bestText.text = "最佳:长度" + PlayerPrefs.GetInt("bestl", 0) + ",分数" + PlayerPrefs.GetInt("bests", 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 BoderSelected(bool isOn)

{

if (isOn)

{

PlayerPrefs.SetInt("border", 1);

}

}

public void NoBoderSelected(bool isOn)

{

if (isOn)

{

PlayerPrefs.SetInt("border", 0);

}

}

public void StartGame()

{

UnityEngine.SceneManagement.SceneManager.LoadScene(1);

}

}

foodmaker.cs

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 = 27;

public int ylimit = 10;

public int xoffset = 10;

public GameObject foodPrefab;

public GameObject rewardPrefab;

public Sprite[] foodSprites;

private Transform foodHolder;

void Awake()

{

_instance = this;

}

void Start () {

foodHolder = GameObject.Find("FoodHolder").transform;

MakeFood(false);

}

void Update () {

}

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);

int x = Random.Range(-xlimit + xoffset, xlimit);

int y = Random.Range(-ylimit, ylimit);

food.transform.localPosition = new Vector3(x * 15, y * 15, 0);

if(isReward)

{

GameObject reward = Instantiate(rewardPrefab);

reward.transform.SetParent(foodHolder, false);

x = Random.Range(-xlimit + xoffset, xlimit);

y = Random.Range(-ylimit, ylimit);

reward.transform.localPosition = new Vector3(x * 15, y * 15, 0);

}

}

}

SnakeHead.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class SnakeHead : MonoBehaviour {

public int step;

public float velocity=0.35f;

private int x;

private int y;

private Vector3 headpos;

public GameObject bodyprefab;

public List<Transform> bodyList = new List<Transform>();

public Sprite[] bodySprites = new Sprite[2];

public Transform canvas;

private bool isDie = false;

public GameObject dieEffect;

public AudioClip eatClip;

public AudioClip dieClip;

void Start()

{

InvokeRepeating("move", 0, velocity);

x = 0; y = step;

}

void Awake()

{

canvas = GameObject.Find("Canvas").transform;

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 Update()

{

if(Input.GetKeyDown(KeyCode.Space)&&MainUIController.Instance.isPause==false && isDie ==false)

{

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.W) && MainUIController.Instance.isPause == false && isDie == false)

{

gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);

x = 0;y = step;

}

if (Input.GetKey(KeyCode.S) && MainUIController.Instance.isPause == false && isDie == false)

{

gameObject.transform.localRotation = Quaternion.Euler(0, 0, 180);

x = 0;y = -step;

}

if (Input.GetKey(KeyCode.A) && MainUIController.Instance.isPause == false && isDie == false)

{ gameObject.transform.localRotation = Quaternion.Euler(0, 0, 90);

x = -step; y = 0;

}

if (Input.GetKey(KeyCode.D) && 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)

{

for(int i = bodyList.Count -2; i>=0; i--)

{

bodyList[i + 1].localPosition = bodyList[i].localPosition;

}

bodyList[0].localPosition = headpos;

}

}

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);

body.GetComponent<Image>().sprite = bodySprites[index];

body.transform.SetParent(canvas, false);

bodyList.Add(body.transform);

}

public void Die()

{

AudioSource.PlayClipAtPoint(dieClip, Vector3.zero);

CancelInvoke();

isDie = true;

Instantiate(dieEffect);

PlayerPrefs.SetInt("lastl", MainUIController.Instance.length);

PlayerPrefs.SetInt("lasts", MainUIController.Instance.score);

if(PlayerPrefs.GetInt("bests",0)<MainUIController.Instance.score)

{

PlayerPrefs.SetInt("bestl", MainUIController.Instance.length);

PlayerPrefs.SetInt("bests", MainUIController.Instance.score);

}

StartCoroutine(GameOver(1.5f));

}

IEnumerator GameOver(float t)

{

yield return new WaitForSeconds(t);

UnityEngine.SceneManagement.SceneManager.LoadScene(1);

}

private void OnTriggerEnter2D(Collider2D collider)

{

if (collider.gameObject.CompareTag("food"))

{

Destroy(collider.gameObject);

MainUIController.Instance.UpdateUI();

Grow();

foodmaker.Instance.MakeFood(Random.Range(0,100)<20?true:false);

}

else if(collider.gameObject.CompareTag("Reward"))

{

Destroy(collider.gameObject);

MainUIController.Instance.UpdateUI(Random.Range(5,15)*10);

Grow();

}

else if(collider.gameObject.CompareTag("body"))

{

Die();

}

else

{

if(MainUIController.Instance.hasboder)

{

Die();

}

else

{

switch (collider.gameObject.name)

{

case "up":

transform.localPosition = new Vector3(transform.localPosition.x, -transform.localPosition.y + 15, transform.localPosition.z);

break;

case "down":

transform.localPosition = new Vector3(transform.localPosition.x, -transform.localPosition.y - 15, transform.localPosition.z);

break;

case "left":

transform.localPosition = new Vector3(-transform.localPosition.x + 135, transform.localPosition.y, transform.localPosition.z);

break;

case "right":

transform.localPosition = new Vector3(-transform.localPosition.x + 165, transform.localPosition.y, transform.localPosition.z);

break;

}

}

}

}

}

相关知识

如何使用unity3D制作游戏
利用unity3D制作贪吃蛇
基于单片机的贪吃蛇游戏设计
单片机贪吃蛇 毕业设计.pdf
如何在贪吃蛇大作战®中获得更多的背景宠物?
贪吃蛇游戏程序设计实验报告
python+pygame 贪吃蛇游戏
如何在贪吃蛇大作战®中获得更多的比赛宠物外观卡牌?
[教材]Unity3D——宠物的跟随和自动攻击
贪吃蛇无尽大作战宠物培养攻略

网址: 利用unity3D制作贪吃蛇 https://www.mcbbbk.com/newsview657290.html

所属分类:萌宠日常
上一篇: JS 原生js实现贪吃蛇小游戏(
下一篇: C#贪吃蛇小游戏

推荐分享