1. 程式人生 > 其它 >unity翻牌遊戲

unity翻牌遊戲

技術標籤:unity

程式碼

/**
 * 
 * 
 *    Title:
 *
 *    description:
 *     遊戲主控
 *     
 *
 *    Date:
 *
 *    version:
 *
 *    modify Recoder:
 * 
 * 
 **/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameControler : MonoBehaviour
{ private int[,] CardsArray = new int[4,5]; private int intFirstNumCard; //第一張數字牌 private int intSecondNumCard; //第二張數字牌 private Button BtnFirstNumCard; //第一張數字牌控制元件
private Button BtnSecondNumCard; //第二張數字牌控制元件 private bool boolIsFirstClick = true; //判斷是否是第一次點選 private float Temptime; private float pubulictime; private
float[] buttontime = new float[20]; //定義按鈕控制元件 public Button btn1_1; public Button btn1_2; public Button btn1_3; public Button btn1_4; public Button btn1_5; public Button btn2_1; public Button btn2_2; public Button btn2_3; public Button btn2_4; public Button btn2_5; public Button btn3_1; public Button btn3_2; public Button btn3_3; public Button btn3_4; public Button btn3_5; public Button btn4_1; public Button btn4_2; public Button btn4_3; public Button btn4_4; public Button btn4_5; void Start() { //初始化二維陣列 StartPrepareGameData(); //buttontime初始化 buttontimevaluaing(); //使用協程,進行輪詢檢測 //StartCoroutine(JudgePuker(2)); } void Update() { pubulictime += Time.deltaTime; Temptime += Time.deltaTime; //定義一個不停走動的臨時時間 if (Temptime > 2) //兩秒檢測一次牌面 { JudgePukerEquals(BtnFirstNumCard, BtnSecondNumCard, intFirstNumCard, intSecondNumCard); Temptime = 0; } } void buttontimevaluaing() { for (int i = 0; i < 20; i++) { buttontime[i] = pubulictime; } } //使用協程,進行輪詢檢測 IEnumerable JudgePuker(int intTime)//IEnumerable C#內部定定義的介面,迭代器結構 { while (true) { //等待指定描述 yield return new WaitForSeconds(intTime); } } //比較兩個button,如果相同就變灰,不相同就還原 private void JudgePukerEquals(Button btn1,Button btn2,int numbercard1,int numbercard2) { if (btn1 != null && btn2 != null) //要求前兩個按鈕不為空 { if (numbercard1 == numbercard2) //當兩個按鈕的數值 { Debug.Log(btn1.GetHashCode()); //得到“雜湊值”(C#裡面每個物件都有gethahcode,這是定義在父類object裡面的) Debug.Log(btn2.GetHashCode()); if (btn1 .GetHashCode() !=btn2.GetHashCode()) //要求不為同一個按鈕 { //如果相同就顯示為灰色 switch (numbercard1) { case 1: btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/1B", typeof(Sprite)) as Sprite; btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/1B", typeof(Sprite)) as Sprite; break; case 2: btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/2B", typeof(Sprite)) as Sprite; btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/2B", typeof(Sprite)) as Sprite; break; case 3: btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/3B", typeof(Sprite)) as Sprite; btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/3B", typeof(Sprite)) as Sprite; break; case 4: btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/4B", typeof(Sprite)) as Sprite; btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/4B", typeof(Sprite)) as Sprite; break; case 5: btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/5B", typeof(Sprite)) as Sprite; btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/5B", typeof(Sprite)) as Sprite; break; case 6: btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/6B", typeof(Sprite)) as Sprite; btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/6B", typeof(Sprite)) as Sprite; break; case 7: btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/7B", typeof(Sprite)) as Sprite; btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/7B", typeof(Sprite)) as Sprite; break; case 8: btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/8B", typeof(Sprite)) as Sprite; btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/8B", typeof(Sprite)) as Sprite; break; case 9: btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/9B", typeof(Sprite)) as Sprite; btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/9B", typeof(Sprite)) as Sprite; break; default: break; //停止點選處理 } //配對之後停止button監聽 btn2.GetComponent<Button>().enabled = false; btn1.GetComponent<Button>().enabled = false; //重置比較的數字牌 intFirstNumCard = 0; intSecondNumCard = 0; } } else { //button沒有被鎖住就翻轉回來 if(btn1.GetComponent<Button>().enabled==true) { btn1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; } if(btn2.GetComponent<Button>().enabled == true) { btn2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; } } } } //對每個陣列下標隨機新增數值,新增背景圖 private void StartPrepareGameData() { ArrayList al = CreateRandomDoublePlayCard(); //獲得成對出現的數列 IEnumerator ietor = al.GetEnumerator(); //得到一個迭代器介面 //遍歷二位陣列 for(int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { if (ietor.MoveNext()) { CardsArray[i, j] = (int)ietor.Current; //迭代器可以依次的把al裡面的數吐出來 } } } #region 新增背景牌的初始圖 btn1_1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn1_2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn1_3.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn1_4.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn1_5.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn2_1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn2_2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn2_3.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn2_4.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn2_5.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn3_1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn3_2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn3_3.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn3_4.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn3_5.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn4_1.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn4_2.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn4_3.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn4_4.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; btn4_5.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/PokerBG3", typeof(Sprite)) as Sprite; #endregion } private ArrayList CreateRandomDoublePlayCard() //使陣列成對出現 { ArrayList al = new ArrayList(); //產生10對數字牌 for (int i = 0; i < 10; i++) //隨機新增是個數 { al.Add(Random .Range(1, 9)); } //使其成對出現 for (int i = 0; i < 9; i++) //複製之前新增的十個數 { int intTemp = (int)al[i]; al.Add(intTemp); } //亂序排列 for (int i = 0; i < al.Count; i++) //打亂順序 { int intRandomPosition = Random.Range(1, al.Count); int intTemp = (int)al[i]; al[i] = al[intRandomPosition]; al[intRandomPosition] = intTemp; } return al; } #region 玩家牌面點選響應 public void CilckButtion1_1() { ProcessUserClick(btn1_1, 0, 0); } public void CilckButtion1_2() { ProcessUserClick(btn1_2, 0, 1); } public void CilckButtion1_3() { ProcessUserClick(btn1_3, 0, 2); } public void CilckButtion1_4() { ProcessUserClick(btn1_4, 0, 3); } public void CilckButtion1_5() { ProcessUserClick(btn1_5, 0, 4); } public void CilckButtion2_1() { ProcessUserClick(btn2_1, 1, 0); } public void CilckButtion2_2() { ProcessUserClick(btn2_2, 1, 1); } public void CilckButtion2_3() { ProcessUserClick(btn2_3, 1, 2); } public void CilckButtion2_4() { ProcessUserClick(btn2_4, 1, 3); } public void CilckButtion2_5() { ProcessUserClick(btn2_5, 1, 4); } public void CilckButtion3_1() { ProcessUserClick(btn3_1, 2, 0); } public void CilckButtion3_2() { ProcessUserClick(btn3_2, 2, 1); } public void CilckButtion3_3() { ProcessUserClick(btn3_3, 2, 2); } public void CilckButtion3_4() { ProcessUserClick(btn3_4, 2, 3); } public void CilckButtion3_5() { ProcessUserClick(btn3_5, 2, 4); } public void CilckButtion4_1() { ProcessUserClick(btn4_1, 3, 0); } public void CilckButtion4_2() { ProcessUserClick(btn4_2, 3, 1); } public void CilckButtion4_3() { ProcessUserClick(btn4_3, 3, 2); } public void CilckButtion4_4() { ProcessUserClick(btn4_4, 3, 3); } public void CilckButtion4_5() { ProcessUserClick(btn4_5, 3, 4); } #endregion private void ProcessUserClick(Button butobj, int RowNumber, int colNumber) { if (boolIsFirstClick) //為第一次點選 { boolIsFirstClick = false; intFirstNumCard = CardsArray[RowNumber,colNumber]; //獲得第一個圖片對應的數 //顯示對應的圖形(動態載入圖片) DisplayPokerCardByNumber(butobj,intFirstNumCard); BtnFirstNumCard = butobj; } else //第二次點選 { boolIsFirstClick = true; intSecondNumCard = CardsArray[RowNumber, colNumber]; //獲得第二個圖片對應的數 //顯示對應的圖形(動態載入圖片) DisplayPokerCardByNumber(butobj, intSecondNumCard); BtnSecondNumCard = butobj; } } //點選顯示牌面,動態的給button賦值圖片 private void DisplayPokerCardByNumber(Button btn, int numberCard) { switch (numberCard) { case 1: btn.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/1A", typeof(Sprite)) as Sprite; break; case 2: btn.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/2A", typeof(Sprite)) as Sprite; break; case 3: btn.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/3A", typeof(Sprite)) as Sprite; break; case 4: btn.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/4A", typeof(Sprite)) as Sprite; break; case 5: btn.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/5A", typeof(Sprite)) as Sprite; break; case 6: btn.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/6A", typeof(Sprite)) as Sprite; break; case 7: btn.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/7A", typeof(Sprite)) as Sprite; break; case 8: btn.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/8A", typeof(Sprite)) as Sprite; break; case 9: btn.GetComponent<Image>().overrideSprite = Resources.Load("Sprites/9A", typeof(Sprite)) as Sprite; break; default: break; } } }