C#實(shí)現(xiàn)簡單飛行棋小游戲
本文實(shí)例為大家分享了C#實(shí)現(xiàn)簡單飛行棋小游戲的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo):實(shí)現(xiàn)飛行棋游戲基礎(chǔ)功能
玩家在地圖觸發(fā)道具:
1、獲得道具,可以進(jìn)行一次選擇 1–交換位置 2–讓對方退隨機(jī)格子
2、踩到炸彈,讓對方暫停一回合
3、乘上了飛機(jī),前進(jìn)10格
4、進(jìn)入隧道,將隨機(jī)從其他隧道口出來
using System; namespace FXQGame { class Program { //儲存地圖數(shù)組 static int[] mMaps = new int[120]; //儲存兩個玩家的坐標(biāo) static int[] mPlayerPos = new int[2]; static string[] mPlayerName = { "玩家一","玩家二" }; //下標(biāo)控制當(dāng)前玩家 static int index; static void Main(string[] args) { //游戲頭 展示信息 GameTitle(); Console.WriteLine(mPlayerName[0]); Console.WriteLine(mPlayerName[1]); Console.WriteLine("游戲玩法:"); Console.WriteLine("輸入任意鍵開始游戲"); //按下任意鍵進(jìn)入下一步驟 Console.ReadKey(); //清屏 Console.Clear(); //初始化地圖 InitGameMap(); //繪制地圖 DrawMap(); //進(jìn)入游戲 Update(); //游戲結(jié)束 if (mPlayerPos[0] > mPlayerPos[1]) { Console.WriteLine("{0}獲得勝利",mPlayerName[0]); } else { Console.WriteLine("{0}獲得勝利", mPlayerName[1]); } Console.ReadKey(); } //游戲邏輯 private static void Update() { index = 0; do { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("{0}按任意鍵開始投擲骰子", mPlayerName[index % 2]); Console.ReadKey(true); int ran = GetRanden(1, 7); Console.WriteLine("{0}擲出{1}點(diǎn)", mPlayerName[index % 2], ran); mPlayerPos[index % 2] += ran; Console.ReadKey(true); Console.WriteLine("{0}開始行動", mPlayerName[index % 2]); //玩家A與玩家B接觸 if (mPlayerPos[0] == mPlayerPos[1] && mPlayerPos[0] != 0) { Console.WriteLine("{0}走到了{(lán)1}的背后進(jìn)行攻擊,{2}后退3格", mPlayerName[index % 2], mPlayerName[(index + 1) % 2], mPlayerName[(index + 1) % 2]); if (mPlayerPos[(index + 1) % 2] - 3 < 0) { mPlayerPos[(index + 1) % 2] = 0; } else { mPlayerPos[(index + 1) % 2] -= 3; } Console.ReadKey(true); } else//玩家在地圖觸發(fā)道具 { switch (mMaps[mPlayerPos[index % 2]]) { case 0: Console.WriteLine("{0}踩到方塊,安全", mPlayerName[index % 2]); Console.ReadKey(true); break; case 1: Console.WriteLine("{0}獲得道具,可以進(jìn)行一次選擇 1--交換位置 2--讓對方退隨機(jī)格子", mPlayerName[index % 2]); string input = Console.ReadLine(); while (true) { if (input == "1") { Console.WriteLine("玩家{0}選擇跟玩家{1}交換位置", mPlayerName[index % 2], mPlayerName[(index + 1) % 2]); int temp = mPlayerPos[0]; mPlayerPos[0] = mPlayerPos[1]; mPlayerPos[1] = temp; Console.WriteLine("交換完畢,請按任意鍵進(jìn)行游戲"); Console.ReadKey(true); break; } else if (input == "2") { Console.WriteLine("玩家{0}選擇讓玩家{1}退回隨機(jī)位置", mPlayerName[index % 2], mPlayerName[(index + 1) % 2]); int r = GetRanden(1, 7); if (mPlayerPos[(index + 1) % 2] - r < 0) { mPlayerPos[(index + 1) % 2] = 0; } else { mPlayerPos[(index + 1) % 2] -= r; } Console.WriteLine("玩家{0}本回合得退回{1}位置,按任意鍵進(jìn)行游戲", mPlayerName[(index + 1) % 2], r); Console.ReadKey(true); break; } else { Console.WriteLine("輸入無效字符,請重新輸入"); input = Console.ReadLine(); } } break; case 2: Console.WriteLine("{0}踩到炸彈,讓對方暫停一回合", mPlayerName[index % 2]); index++; Console.ReadKey(true); break; case 3: Console.WriteLine("{0}乘上了飛機(jī),前進(jìn)10格", mPlayerName[index % 2]); mPlayerPos[index % 2] += 10; Console.ReadKey(true); break; case 4: Console.WriteLine("{0}進(jìn)入隧道,將隨機(jī)從其他隧道口出來", mPlayerName[index % 2]); int[] tunnel = { 20, 25, 45, 63, 72, 88, 90 }; mPlayerPos[index % 2] = tunnel[GetRanden(0, 7)]; Console.WriteLine("{0}從其他隧道口出來", mPlayerName[index % 2], mPlayerPos[index % 2]); Console.ReadKey(true); break; } } index++; Console.Clear(); DrawMap(); }while (mPlayerPos[0] < 99 && mPlayerPos[1] < 99); //當(dāng)某一位玩家到達(dá)終點(diǎn)時,結(jié)束游戲 } //得到隨機(jī)數(shù) private static int GetRanden(int min,int max) { Random random = new Random(); return random.Next(min, max); } //游戲頭介紹 private static void GameTitle() { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(DateTime.Now); Console.WriteLine("飛行棋雙人小游戲"); Console.WriteLine("************************************"); } //初始化游戲地圖 private static void InitGameMap() { //玩家道具 在數(shù)組中存儲為1 可以讓敵人退后隨機(jī)距離 也可以交換雙方位置 int[] planeItem = { 6,23,40,55,69,83 }; for(int i = 0; i< planeItem.Length; i++) { mMaps[planeItem[i]] = 1; } //炸彈道具 在數(shù)組中存儲為2 暫停一回合 int[] bombItem = {5,13,17,33,38,50,64,80,94 }; for (int i = 0; i < bombItem.Length; i++) { mMaps[bombItem[i]] = 2; } //飛機(jī)道具 在數(shù)組中存儲為3 前進(jìn)10格 int[] propsItem = {9,27,60,93 }; for (int i = 0; i < propsItem.Length; i++) { mMaps[propsItem[i]] = 3; } //隧道 在數(shù)組中存儲為4 從其他隨機(jī)隧道鉆出 int[] tunnelItem = {20,25,45,63,72,88,90 }; for (int i = 0; i < tunnelItem.Length; i++) { mMaps[tunnelItem[i]] = 4; } } //繪制地圖 private static void DrawMap() { Console.WriteLine("玩家一用A表示,玩家二用B表示"); Console.WriteLine("圖例:□普通方塊 ◇道具 ●炸彈 $飛機(jī) ¤隧道"); //第一行 for (int i = 0; i < 30; i++) { Draw(i); } //第一豎行 Console.WriteLine(); for (int i = 30; i < 35; i++) { for (int j = 0; j <= 28; j++) {//兩個空格 Console.Write(" "); } Draw(i); Console.WriteLine(); } //第二行 for (int i = 64; i >= 35; i--) { Draw(i); } Console.WriteLine(); //第二豎行 for(int i = 65; i <= 69; i++) { Draw(i); Console.WriteLine(); } //第三行 for (int i = 70; i <= 99; i++) { Draw(i); } Console.WriteLine(); } //判斷當(dāng)前點(diǎn)屬于什么特殊屬性 并繪制出來 private static void Draw(int i) { //當(dāng)兩人重合顯示 if (mPlayerPos[0] == mPlayerPos[1] && mPlayerPos[0] == i) { Console.Write("<>"); } else if (mPlayerPos[0] == i) {//玩家一顯示A Console.ForegroundColor = ConsoleColor.Red; Console.Write("A"); } else if (mPlayerPos[1] == i) {//玩家二顯示B Console.ForegroundColor = ConsoleColor.Red; Console.Write("B"); } else { switch (mMaps[i]) { case 0: Console.ForegroundColor = ConsoleColor.Blue; Console.Write("□"); break; case 1: Console.ForegroundColor = ConsoleColor.White; Console.Write("◇"); break; case 2: Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Write("●"); break; case 3: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("$"); break; case 4: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("¤"); break; } } } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)順序隊(duì)列和鏈隊(duì)列的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于C#實(shí)現(xiàn)順序隊(duì)列和鏈隊(duì)列的代碼實(shí)例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10淺談C#在網(wǎng)絡(luò)波動時防重復(fù)提交的方法
這篇文章主要介紹了淺談C#在網(wǎng)絡(luò)波動時防重復(fù)提交的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04C#延時關(guān)閉電腦、取消關(guān)閉電腦操作方法(需管理員權(quán)限)
在C#中,如果想實(shí)現(xiàn)延時關(guān)閉電腦和取消關(guān)閉的功能,可以有多種方法,下面給大家分享C#延時關(guān)閉電腦、取消關(guān)閉電腦操作方法,感興趣的朋友一起看看吧2024-06-06C#實(shí)現(xiàn)抓取和分析網(wǎng)頁類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)抓取和分析網(wǎng)頁類,實(shí)例分析了C#抓取及分析網(wǎng)頁中文本及連接的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-05-05C#獲取路由器外網(wǎng)IP,MAC地址的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#獲取路由器外網(wǎng)IP,MAC地址的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-11-11C#使用Zxing.dll組件解析二維碼的實(shí)現(xiàn)
ZXing是一個開源的,支持多種格式的條形碼圖像處理庫,本文主要介紹了C#使用Zxing.dll組件解析二維碼的實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-09-09C# 實(shí)現(xiàn)的圖片蓋章功能,支持拖拽、旋轉(zhuǎn)、放縮、保存
這篇文章主要介紹了C# 實(shí)現(xiàn)的圖片蓋章功能,支持拖拽、旋轉(zhuǎn)、放縮、保存,需要的朋友可以參考下2014-04-04C#調(diào)用dos窗口獲取相關(guān)信息的方法
這篇文章主要介紹了C#調(diào)用dos窗口獲取相關(guān)信息的方法,涉及C#調(diào)用dos窗口及進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C#使用protobuf-net進(jìn)行序列化的詳細(xì)操作
本文帶領(lǐng)大家學(xué)習(xí)C#中protobuf-net工具的另一種使用體驗(yàn),這個工具的使用體驗(yàn)屬于Code-First模式,先定義類型,并使用注解進(jìn)行標(biāo)記,不需要先編寫.proto文件,感興趣的朋友跟隨小編一起看看吧2021-11-11