使用C#怎么編寫一個飛行棋游戲?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比土默特右旗網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式土默特右旗網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋土默特右旗地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。
管理類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class ProgramManage { //隨機數(shù) public static Random rd = new Random(); //定義地圖大小 public static int[] Maps = new int[100]; // public static bool[] PlayerPause = {false,false }; //定義玩家A和玩家B的坐標(biāo),0是玩家A的坐標(biāo);1是玩家B的坐標(biāo) public static int[] PlayerPos = new int[2]; //存儲兩個玩家的姓名 public static string[] PlayerName = new string[2]; //定義名字錯誤類型 public static string[] Error = {"正確","不能有數(shù)字","重名了","不能為空" }; //畫游戲頭 public static void GanmeShow() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("*************************"); Console.WriteLine("********飛行棋游戲*******"); Console.WriteLine("*************************"); Console.ForegroundColor = ConsoleColor.Green; } //定義地圖關(guān)卡布局 public static void InitailMap() { int[] luckturn = { 6, 23, 40, 55, 69, 83 };//幸運輪盤◎ for (int i = 0; i < luckturn.Length; i++) { Maps[luckturn[i]] = 1; } int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷× for (int i = 0; i < landMine.Length; i++) { Maps[landMine[i]] = 2; } int[] pause = { 9, 27, 60, 93 };//暫停▲ for (int i = 0; i < pause.Length; i++) { Maps[pause[i]] = 3; } int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//時空隧道卍 for (int i = 0; i < timeTunnel.Length; i++) { Maps[timeTunnel[i]] = 4; } } //定義地圖元素。輸入坐標(biāo)值,方法內(nèi)部判斷當(dāng)前坐標(biāo)應(yīng)當(dāng)放什么符號,返回符號值 public static string MapElement(int i) { string Element = "0"; //如果玩家AB坐標(biāo)相同并都在當(dāng)前位置則畫一個“<>” if (PlayerPos[0] == i && PlayerPos[1] == i) { Element="<>"; } //有A畫A,有B畫B,都沒有畫地圖 else if (PlayerPos[0] == i) { Element="A"; } else if (PlayerPos[1] == i) { Element="B"; } else { switch (Maps[i]) { case 0: Element = "□"; break; case 1: Element = "◎"; break; case 2: Element = "×"; break; case 3: Element = "▲"; break; case 4: Element = "卍"; break; } } return Element; } //繪制地圖 public static void DrawMap() { Console.ForegroundColor = ConsoleColor.White; //第一橫行 for (int i = 0; i < 30; i++) { Console.Write(MapElement(i)); } //第一豎行 for (int i = 30; i < 35; i++) { Console.WriteLine(); Console.Write(" "); Console.Write(MapElement(i)); } //第二橫行 Console.WriteLine(); for (int i = 64; i > 34; i--) { Console.Write(MapElement(i)); } //第二豎行 for (int i = 65; i < 70; i++) { Console.WriteLine(); Console.Write(MapElement(i)); } //第三橫行 Console.WriteLine(); for (int i = 70; i < 100; i++) { Console.Write(MapElement(i)); } Console.WriteLine(); } //判斷姓名是否符合要求 public static int NameJudge(string str) { int result = 0; //判斷是否為空 if (str == "") { result = 3; return result; } else { result = 0; } //判斷是否是字符串 for (int i = 0; i < str.Length; i++) { if (char.IsNumber(str,i)) { result = 1; return result; } else { result = 0; } } //判斷是否重名 for (int i = 0; i < PlayerName.Length; i++) { if (PlayerName[i]==str) { result = 2; return result; } else { result = 0; } } return result; } //投骰子 public static void Play(int player) { PlayerPos[player] += rd.Next(1, 7); } //坐標(biāo)不能出地圖 public static void Limit(int player) { if (PlayerPos[player] < 0) { PlayerPos[player] = 0; } else if (PlayerPos[player] > 99) { PlayerPos[player] = 99; } } //執(zhí)行規(guī)則 public static void GameRule(int player) { //踩到對手 if (PlayerPos[player]==PlayerPos[1-player]) { PlayerPos[1 - player] -= 6; Console.WriteLine("{0}踩到對手,對手退6格", PlayerName[player]); } //什么也沒踩到 else if (Maps[PlayerPos[player]] == 0) { Console.WriteLine("{0}玩家前進(jìn)了,什么也沒踩到",PlayerName[player]); } //幸運輪盤 else if (Maps[PlayerPos[player]] == 1) { Console.WriteLine("1,與對手交換位置。2,讓對手退回原點"); string chose; while (true) { chose = Console.ReadLine(); if (chose == "1") { int tem = PlayerPos[player]; PlayerPos[player] = PlayerPos[1 - player]; PlayerPos[1 - player] = tem; Console.WriteLine("{0}與對手交換了位置", PlayerName[player]); break; } else if (chose == "2") { PlayerPos[1 - player] = 0; Console.WriteLine("{0}把對手打回原位", PlayerName[player]); break; } else { Console.WriteLine("請重新輸入"); } } } //地雷 else if (Maps[PlayerPos[player]] == 2) { PlayerPos[player] -= 6; Console.WriteLine("{0}踩到地雷了,倒退6格", PlayerName[player]); } //暫停 else if (Maps[PlayerPos[player]] == 3) { PlayerPause[player] = true; Console.WriteLine("{0}暫停一回合", PlayerName[player]); } //時空隧道 else if (Maps[PlayerPos[player]] == 4) { PlayerPos[player] += 10; Console.WriteLine("{0}前進(jìn)10格", PlayerName[player]); } } //判斷輸贏 public static string Winner() { if (PlayerPos[0]>PlayerPos[1]) { return PlayerName[0]; } else { return PlayerName[1]; } } } }
實現(xiàn)類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //初始化標(biāo)題 ProgramManage.GanmeShow(); //初始化地圖關(guān)卡 ProgramManage.InitailMap(); //輸入名字 string name; for (int i = 0; i < ProgramManage.PlayerName.Length; i++) { Console.WriteLine("請輸入玩家{0}的名字", i+1); name = Console.ReadLine(); while (ProgramManage.NameJudge(name)!=0) { Console.WriteLine("名字格式不對,{0},請重新輸入", ProgramManage.Error[ProgramManage.NameJudge(name)]); name = Console.ReadLine(); } ProgramManage.PlayerName[i] = name; } //刷新界面 Console.Clear(); ProgramManage.GanmeShow(); Console.WriteLine("玩家A名字是{0},玩家B名字{1}", ProgramManage.PlayerName[0], ProgramManage.PlayerName[1]); ProgramManage.DrawMap(); //游戲主循環(huán) //初始化玩家A開始 int player = 0; //兩個玩家都沒在終點的時候不停的玩游戲 while (ProgramManage.PlayerPos[0]<99&&ProgramManage.PlayerPos[1]<99) { //判斷當(dāng)前玩家是否暫停,如果暫停了就停一回合 if (ProgramManage.PlayerPause[player]==false) { Console.WriteLine("輪到玩家{0}敲回車投骰子", ProgramManage.PlayerName[player]); Console.ReadKey(); //玩家player投骰子 ProgramManage.Play(player); ProgramManage.Limit(player); //清空界面 Console.Clear(); //執(zhí)行規(guī)則,如果踩到了關(guān)卡,一直執(zhí)行,直到踩到空白點位置 while (true) { if (ProgramManage.Maps[ProgramManage.PlayerPos[player]] != 0) { ProgramManage.GameRule(player); ProgramManage.Limit(player); } ProgramManage.GameRule(player); break; } ProgramManage.Limit(player); //變化玩家 player = 1 - player; //刷新地圖 ProgramManage.DrawMap(); } else { //當(dāng)前玩家恢復(fù)暫停 ProgramManage.PlayerPause[player] = false; //下一回合讓給另一個玩家 player = 1 - player; continue; } } //游戲結(jié)束 Console.WriteLine("玩家{0}贏了", ProgramManage.Winner()); Console.ReadKey(); } } }
看完上述內(nèi)容,你們掌握使用C#怎么編寫一個飛行棋游戲的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!