斗舞游戏钱钱钱
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;public class Controller : MonoBehaviour {GameObject main, left, right;// 對應每張牌上的Button組件Button[] buttons;// 每個位置的牌對應哪張圖int[] ids;// 每個位置的按鈕是否被點開bool[] clicked;// 行和列的數量int col= 0, row = 0;// 第一/二個被點擊的牌的位置,游戲對象int firstClickedIdx = -1, secondClickedIdx = -1;// 第一/二個被點擊的牌是哪張牌int firstClickedPer = -1, secondClickedPer = -1;// 第一/二個被點擊的牌的游戲對象GameObject firstClickedGO, secondClickedGO;// 牌背面的精靈Sprite pokerBackSprit;int corrected = 0;//計時器public float time = 100;public Text time_text;//計分public int score = 0;public Text text_score;//音樂public AudioClip audioClip;public AudioSource audioSource;//結束提示public GameObject gameover;public Text end_score;public Text end_time;public Text end_text;public float addtime = 10;public GameObject star;float time_ = 0;void Start () {Time.timeScale = 0;// 相關變量的初始化initVars ();// 準備開始游戲qwprepareGame ();gameover.SetActive(false);}void Update(){//計時if (time > 0 && corrected < 18){time_ = time_ + Time.deltaTime;time = time - Time.deltaTime;time_text.text = "時間 :" + time.ToString("f0") + " S";audioSource.PlayOneShot(audioClip);}else{audioSource.Stop();gameover.SetActive(true);end_text.text = "游 戲 失 敗";end_score.text = "最終得分:" + score + " 分";end_time.text = "最終用時:" + (time_- time).ToString("f2") + " S";}if(time>0&&corrected==18){gameover.SetActive(true);end_text.text = "游 戲 勝 利";end_score.text = "最終得分:" + score + " 分";end_time.text = "最終用時:" + (time_ - time).ToString("f2") + " S";}//計分text_score.text = "得分 :" + score;}public void di(){star.SetActive(false);time = 30;addtime = 10;Time.timeScale = 1;}public void zhong(){star.SetActive(false);time = 20;addtime = 8;Time.timeScale = 1;}public void gao(){star.SetActive(false);time = 10;addtime = 8;Time.timeScale = 1;}public void LoadGame(){SceneManager.LoadScene(0);}void initVars(){// 撲克牌的行數和列數col = 6;row = 3;// 定義一個Button數組,表示每一張牌// Button 表示按鈕上的 Button 組件buttons = new Button[row * col];// 用于Button的格式字符串string strFormat = "Canvas/Main/Left/Button{0}{1}";Debug.Log (string.Format(strFormat,8,1));for (int i = 0; i < row; ++i) {for (int j = 0; j < col; ++j) {// idx 表示數組的索引值// fromRCToIdx 表示 From Row and Col to Index// 比如 第一行第一個(0,0),對應 0// 比如 第二行第二個(1,1),對應 7 = (2-1)*6+(2-1)int idx = fromRCToIdx (new int[2]{ i, j }); // 為數組中的每一個元素賦值,和界面上的牌一一對應buttons [idx] = GameObject.Find (string.Format (strFormat, i, j)).GetComponent<Button> ();// 按鈕自帶字符串,手動刪太麻煩了,因此在這里順手就去掉Text txt = buttons [idx].transform.GetChild (0).gameObject.GetComponent<Text> ();txt.text = "";}}for (int i = 0; i < row; ++i) {for (int j = 0; j < col; ++j) {int idx = fromRCToIdx (new int[2]{ i, j });Button btn = buttons [idx];// 給每一個按鈕增加點擊響應。btn.onClick.AddListener (delegate() {pokerClicked (btn.gameObject);});}}// 指定撲克牌的背景圖片pokerBackSprit = Resources.Load ("Textures/PokerBG1", typeof(Sprite)) as Sprite;// 找到左邊面板left = GameObject.Find ("Canvas/Main/Left");}void prepareGame(){// 為每一個牌分配一個圖片List<int> t = new List<int> ();// 因為準備了 9 組圖片,所以這里生成 18 個隨機數,0-8各出現兩次for (int i = 0; i < 9; ++i) {t.Add (i);t.Add (i);}// ids 是一個整型數組,表示各按鈕對應的圖片// 比如 ids[7] = 8,表示第八張牌對應第九張照片// 注意,第八張牌其實就是第二排第二張ids = new int[row*col];// clicked 記錄的是該按鈕是否被點擊的信息clicked = new bool[row*col];for (int i = 0; i < row; ++i)for (int j = 0; j < col; ++j) {// 每次生成一個隨機數,范圍從 0 到 t 的長度int ii = Random.Range (0, t.Count);// 根據button 的行號和列號得到其索引值,比如(1,1)->7int idx = fromRCToIdx (new int[2]{ i, j }); // 表示該牌沒有被點擊clicked [idx] = false;// 賦予該牌一張照片ids [idx] = t [ii];// 既然照片已經賦給一張牌了,因此將它從 t 中去掉t.RemoveAt (ii);}}int[] getRCFromName(string name){int[] ret = new int[2];ret[0] = int.Parse (name.Substring(6,1));ret [1] = int.Parse (name.Substring(7,1));return ret;}int fromRCToIdx(int[] rc){return rc [0] * col + rc [1];}int[] fromIdxToRC(int idx){int[] ret = new int[2];ret [0] = idx / col;ret [1] = idx % col;return ret;}void showImage(GameObject go){// 獲取行列和索引值int[] rc = getRCFromName (go.name);int idx = fromRCToIdx (rc);// 獲取當前牌的 Image 組件Image img = go.GetComponent<Image> ();// 利用牌的命名規則,從資源中讀取圖像,并顯示Sprite s = Resources.Load (string.Format ("Textures/{0}A", ids [idx] + 1), typeof(Sprite)) as Sprite;img.overrideSprite = s;}void showImageCorrect(GameObject go){int[] rc = getRCFromName (go.name);int idx = fromRCToIdx (rc);Image img = go.GetComponent<Image> ();Sprite s = Resources.Load (string.Format ("Textures/{0}B", ids [idx] + 1), typeof(Sprite)) as Sprite;img.overrideSprite = s;}void showBack(GameObject go){Image img = go.GetComponent<Image> ();img.overrideSprite = pokerBackSprit;}void clearFlag(){firstClickedIdx = -1;firstClickedPer = -1;firstClickedGO = null;secondClickedIdx = -1;secondClickedPer = -1;secondClickedGO = null;}void judgeWin(){if (firstClickedPer == -1 || secondClickedPer == -1)return;// 如果點開的兩張牌圖片相同if (firstClickedPer == secondClickedPer) {// 則顯示灰色showImageCorrect (firstClickedGO);showImageCorrect (secondClickedGO);// 并記錄信息,表示它們已經被點擊過了clicked [firstClickedIdx] = true;clicked [secondClickedIdx] = true;// 記錄正確的數量corrected += 2;score =score+10;time = time + addtime;time_ = time_ + addtime;} else {// 否則繼續顯示背面showImage(firstClickedGO);showImage(secondClickedGO);showBack(firstClickedGO);showBack(secondClickedGO);}// 清空信息clearFlag ();// 如果 18 張牌都對了,游戲結束if (corrected == 18) {Debug.Log ("END");}}// 每個牌被點擊后的代碼void pokerClicked(GameObject go){// 根據名字確定當前牌在第幾行第幾列int[] rc = getRCFromName (go.name);// 進而確定它在 button 數組中的位置int idx = fromRCToIdx (rc);// 確定它對應的是那一張照片int per = ids [idx];// 如果當前牌已經被點擊了(正面向上),則當作什么也沒有發生if (clicked [idx])return;// 如果這是兩張中翻開的第一張if (firstClickedIdx == -1) {// 記錄信息firstClickedIdx = idx;firstClickedPer = per;firstClickedGO = go;// 讓它正面向上showImage (go);// 如果這是兩張中翻開的第二張} else if (secondClickedIdx == -1) {// 如果正好點到了第一張,或者早就翻開的牌,則當作什么也沒有發生if (idx == firstClickedIdx || clicked [idx])return;// 記錄信息secondClickedIdx = idx;secondClickedPer = per;secondClickedGO = go;// 讓它正面向上showImage (go);} else {// 判斷是否游戲結束judgeWin ();}}public void GetExit()//退出運行{
#if UNITY_EDITORUnityEditor.EditorApplication.isPlaying = false;//用于退出運行#else
Application.Quit();
#endif}}
總結
- 上一篇: 实时视频通信技术调研
- 下一篇: 荣耀8viper4android,王者荣