C#繪制飛行棋地圖小程序
1、 初始化地圖,在繪制時可先將地圖進(jìn)行初始化,用數(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 };//幸運(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};//時空隧道 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è)計時所講的分布繪制,在繪制地圖時應(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、繪制地圖,在得到 返回的圖案后,便可進(jìn)行地圖的繪制,這里給出繪制第一行的代碼
/// <summary>
/// 繪制游戲地圖
/// </summary>
static void DrownMap()
{
Console.WriteLine("圖例:幸運(yùn)轉(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#使用AForge實現(xiàn)調(diào)用攝像頭的示例詳解
AForge是一個專門為開發(fā)者和研究者基于C#框架設(shè)計的,這個框架提供了不同的類庫和關(guān)于類庫的資源,本文為大家介紹了C#使用AForge實現(xiàn)調(diào)用攝像頭的相關(guān)教程,需要的可以了解下2023-11-11
C# List<T> Contains<T>()的用法小結(jié)
本篇文章主要是對C#中List<T> Contains<T>()的用法進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01

