C#繪制飛行棋地圖小程序
1、 初始化地圖,在繪制時可先將地圖進行初始化,用數(shù)組來存儲關(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;
}
//用于存儲關(guān)卡位置
int[] luckyTurn = { 6, 23, 40, 55, 69, 83,98 };//幸運轉(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};//時空隧道 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)的值,在將地圖進行初始化之后,便可開始進行繪制地圖的操作了,地圖繪制可使用 在程序設(shè)計時所講的分布繪制,在繪制地圖時應(yīng)檢驗該該坐標(biāo)點的值,在根據(jù)該點的值繪制相應(yīng)的圖案,在檢查時根據(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="";//用于返回 給一個坐標(biāo)相應(yīng)的圖案
if (playerPos[0] == i && playerPos[1] == i)//判斷是否是對戰(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、繪制地圖,在得到 返回的圖案后,便可進行地圖的繪制,這里給出繪制第一行的代碼
/// <summary>
/// 繪制游戲地圖
/// </summary>
static void DrownMap()
{
Console.WriteLine("圖例:幸運轉(zhuǎn)盤 ○ 地雷 ☆ 暫停 ▲ 時空隧道 卍");
//畫第一行 下標(biāo)0-29 的地圖
for(int i=0;i<30;i++)//循環(huán)坐標(biāo)得到 第一行每個點的圖案
{
Console.Write(GetMapString(i)); //調(diào)用函數(shù)得到每個坐標(biāo)的圖案
}
Console.Write("\n");
Console.ResetColor();//重置前景色
}
以上所述是小編給大家介紹的C#繪制飛行棋地圖小程序,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
C#實現(xiàn)將窗體固定在顯示器的左上角且不能移動的方法
這篇文章主要介紹了C#實現(xiàn)將窗體固定在顯示器的左上角且不能移動的方法,涉及C#窗體固定操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08

