C#繪制飛行棋地圖小程序
1、 初始化地圖,在繪制時(shí)可先將地圖進(jìn)行初始化,用數(shù)組來存儲(chǔ)關(guān)卡的位置,然后利用循環(huán)給地圖中 關(guān)卡所在處賦予代表關(guān)卡的值。
關(guān)鍵代碼如下
/// <summary> /// 初始化游戲地圖 /// </summary> static void InitialMap() { for (int i=0;i<Map.Length;i++) { Map[i] =0; } //用于存儲(chǔ)關(guān)卡位置 int[] luckyTurn = { 6, 23, 40, 55, 69, 83,98 };//幸運(yùn)轉(zhuǎn)盤 1 int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷 2 int[] pause = { 9, 27, 60, 93 };//暫停 3 int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90};//時(shí)空隧道 4 for (int i=0;i<luckyTurn.Length;i++) { int pos = luckyTurn[i]; Map[pos] = 1; } for (int i=0;i<landMine.Length;i++) { Map[landMine[i]] = 2; } for (int i=0;i<pause.Length;i++) { int pos = pause[i]; Map[pos] = 3; } for(int i=0;i<timeTunnel.Length;i++) { int pos = timeTunnel[i]; Map[pos] =4; } }
2、檢查坐標(biāo)的值,在將地圖進(jìn)行初始化之后,便可開始進(jìn)行繪制地圖的操作了,地圖繪制可使用 在程序設(shè)計(jì)時(shí)所講的分布繪制,在繪制地圖時(shí)應(yīng)檢驗(yàn)該該坐標(biāo)點(diǎn)的值,在根據(jù)該點(diǎn)的值繪制相應(yīng)的圖案,在檢查時(shí)根據(jù)值 返回相應(yīng)的圖案 ,在利用循環(huán)繪制出即可,檢查坐標(biāo)的值代碼如下:
/// <summary> /// 獲得要繪制的坐標(biāo) /// </summary> /// <param name="i"> 要繪制的坐標(biāo)</param> /// <returns></returns> static string GetMapString(int i) { string Result="";//用于返回 給一個(gè)坐標(biāo)相應(yīng)的圖案 if (playerPos[0] == i && playerPos[1] == i)//判斷是否是對(duì)戰(zhàn)雙方所在此處 { Console.ForegroundColor = ConsoleColor.Yellow;//設(shè)置圖案的前景色為黃色 Result = "<>";//得到兩人均在圖案 } else if (playerPos[0] == i) { Console.ForegroundColor = ConsoleColor.Yellow; Result = "A";//得到A均在圖案 } else if (playerPos[1] == i) { Console.ForegroundColor = ConsoleColor.Yellow; Result = "B";//得到B均在圖案 } else { switch (Map[i]) { case 0: Console.ForegroundColor = ConsoleColor.White; Result = "□";//得到普通均在圖案 break; case 1: Console.ForegroundColor = ConsoleColor.Red; Result = "○";//得轉(zhuǎn)盤圖案 break; case 2: Console.ForegroundColor = ConsoleColor.Blue; Result = "☆"; break; case 3: Console.ForegroundColor = ConsoleColor.Green; Result = "▲"; break; case 4: Console.ForegroundColor = ConsoleColor.DarkBlue; Result = "卍"; break; } } return Result; //返回圖案 }
3、繪制地圖,在得到 返回的圖案后,便可進(jìn)行地圖的繪制,這里給出繪制第一行的代碼
/// <summary> /// 繪制游戲地圖 /// </summary> static void DrownMap() { Console.WriteLine("圖例:幸運(yùn)轉(zhuǎn)盤 ○ 地雷 ☆ 暫停 ▲ 時(shí)空隧道 卍"); //畫第一行 下標(biāo)0-29 的地圖 for(int i=0;i<30;i++)//循環(huán)坐標(biāo)得到 第一行每個(gè)點(diǎn)的圖案 { Console.Write(GetMapString(i)); //調(diào)用函數(shù)得到每個(gè)坐標(biāo)的圖案 } Console.Write("\n"); Console.ResetColor();//重置前景色 }
以上所述是小編給大家介紹的C#繪制飛行棋地圖小程序,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C#使用CefSharp自定義緩存實(shí)現(xiàn)
本文介紹了如何使用C#和CefSharp自定義緩存實(shí)現(xiàn)減少Web應(yīng)用程序的網(wǎng)絡(luò)請(qǐng)求,提高應(yīng)用程序性能。首先,本文講解了CefSharp的基本知識(shí)和使用方法。然后,詳細(xì)闡述了在CefSharp中實(shí)現(xiàn)自定義緩存的步驟和技巧。最后,通過實(shí)例演示了如何使用自定義緩存功能獲取并展示網(wǎng)頁數(shù)據(jù)2023-04-04C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的示例詳解
AForge是一個(gè)專門為開發(fā)者和研究者基于C#框架設(shè)計(jì)的,這個(gè)框架提供了不同的類庫和關(guān)于類庫的資源,本文為大家介紹了C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的相關(guān)教程,需要的可以了解下2023-11-11C#中單問號(hào)(?)和雙問號(hào)(??)的用法整理
本文詳細(xì)講解了C#中單問號(hào)(?)和雙問號(hào)(??)的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C# List<T> Contains<T>()的用法小結(jié)
本篇文章主要是對(duì)C#中List<T> Contains<T>()的用法進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01