使用二維陣列製作卡牌遊戲的資料庫範例 在 Unity 中使用 二維陣列 來存放卡牌遊戲的資料庫,可以用來儲存卡牌的屬性,例如 卡牌名稱、攻擊力、防禦力、類型 等等。以下是一個 卡牌資料庫(Card Database) 的範例。 1. 基本二維陣列存儲卡牌數據 此範例建立一個基本的 卡牌資料庫,並用 Start() 方法來讀取卡牌的資訊。 "水精靈", "水", 1800, 2200), "噴火龍", "火", 2500, 2000), "風暴鷹", "風", 1900, 1600), "冰霜狼", "冰", 2200, 2300), "雷鳴豹", "雷", 2300, 1700), "聖騎士", "光", 2400, 2100), 程式碼1. GameCard.cs (卡牌類別) using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; // TextMeshPro 支援 public class CardDatabase : MonoBehaviour { public TextMeshProUGUI textDisplay; // UI 文字物件 (需手動設定) private List cards; // 卡牌清單 void Start() { // 初始化卡片列表 cards = new List { new GameCard("火龍", "火", 2500, 2000), new GameCard("水精靈", "水", 1800, 2200), new GameCard("森林妖精", "草", 2000, 1500), new GameCard("雷鳴獵豹", "雷", 2300, 1700) }; DisplayCards(); } void DisplayCards() { if (textDisplay != null) { textDisplay.text = ""; // 清空文字 foreach (GameCard card in cards) { textDisplay.text += card.ToString() + "\n"; } } else { Debug.LogError("請將 TextMeshProUGUI 物件指派給 textDisplay!"); } } } 程式碼 2. CardDatabase.cs (管理 & 顯示卡牌) using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameCard { public string Name { get; private set; } public string Type { get; private set; } public int Attack { get; private set; } public int Defense { get; private set; } public GameCard(string name, string type, int attack, int defense) { Name = name; Type = type; Attack = attack; Defense = defense; } public override string ToString() { return $"名稱:{Name}\n類型:{Type}\n攻擊力:{Attack}\n防禦力:{Defense}\n"; } } Unity 設定步驟 1. 新增 UI 文字 在 Hierarchy 新增 UI → TextMeshPro - Text,命名為 CardText。 調整字型大小、對齊方式,確保可讀性。 放置在適當位置,例如畫面上方或中央。 2. 連結 UI 文字 選取包含 CardDatabase 的 GameObject (例如 CardManager)。 在 Inspector 視窗內,找到 Text Display 欄位。 將 CardText 拖曳到 textDisplay 欄位。 3. 執行遊戲 畫面上會顯示所有卡片資訊 (不再只是 Debug.Log)! 每張卡片都有換行,顯示清楚。 中文若無法顯示,請參考老師另外提供的教學 ★★★ 抽卡牌 ★★★ 新增 DrawCards() 方法 → 隨機抽取 3 張卡牌 並存入 drawnCards 陣列。 新增 UI 按鈕,按下時執行 DrawCards()。 更新 UI,顯示「抽出的卡牌」。 程式碼 1. GameCard.cs(卡牌類別) using UnityEngine; public class GameCard { public string Name { get; private set; } public string Type { get; private set; } public int Attack { get; private set; } public int Defense { get; private set; } public GameCard(string name, string type, int attack, int defense) { Name = name; Type = type; Attack = attack; Defense = defense; } public override string ToString() { return $"名稱:{Name}\n類型:{Type}\n攻擊力:{Attack}\n防禦力:{Defense}\n"; } } 程式碼 2. CardDatabase.cs(管理 & 抽卡) using UnityEngine; using TMPro; using UnityEngine.UI; using System.Collections.Generic; public class CardDatabase : MonoBehaviour { public TextMeshProUGUI allCardsText; // 顯示所有卡牌 public TextMeshProUGUI drawnCardsText; // 顯示抽出的卡牌 public Button drawButton; // 抽卡按鈕 private List cards; // 所有卡牌 private List drawnCards; // 抽出的卡牌 void Start() { // 初始化卡片列表 cards = new List { new GameCard("火龍", "火", 2500, 2000), new GameCard("水精靈", "水", 1800, 2200), new GameCard("森林妖精", "草", 2000, 1500), new GameCard("雷鳴獵豹", "雷", 2300, 1700) }; drawnCards = new List(); // 初始化抽出的卡牌列表 DisplayAllCards(); // 設定按鈕點擊事件 if (drawButton != null) { drawButton.onClick.AddListener(DrawCards); } else { Debug.LogError("請在 Inspector 內指定抽卡按鈕!"); } } void DisplayAllCards() { if (allCardsText != null) { allCardsText.text = "【所有卡牌】\n"; foreach (GameCard card in cards) { allCardsText.text += card.ToString() + "\n"; } } } void DrawCards() { // 確保有足夠的卡牌可以抽 if (cards.Count < 2) { Debug.LogError("卡牌數量不足!"); return; } drawnCards.Clear(); // 清除舊的抽卡結果 // 隨機抽出 3 張不同的卡牌 List drawnIndexes = new List(); while (drawnIndexes.Count < 3) { int randomIndex = Random.Range(0, cards.Count); if (!drawnIndexes.Contains(randomIndex)) // 確保不重複抽到同一張 { drawnIndexes.Add(randomIndex); drawnCards.Add(cards[randomIndex]); } } // 顯示抽出的卡牌 DisplayDrawnCards(); } void DisplayDrawnCards() { if (drawnCardsText != null) { drawnCardsText.text = "【抽出的卡牌】\n"; foreach (GameCard card in drawnCards) { drawnCardsText.text += card.ToString() + "\n"; } } } } Unity 設定步驟 1. 新增 UI 文字 在 Hierarchy 新增 UI → TextMeshPro - Text,命名為 AllCardsText。 複製 AllCardsText,命名為 DrawnCardsText(顯示抽出的卡牌)。 2. 新增按鈕 在 Hierarchy 新增 UI → Button,命名為 DrawButton。 改變按鈕文字,讓它顯示「抽卡」。 選取 DrawButton,找到 Button 組件,點擊 OnClick()。 拖曳 CardDatabase 物件進去,然後選擇 CardDatabase.DrawCards()。 3. 連結 UI 選取 CardDatabase 的 GameObject (例如 CardManager)。 在 Inspector 視窗內: 將 AllCardsText 拖曳到 allCardsText 欄位。 將 DrawnCardsText 拖曳到 drawnCardsText 欄位。 將 DrawButton 拖曳到 drawButton 欄位。 執行結果 - 遊戲開始時,UI 會顯示所有卡牌。 - 每次點擊按鈕,會隨機抽出卡牌並顯示。 - 確保不會抽到相同的卡牌。