[C#]遊戲地圖繪製——多玩家版

繪製100格遊戲地圖(根據“傳智播客”.NET視頻:飛行棋項目 製作)  

這個是多個玩家的版本,增加了圖標顏色,增加圖例說明。

  1. using System; 
  2.  
  3. namespace GameMap 
  4.     internal class Program 
  5.     { 
  6.         //數組的下標爲0的元素對應地圖上的第1格,下標爲1的元素對應第2格…下標爲n的元素對應第n+1格 
  7.  
  8.         //定義一個數組存放地圖的格數 
  9.         private static int[] Map = new int[100]; 
  10.  
  11.         //定義一個數組存放玩家的座標,playerPos[0] 存玩家A的座標,playerPos[1] 存玩家B的座標 
  12.         private static int[] playerPos = { 1, 1, 0, 5, }; 
  13.  
  14.         //定義一個數組存放玩家的標識字符(不能小於玩家人數) 
  15.         private static string[] playerIco = { "A""B""C""D" }; 
  16.  
  17.         private static void Main(string[] args) 
  18.         { 
  19.             //圖例及玩家說明文字 
  20.             Console.WriteLine("圖例說明:幸運輪盤—◎   地雷—★   暫停—▲   時空隧道—※   普通—□"); 
  21.             Console.Write("玩家說明:多名玩家—<>  "); 
  22.             for (int i = 0; i < playerPos.Length; i++) 
  23.             { 
  24.                 Console.Write("第{0}位玩家:{1}  ", i + 1, playerIco[i]); 
  25.             } 
  26.  
  27.             //初始化地圖數據 
  28.             InitialMap(); 
  29.  
  30.             //繪製地圖 
  31.             DrawMap(); 
  32.  
  33.             Console.ReadKey(); 
  34.         } 
  35.  
  36.         /// <summary> 
  37.         /// 設置地圖關卡的位置 
  38.         /// </summary> 
  39.         private static void InitialMap() 
  40.         { 
  41.             //在下面的數組存儲我們遊戲地圖各個關卡 
  42.  
  43.             //在數組中用以下數字表示相關圖標 
  44.             //1:幸運輪盤—◎ 
  45.             //2:地雷—★ 
  46.             //3:暫停—▲ 
  47.             //4:時空隧道—※ 
  48.             //0:普通—□ 
  49.  
  50.             //定義相關關卡的位置 
  51.             int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };      //幸運輪盤1 
  52.             int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };      //地雷2 
  53.             int[] pause = { 9, 27, 60, 93 };        //暫停3 
  54.             int[] timeTunel = { 20, 25, 45, 63, 72, 88, 90 };      //時空隧道4 
  55.  
  56.             for (int i = 0; i < luckyTurn.Length; i++) 
  57.             { 
  58.                 Map[luckyTurn[i]] = 1;  //把地圖Map的第luckyTurn[i]格設置爲1(幸運輪盤) 
  59.             } 
  60.  
  61.             for (int i = 0; i < landMine.Length; i++) 
  62.             { 
  63.                 Map[landMine[i]] = 2;   //把地圖Map的第landMine[i]格設置爲2(地雷) 
  64.             } 
  65.  
  66.             for (int i = 0; i < pause.Length; i++)  //把地圖Map的第pause[i]格設置爲3(暫停) 
  67.             { 
  68.                 Map[pause[i]] = 3; 
  69.             } 
  70.  
  71.             for (int i = 0; i < timeTunel.Length; i++)  //把地圖Map的第timeTunel[i]格設置爲4(時空隧道) 
  72.             { 
  73.                 Map[timeTunel[i]] = 4; 
  74.             } 
  75.         } 
  76.  
  77.         /// <summary> 
  78.         /// 判斷當前格應該繪製的圖標 
  79.         /// </summary> 
  80.         /// <param name="pos">當前格位置</param> 
  81.         /// <returns>當前格圖標</returns> 
  82.         private static string MapIco(int pos) 
  83.         { 
  84.             string ico = "";     //存放要繪製的圖標字符 
  85.  
  86.             //判斷是否有兩個以上的玩家處於當前格上 
  87.             int j = 0; 
  88.             for (int i = 0; i < playerPos.Length; i++) 
  89.             { 
  90.                 if (playerPos[i] == pos)     //判斷玩家是否在當前格上 
  91.                 { 
  92.                     j++; 
  93.                     if (j >= 2)     //有兩個以上玩家在同一個格上 
  94.                     { 
  95.                         Console.ForegroundColor = ConsoleColor.Yellow;  //設置圖標顏色 
  96.                         ico = ("<>"); 
  97.                         return ico; 
  98.                     } 
  99.                 } 
  100.             } 
  101.  
  102.             //判斷當前格上是否有某一位玩家存在 
  103.             for (int i = 0; i < playerPos.Length; i++) 
  104.             { 
  105.                 if (playerPos[i] == pos)    //當前格有玩家playerPos[i]存在 
  106.                 { 
  107.                     Console.ForegroundColor = ConsoleColor.Yellow;  //設置圖標顏色 
  108.                     ico = playerIco[i];     //獲得該玩家的標識字符 
  109.                     return ico; 
  110.                 } 
  111.             } 
  112.  
  113.             switch (Map[pos])     //根據當前格的值來顯示相應的圖標 
  114.             { 
  115.                 case 1: 
  116.                     Console.ForegroundColor = ConsoleColor.Magenta; //設置圖標顏色 
  117.                     ico = ("◎");     //1:幸運輪盤—◎ 
  118.  
  119.                     break
  120.  
  121.                 case 2: 
  122.                     Console.ForegroundColor = ConsoleColor.Red;    //設置圖標顏色 
  123.                     ico = ("★");     //2:地雷—★ 
  124.  
  125.                     break
  126.  
  127.                 case 3: 
  128.                     Console.ForegroundColor = ConsoleColor.Cyan;    //設置圖標顏色 
  129.                     ico = ("▲");     //3:暫停—▲ 
  130.  
  131.                     break
  132.  
  133.                 case 4: 
  134.                     Console.ForegroundColor = ConsoleColor.Green;   //設置圖標顏色 
  135.                     ico = ("※");     //4:時空隧道—※ 
  136.  
  137.                     break
  138.  
  139.                 default
  140.                     Console.ForegroundColor = ConsoleColor.White;   //設置圖標顏色 
  141.                     ico = ("□");     //0:普通—□ 
  142.                     break
  143.             } 
  144.             return ico; 
  145.         } 
  146.  
  147.         /// <summary> 
  148.         /// 繪製地圖 
  149.         /// </summary> 
  150.         private static void DrawMap() 
  151.         { 
  152.             Console.WriteLine();    //新起一行 
  153.  
  154.             //畫第一行 
  155.             for (int i = 0; i <= 29; i++) 
  156.             { 
  157.                 Console.Write(MapIco(i));       //繪製當前格的圖標 
  158.             } 
  159.  
  160.             Console.WriteLine();    //第一行結束,換行 
  161.  
  162.             //畫第右邊列(包含5行,每行前29格爲空字符) 
  163.             for (int i = 30; i <= 34; i++)      //循環繪製5行 
  164.             { 
  165.                 for (int j = 0; j < 29; j++)    //有圖標的字符串需要繪製在第30格,因此需要每行前29格繪製兩個空字符串 
  166.                 { 
  167.                     Console.Write("  "); 
  168.                 } 
  169.                 Console.WriteLine(MapIco(i)); ;      //繪製當前格的圖標,並換行 
  170.             } 
  171.  
  172.             //畫第二行 
  173.             for (int i = 64; i >= 35; i--) 
  174.             { 
  175.                 Console.Write(MapIco(i));     //繪製當前格的圖標 
  176.             } 
  177.  
  178.             Console.WriteLine();    //第二行結束,換行 
  179.  
  180.             //畫第左邊列 
  181.             for (int i = 65; i <= 69; i++) 
  182.             { 
  183.                 Console.WriteLine(MapIco(i));      //繪製當前格的圖標,並換行 
  184.             } 
  185.  
  186.             //畫第三行 
  187.             for (int i = 70; i <= 99; i++) 
  188.             { 
  189.                 Console.Write(MapIco(i));       //繪製當前格的圖標 
  190.             } 
  191.  
  192.             Console.WriteLine(); 
  193.  
  194.             Console.ResetColor();   //重置控制檯的前景色爲默認 
  195.         } 
  196.     } 

效果圖:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章