溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#如何實(shí)現(xiàn)騎士飛行棋

發(fā)布時(shí)間:2021-10-15 15:59:26 來源:億速云 閱讀:139 作者:柒染 欄目:編程語(yǔ)言

本篇文章為大家展示了C#如何實(shí)現(xiàn)騎士飛行棋,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

飛行棋小游戲是學(xué)習(xí)C#以來,接觸的第一個(gè)游戲項(xiàng)目,根據(jù)小楊老師的思路引導(dǎo),自己的代碼也實(shí)現(xiàn)了功能,經(jīng)過思路的梳理,試著不借助代碼自己去實(shí)現(xiàn)功能,感觸就是不管想的多明白,實(shí)踐起來完全不一樣,所以,還得多多實(shí)踐,培養(yǎng)嚴(yán)謹(jǐn)?shù)倪壿嬎季S。下面看看我梳理的思路~

游戲中界面

飛行棋流程思路

擲骰子流程

游戲運(yùn)行流程

擲骰子代碼

public static void RowShaiZi(int xy){   Random r = new Random();//隨機(jī)數(shù)   int num = r.Next(1, 7);   string str = "";   Console.WriteLine("{0}按任意鍵開始擲骰子", PlayerNames[xy]);   Console.ReadKey(true);//不顯示輸入內(nèi)容,開始擲骰子      Console.WriteLine("{0}擲出了{(lán)1}", PlayerNames[xy], num);   Console.WriteLine("{0}按任意鍵開始行動(dòng)……", PlayerNames[xy]);   Console.ReadKey(true);      PlayerPos[xy] += num;//玩家坐標(biāo)累加   CheckPos();//檢驗(yàn)玩家坐標(biāo)是否超出范圍方法   if (PlayerPos[xy] == PlayerPos[1- xy])//傳進(jìn)來玩家0,1-0是玩家1;傳進(jìn)來是玩家1,1-0是玩家0   {     str = string.Format ("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[xy], PlayerNames[1 - xy], PlayerNames[1 - xy]);     PlayerPos[1- xy] -= 6;     CheckPos();   }   else   {     switch (map[PlayerPos[xy]])     {       case 0: str = "行動(dòng)完畢"; break;       case 1:         str = string.Format("{0}走到了幸運(yùn)輪盤,請(qǐng)選擇1---交換位置,2---轟炸對(duì)方", PlayerNames[xy]);         int number = ReadInt(str, 1, 2);//調(diào)用走到幸運(yùn)輪盤,判斷輸入數(shù)字的方法         if (number == 1)//幸運(yùn)輪盤中輸入1         {           int temp = xy;           temp = PlayerPos[xy];           PlayerPos[xy] = PlayerPos[1- xy];           PlayerPos[1- xy] = temp;           str = string.Format("玩家{0}選擇了與玩家{1}交換位置", PlayerNames[xy], PlayerNames[1- xy]);           Console.WriteLine(str);         }         else//幸運(yùn)輪盤中輸入2         {           PlayerPos[1- xy] = 0;           str = string.Format("玩家{0}選擇了轟炸玩家{1}", PlayerNames[xy], PlayerNames[1- xy]);           Console.WriteLine(str);         }         break;       case 2:         str = "恭喜你,踩到地雷了,退6格";         PlayerPos[xy] -= 6;         CheckPos();         Console.WriteLine(str);         break;       case 3:         str = "踩到暫停了";         Console.WriteLine(str);         flag[xy] = true;         break;       case 4:         str = "恭喜你,幸運(yùn)轉(zhuǎn)盤讓你前進(jìn)10格";         Console.WriteLine(str);         PlayerPos[xy] += 10;         CheckPos();         break;     }   }   Console.ReadKey();   Console.Clear();   DrawMap();

運(yùn)行游戲代碼

static void Main(string[] args){  ShowUI();//游戲頭  IntMap();//初始化地圖  do//輸入玩家A姓名  {    Console.WriteLine("請(qǐng)輸入玩家A姓名");    PlayerNames[0] = Console.ReadLine();    if (PlayerNames[0] =="")    {      Console.Write("玩家A姓名不能為空");    }  }  while (PlayerNames[0] == "");  do//輸入玩家B姓名  {    Console.WriteLine("請(qǐng)輸入玩家B的姓名");    PlayerNames[1] = Console.ReadLine();    if(PlayerNames[1]=="")    {      Console.Write("玩家B姓名不能為空,");    }    if(PlayerNames[0]==PlayerNames[1])    {      Console.Write("玩家B姓名不能與玩家A姓名相同,");    }  } while (PlayerNames[1] == "" || PlayerNames[0] == PlayerNames[1]);  Console.Clear();    ShowUI(); //游戲頭  DrawMap();//畫地圖  Console.ReadKey();  Console.WriteLine("對(duì)戰(zhàn)開始……");  Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);  Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);  while (PlayerPos [0]<=99 && PlayerPos [1]<=99)  {    //玩家1擲骰子    if(flag[0]==false )    {      RowShaiZi(0);    }    else    {      flag[0] = false;    }    //判斷玩家1是否勝利    if(PlayerPos[0]==99)    {      Console.WriteLine("恭喜玩家{0}勝利了?。?!", PlayerNames[0]);      break;    }    //玩家2擲骰子    if(flag[1]==false )    {      RowShaiZi(1);    }    else    {      flag[1] = false;    }    //判斷玩家2是否勝利    if(PlayerPos[1]==99)    {      Console.WriteLine("恭喜玩家{0}勝利了!??!", PlayerNames[1]);      break;    }    Console.WriteLine("行動(dòng)完畢……");  }  Console.ReadKey();}

上述內(nèi)容就是C#如何實(shí)現(xiàn)騎士飛行棋,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI