Pop
来源:互联网 发布:java获取访问者ip mac 编辑:程序博客网 时间:2024/05/03 07:55
自己一款用U3D开发的简单的小游戏
】
一、一个星星类
using UnityEngine;using System.Collections;public class Stars : MonoBehaviour { public int rowIndex = 0; public int columnIndex = 0; public float xoff =-2.16f; public float yoff =-3.14f; public float size = 0.48f; public GameObject[] _Satrs; private GameObject nStar; public int type; public BgController BgControl;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {} void OnMouseDown() { BgControl.Select(this); } public void AddStars() { if (nStar != null) return; type = Random.Range(0, _Satrs.Length);//随机不同颜色的星星 nStar = (GameObject)Instantiate(_Satrs[type]); nStar.transform.parent = this.transform; } public void StarPos() { AddStars(); transform.position = new Vector3(xoff + columnIndex * size, yoff + rowIndex * size, 0); } public void TweenTopos() { AddStars(); ; iTween.MoveTo(this.gameObject, iTween.Hash("x", xoff + columnIndex * size,"y", yoff + rowIndex * size,"time", 0.4f)); } //销毁 public void Dispose() { BgControl = null; Destroy(this.gameObject); }}
二、星星缩放属性
public class StarScale : MonoBehaviour {void Start () { int s = Random.Range(0, 5); float t = 0.0f; if (s >2) t = 2.0f; else if (s >0) t = 3.0f; else t = 0.5f; iTween.ScaleFrom(this.gameObject, Vector3.zero, t);}}
三、主要函数
。。。enum GameState { Playing, Pause, Lose, Win}public class BgController : MonoBehaviour { ...省略一些... IEnumerator Start () { //关卡++ level++; //生成目标分数 goal = 1000 * (level + 1) * level / 2; goalTxt.text = goal.ToString(); //关卡信息的输出 LevelTxt.text = level.ToString(); //移动的目标分数 tagerTxt.text = "目标:" + goalTxt.text; //移动的关卡信息 t.text = "Level "+ level.ToString(); //玩家的分数信息 scoreTxt.text = Score.ToString(); BestScoreTxt.text = PlayerPrefs.GetInt("BestScore", 0).ToString(); //0.5s后加载星星 yield return new WaitForSeconds(3.5f); StartGameStars();//等待3.5s,初始化星星} void Update() { //判断游戏是否重新开始 if (this.GetComponent<BtnControl>().reStart) { Debug.Log("reStart!!!"); level = 0; Score = 0; this.GetComponent<BtnControl>().reStart = false; Application.LoadLevel("PlayGame"); //Time.timeScale = 0; } //判断游戏是否暂停 if (this.GetComponent<BtnControl>().pause) { gameState = GameState.Pause; Time.timeScale = 0; PauseMenu.SetActive(true); } else { gameState = GameState.Playing; Time.timeScale = 1; PauseMenu.SetActive(false); } //按下返回键,返回暂停界面 if (Input.GetKey(KeyCode.Escape)) { this.GetComponent<BtnControl>().pause = true; } } //添加星星 private void StartGameStars() { for (int x = 0; x < RowNum; x++)//hang { for (int y = 0; y < ColumnNum; y++)//lie { i2++; //add stars starArr[x, y] = Instantiate(Star) as GameObject; starArr[x, y].gameObject.name = i2.ToString(); Stars s = starArr[x, y].GetComponent<Stars>();//获取当前的candy脚本 s.transform.parent = StarParent.transform; s.columnIndex = y;//糖果的行列 s.rowIndex = x; s.StarPos();//初始化糖果的位置 s.BgControl = this; } } } //选择的星星 public void Select(Stars s) { CheckSelect(s); if (matches.Count > 1 && gameState == GameState.Playing) { StartCoroutine(RemoveMatches(0.05f)); AddScore(); } //RemoveMatches(); } //算法检测 private void CheckSelect(Stars s) { matches = new ArrayList(); AddMatch(s); for (int y = 0; y < matches.Count; y++) { Stars s1 = matches[y] as Stars; //左 if (s1.columnIndex > 0 && starArr[s1.rowIndex, s1.columnIndex - 1]!=null && s1.type == starArr[s1.rowIndex, s1.columnIndex - 1].GetComponent<Stars>().type) { Stars a = starArr[s1.rowIndex, s1.columnIndex - 1].GetComponent<Stars>(); AddMatch(a); } //右 if (s1.columnIndex < ColumnNum - 1 && starArr[s1.rowIndex, s1.columnIndex + 1] !=null && s1.type == starArr[s1.rowIndex, s1.columnIndex + 1].GetComponent<Stars>().type) { Stars a = starArr[s1.rowIndex, s1.columnIndex + 1].GetComponent<Stars>(); AddMatch(a); } //上 if (s1.rowIndex < RowNum - 1 && starArr[s1.rowIndex + 1, s1.columnIndex] !=null && s1.type == starArr[s1.rowIndex + 1, s1.columnIndex].GetComponent<Stars>().type) { Stars a = starArr[s1.rowIndex + 1, s1.columnIndex].GetComponent<Stars>(); AddMatch(a); } //下 if (s1.rowIndex > 0 && starArr[s1.rowIndex - 1, s1.columnIndex]!=null && s1.type == starArr[s1.rowIndex - 1, s1.columnIndex].GetComponent<Stars>().type) { Stars a = starArr[s1.rowIndex - 1, s1.columnIndex].GetComponent<Stars>(); AddMatch(a); } } } //将与选择颜色一样的星星放在一个数组private void AddMatch(Stars s){if (matches == null)matches = new ArrayList();int index = matches.IndexOf(s);//判断是否重复if (index == -1)//没有则加入matches.Add(s);} //移除待消除的星星 private IEnumerator RemoveMatches(float t) { Stars tmp; for (int i = 0; i < matches.Count; i++) { tmp = matches[i] as Stars; yield return new WaitForSeconds(t); Remove(tmp); //判断是否为奖励时的最后一个,是的则加上分数,并开始新的关卡 if (addk && a == 0 && i == matches.Count - 1) { a++; StartCoroutine(reward()); } } StartCoroutine(MoveDown()); matches = new ArrayList(); } //最后的奖励 private IEnumerator reward() { yield return new WaitForSeconds(0.2f); //显示奖励分数 _Sf.text = "奖励 " + k.ToString(); StartCoroutine(ShowTxt()); Score += k; scoreTxt.text = Score.ToString(); addk = false; StartCoroutine(gameLevel()); } //判断游戏输赢,赢则下一关 private IEnumerator gameLevel() { //StartCoroutine(GameP()); yield return new WaitForSeconds(2.0f); if (Score < goal) { gameState = GameState.Lose; BestScores(); } else { gameState = GameState.Win; } if (gameState == GameState.Win) { Debug.Log("win"); Application.LoadLevel("PlayGame"); } else if (gameState == GameState.Lose) { Debug.Log("lose"); Lose.SetActive(true); StartCoroutine(Home()); } } //销毁星星 private void Remove(Stars s) { GameObject go = Instantiate(miniStars, s.gameObject.transform.position, Quaternion.identity) as GameObject; iTween.MoveTo(go, iTween.Hash("x", miniStarsTarget.transform.position.x, "y", miniStarsTarget.transform.position.y, "time", 1.5f)); Destroy(go, 0.8f); s.Dispose(); audioSource.PlayOneShot(SoundClips[0]); } //星星下移 private IEnumerator MoveDown() { yield return new WaitForSeconds(0.00f); for (int x = 0; x < RowNum - 1; x++) { for (int y = 0; y < ColumnNum; y++) { if (starArr[x, y] == null) { int i = 1; while (x + i<RowNum && starArr[x + i, y] == null) { i++; } if ( x + i < RowNum && starArr[x + i, y] != null ) { starArr[x, y] = starArr[x + i, y]; starArr[x + i, y] = null; Stars s = starArr[x, y].GetComponent<Stars>(); s.rowIndex = x; s.columnIndex = y; s.TweenTopos(); } } } } StartCoroutine(MoveLeft()); } //星星向左移动 private IEnumerator MoveLeft() { yield return new WaitForSeconds(0.05f); //当有空列时,向左移动 for (int y = 0; y < ColumnNum-1; y++)//列 { int k = 0; for (int x = 0; x < RowNum; x++) { if (starArr[x, y] == null) { k++; } } if (k == 10) { int i = 1; while (y + i < ColumnNum && starArr[0, y + i] == null) { i++; } for (int x = 0; x < RowNum; x++) { if (y + i < ColumnNum && starArr[x, y + i] != null) { starArr[x, y] = starArr[x, y + i]; starArr[x, y + i] = null; Stars s = starArr[x, y].GetComponent<Stars>(); s.rowIndex = x; s.columnIndex = y; s.TweenTopos(); } } } } StartCoroutine(CheckGame()); } //检测游戏是否结束,并加载新的场景 private IEnumerator CheckGame() { yield return new WaitForSeconds(0.3f); if (isNoMore()) { Debug.Log("nomore"); StartCoroutine(GameP()); } } //遍历行列信息,判断游戏是否结束 private bool isNoMore() { for (int x = 0; x < RowNum; x++) { for (int y = 0; y < ColumnNum; y++) { if (starArr[x, y] != null && y + 1 < ColumnNum && starArr[x, y + 1] != null) { if (starArr[x, y].GetComponent<Stars>().type == starArr[x, y + 1].GetComponent<Stars>().type) return false; } } } for (int y = 0; y < ColumnNum; y++) { for (int x = 0; x < RowNum; x++) { if (starArr[x, y] != null && starArr[x + 1, y] != null) { if (starArr[x, y].GetComponent<Stars>().type == starArr[x + 1, y].GetComponent<Stars>().type) return false; } } } return true; } //弹出NoMore提示,没有更多的可消除星星 int i = 0; private IEnumerator GameP() { yield return new WaitForSeconds(0.4f); NoMore.SetActive(true); for (int x = RowNum-1; x >-1; x--) { for (int y = ColumnNum-1; y >-1 ; y--) { if (starArr[x, y] != null) { i++; if (i > 10) { break; } Stars s = starArr[x, y].GetComponent<Stars>(); AddMatch(s); k -= (20 + (i - 1) * 40); } } } if (matches.Count > 0) StartCoroutine(RemoveMatches(0.1f)); else { if (a == 0) { a++; StartCoroutine(reward()); } } addk = true; } //分数计算 private void AddScore() { if (isNoMore() == false) { int sf = 0; for (int i = 1; i <= matches.Count; i++) { sf = sf + ((i - 1) * 10 + 5); } _Sf.text = matches.Count.ToString() + "连消 " + sf.ToString(); StartCoroutine(ShowTxt()); Score += sf; scoreTxt.text = Score.ToString(); } } IEnumerator ShowTxt() { yield return new WaitForSeconds(1.0f); _Sf.text = " "; } private void BestScores() { int s = PlayerPrefs.GetInt("BestScore", 0); if (Score > s) { PlayerPrefs.SetInt("BestScore", Score); } } private IEnumerator Home() { yield return new WaitForSeconds(3.0f); Application.LoadLevel("UI_Menu"); }}
可能存在很多不足希望大家见谅。
菜鸟初作,不喜勿抨,个人学习。
希望有大神指点。
0 0
- pop
- pop
- pop
- pop
- pop
- Pop
- .pop
- pop
- Pop
- Pop
- pop
- Balloon Pop Pop
- SMTP&POP
- Array#pop
- POP服务器
- pthread_cleanup_push/pop
- pop smtp
- 一个pop
- 单调队列入门
- 图解Nginx-Nginx进程模型1
- 蓝桥杯--猴子选大王-约瑟夫问题
- 1097. Deduplication on a Linked List (25)
- cp命令和cp-r的区别-----Python学习中的小贴士E01
- Pop
- 让Tux逃离虚拟世界
- 消息摘要 —— MD5算法
- GDKOI2016滚粗记
- 【转载】点击率预估的几个经典模型简介
- R语言dplyr简介
- zookeeper中选举leader过程
- 1098. Insertion or Heap Sort (25)
- HDU1711-Number Sequence-KMP