您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關C#如何實現控制臺飛行棋小游戲,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
本文實例為大家分享了C#實現控制臺飛行棋小游戲的具體代碼,供大家參考,具體內容如下
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace 飛行棋小游戲 { class Program { /// <summary> /// 地圖 /// </summary> static int[] Maps = new int[100]; /// <summary> /// 用戶姓名 /// </summary> static string[] playerName = new string[2]; /// <summary> /// 兩名玩家的位置 /// </summary> static int[] playerPosition = new int[2]; /// <summary> /// 標記兩個玩家誰該進行投擲篩子 /// </summary> static bool[] playerFlag = new bool[2]; static void Main(string[] args) { //游戲標題 ShowTitle(); #region///用戶名注冊 Console.WriteLine("請兩名玩家先進行用戶注冊!"); Console.WriteLine("注冊規則:不能為空,并且不能是純數字,玩家AB姓名不能相同"); Console.WriteLine("請輸入玩家A的姓名:"); string pattern = @"^\d+$"; bool isok = true; while (isok) { playerName[0] = Console.ReadLine(); if (string.IsNullOrWhiteSpace(playerName[0])) { Console.WriteLine("玩家A的姓名不能為空,請重新輸入"); } else if (Regex.IsMatch(playerName[0], pattern)) { Console.WriteLine("玩家A的姓名不能是純數字,請重新輸入"); } else { isok = false; } } isok = true; Console.WriteLine("請輸入玩家B的姓名:"); while (isok) { playerName[1] = Console.ReadLine(); if (string.IsNullOrWhiteSpace(playerName[1])) { Console.WriteLine("玩家B的姓名不能為空,請重新輸入"); } else if (Regex.IsMatch(playerName[1], pattern)) { Console.WriteLine("玩家B的姓名不能是純數字,請重新輸入"); } else if (playerName[0] == playerName[1]) { Console.WriteLine("玩家B的姓名已被占用,請重新輸入"); } else { isok = false; } } #endregion Console.Clear(); ShowTitle(); ShowRule(); InitialMap(); DrawMap(); //開始進入游戲 //判斷兩名玩家都未到達終點則游戲繼續 while (playerPosition[0] < 99 && playerPosition[1] < 99) { if (playerFlag[0] == false) { PlayGame(0); } else { playerFlag[0] = false; } if (playerFlag[1] == false) { PlayGame(1); } else { playerFlag[1] = false; } if (playerPosition[0] >= 99) { Console.WriteLine("恭喜玩家【{0}】獲勝!", playerName[0]); break; } else if (playerPosition[1] >= 99) { Console.WriteLine("恭喜玩家【{0}】獲勝!", playerName[1]); break; } } Console.ReadLine(); } /// <summary> /// 繪制游戲標題 /// </summary> static void ShowTitle() { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*****************飛行棋*****************"); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("****************************************"); } /// <summary> /// 繪制游戲規則 /// </summary> static void ShowRule() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("------------------------------------------------------------"); Console.WriteLine("玩家【{0}】使用A表示", playerName[0]); Console.WriteLine("玩家【{0}】使用B表示", playerName[1]); Console.WriteLine("規則說明:"); Console.WriteLine("1.玩家A先擲骰子,A、B玩家輪流投擲骰子"); Console.WriteLine("2.“□”普通格子對于玩家沒有任何獎懲!"); Console.WriteLine("3.“◎”幸運輪盤玩家具有兩種選擇:a.選擇與對方交換位置;b.選擇轟炸對方使對方倒退6步"); Console.WriteLine("4.“★”地雷對于玩家懲罰使玩家倒退6步"); Console.WriteLine("5.“▲”暫停懲罰玩家下一輪暫停操作"); Console.WriteLine("6.“卍”時空隧道獎勵玩家直接前進10步"); Console.WriteLine("7.如果踩到對方則懲罰對方直接倒退6步"); Console.WriteLine("8.“<>”代表玩家AB位置重合"); Console.WriteLine("------------------------------------------------------------"); } /// <summary> /// 初始化地圖 /// </summary> static void InitialMap() { //確定“◎”=1 int[] lunckturn = { 6, 23, 40, 55, 69, 83 }; for (int i = 0; i < lunckturn.Length; i++) { int index = lunckturn[i]; Maps[index] = 1; } //確定“★”=2 int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; for (int i = 0; i < landmine.Length; i++) { Maps[landmine[i]] = 2; } //確定“▲”=3 int[] pause = { 9, 27, 60, 93 }; for (int i = 0; i < pause.Length; i++) { Maps[pause[i]] = 3; } //確定“卍”=4 int[] timeTunnel = { 20, 25, 45, 63, 88, 90 }; for (int i = 0; i < timeTunnel.Length; i++) { Maps[timeTunnel[i]] = 4; } } /// <summary> /// 繪制地圖 /// </summary> static void DrawMap() { //第一行 for (int i = 0; i < 30; i++) { Console.Write(DrawString(i)); } Console.WriteLine(); //第一列 for (int i = 30; i < 35; i++) { for (int k = 0; k < 29; k++) { Console.Write(" "); } Console.Write(DrawString(i)); Console.WriteLine(); } //第二行 for (int i = 64; i > 34; i--) { Console.Write(DrawString(i)); } Console.WriteLine(); //第二列 for (int i = 65; i < 70; i++) { Console.WriteLine(DrawString(i)); } //第三行 for (int i = 70; i < 100; i++) { Console.Write(DrawString(i)); } Console.WriteLine(); } /// <summary> /// 根據地圖數組每個位置的值選擇繪制的圖案 /// </summary> /// <param name="type">地圖數組中位置數字代表地圖位置索引</param> /// <returns>返回最終選擇的圖案</returns> static string DrawString(int index) { string str = ""; //兩名玩家的位置重合,并且保證兩個玩家的位置都在地圖中 if (playerPosition[0] == playerPosition[1] && playerPosition[0] == index) { Console.ForegroundColor = ConsoleColor.White; str = "<>"; } else if (playerPosition[0] == index) { Console.ForegroundColor = ConsoleColor.DarkYellow; str = "A"; } else if (playerPosition[1] == index) { Console.ForegroundColor = ConsoleColor.DarkCyan; str = "B"; } else { switch (Maps[index]) { case 0: Console.ForegroundColor = ConsoleColor.Cyan; str = "□"; break; case 1: Console.ForegroundColor = ConsoleColor.Magenta; str = "◎"; break; case 2: Console.ForegroundColor = ConsoleColor.Red; str = "★"; break; case 3: Console.ForegroundColor = ConsoleColor.DarkBlue; str = "▲"; break; case 4: Console.ForegroundColor = ConsoleColor.Green; str = "卍"; break; default: break; } } return str; } /// <summary> /// 實現游戲功能的方法 /// </summary> /// <param name="playerIndex">通過玩家索引判斷本次游戲操作是由哪個玩家發起的</param> static void PlayGame(int playerIndex) { #region ///為了實現玩家擲骰子算出移動步數 Random r = new Random(); Console.WriteLine("本回合由玩家【{0}】按下任意鍵擲骰子!", playerName[playerIndex]); Console.ReadKey(true); int number = r.Next(1, 7); Console.WriteLine("玩家【{0}】擲出<{1}>點", playerName[playerIndex], number); Console.WriteLine("玩家【{0}】按下任意鍵開始移動!", playerName[playerIndex]); Console.ReadKey(true); playerPosition[playerIndex] += number; Console.WriteLine("玩家【{0}】移動完成!", playerName[playerIndex]); #endregion CheckPosition(); #region///進行對移動結果的格子獎懲判斷 //先判斷兩個玩家是否踩到對方 if (playerPosition[0] == playerPosition[1]) { Console.WriteLine("玩家【{0}】踩到玩家【{1}】,玩家【{1}】退6格!", playerName[playerIndex], playerName[1 - playerIndex]); playerPosition[1 - playerIndex] -= 6; } else { switch (Maps[playerPosition[playerIndex]]) { //踩到普通格子 case 0: Console.WriteLine("玩家【{0}】踩到安全地帶!無獎懲!按下任意鍵刷新界面!", playerName[playerIndex]); Console.ReadKey(true); break; //踩到幸運輪盤 case 1: Console.WriteLine("玩家【{0}】踩到幸運輪盤!請選擇:a--交換位置,b--轟炸對方", playerName[playerIndex]); string type = Console.ReadLine(); while (true) { if (type == "a") { Console.WriteLine("玩家【{0}】選擇與玩家【{1}】交換位置", playerName[playerIndex], playerName[1 - playerIndex]); int temp = playerPosition[0]; playerPosition[0] = playerPosition[1]; playerPosition[1] = temp; Console.WriteLine("玩家【{0}】與玩家【{1}】交換位置完成,按下任意鍵刷新界面!", playerName[playerIndex], playerName[1 - playerIndex]); Console.ReadKey(true); break; } else if (type == "b") { Console.WriteLine("玩家【{ 0}】選擇轟炸玩家【{ 1}】", playerName[playerIndex], playerName[1 - playerIndex]); playerPosition[1 - playerIndex] -= 6; Console.WriteLine("玩家【{ 0}】轟炸玩家【{ 1}】完成,按下任意鍵刷新界面!", playerName[playerIndex], playerName[1 - playerIndex]); Console.ReadKey(true); break; } else { Console.WriteLine("請輸入正確的指令!請選擇:a--交換位置,b--轟炸對方"); type = Console.ReadLine(); } } break; //踩到地雷 case 2: Console.WriteLine("玩家【{0}】踩到地雷,退6格!按下任意鍵刷新界面", playerName[playerIndex]); playerPosition[playerIndex] -= 6; Console.ReadKey(true); break; //踩到暫停 case 3: Console.WriteLine("玩家【{0}】踩到暫停,下一回合暫停!按下任意鍵刷新界面", playerName[playerIndex]); playerFlag[playerIndex] = true; break; //踩到時空隧道 case 4: Console.WriteLine("玩家【{0}】踩到時空隧道,前進10步!按下任意鍵刷新界面", playerName[playerIndex]); playerPosition[playerIndex] += 10; Console.ReadKey(true); break; default: break; } } CheckPosition(); Console.Clear(); ShowTitle(); ShowRule(); InitialMap(); DrawMap(); #endregion } /// <summary> /// 斷是否超越終點或者超出起點 /// </summary> static void CheckPosition() { if (playerPosition[0] < 0) { playerPosition[0] = 0; } if (playerPosition[0] > 99) { playerPosition[0] = 99; } if (playerPosition[1] < 0) { playerPosition[1] = 0; } if (playerPosition[1] > 99) { playerPosition[1] = 99; } } } }
關于“C#如何實現控制臺飛行棋小游戲”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。