飞行棋游戏中,用到了一些方法。
方法可以理解成把一个要经常做的操作或者一段要经常写到的代码,写成方法,从而简便了代码的调用和工作效率。
飞行棋中用到了以下几个方法:
showUI方法:用来显示游戏的界面
代码如下:
class UI { /// <summary> /// 用于绘制程序的开始界面 /// </summary> public static void showUI() { Console.WriteLine("********************************************");//输出游戏的欢迎界面 Console.WriteLine("************骑 士 飞 行 棋**************"); Console.WriteLine("********************************************"); }
public static void huitu() { for (int a = 0; a < num.Length; a++) { num[a] = 0; } int[] luckten = { 6, 23, 40, 55, 69, 83 };//幸运轮盘 int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷 int[] pause = { 9, 27, 60, 93 };//暂停 int[] timetunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道 for (int a = 0; a < luckten.Length; a++) { num[luckten[a]] = 1;//使地图上下标为luckten内的值的元素变成幸运轮盘 } for (int a = 0; a < landmine.Length; a++) { num[landmine[a]] = 2;//使地图下标为landmine的元素变成地雷 } for (int a = 0; a < pause.Length; a++) { num[pause[a]] = 3;//使地图下标为pause的元素值的元素变成暂停 } for (int a = 0; a < timetunnel.Length; a++) { num[timetunnel[a]] = 4;//使下标为timetunnle的变成时空隧道 } }
public static void drowmap() {//绘制0-29格的地图 for (int a = 0; a <= 29; a++) { Console.Write(getmappos(a)); } Console.WriteLine(""); //绘制30-34格的地图 for (int a = 30; a <= 34; a++) { for (int j = 0; j < 29; j++) { Console.Write(" "); } Console.WriteLine(getmappos(a)); //绘制35-64的地图 } for (int a = 64; a >= 35; a--) { Console.Write(getmappos(a)); } Console.WriteLine(); for(int a=65;a<=69;a++) { Console.WriteLine(getmappos(a)); } for (int a = 70; a <= 99; a++) { Console.Write(getmappos(a)); }
/// <summary> /// 用于根据数字绘制地图的图案 /// </summary> /// <param name="a">a为输入数组所代表的格数</param> /// <returns></returns> public static string getmappos(int a) { string fuck=""; if (playerpost[0] == a && playerpost[1] == a) { Console.ForegroundColor = ConsoleColor.Yellow; fuck = ("<>"); } else if (playerpost[0] == a) { Console.ForegroundColor = ConsoleColor.Yellow; fuck = ("A"); } else if (playerpost[1] == a) { Console.ForegroundColor = ConsoleColor.Yellow; fuck = ("B"); } else { switch (num[a]) { case 0: Console.ForegroundColor = ConsoleColor.White; fuck = ("□"); break; case 1: Console.ForegroundColor = ConsoleColor.Red; fuck = ("◎"); break; case 2: Console.ForegroundColor = ConsoleColor.Green; fuck = ("★"); break; case 3: Console.ForegroundColor = ConsoleColor.DarkBlue; fuck = ("▲"); break; case 4: Console.ForegroundColor = ConsoleColor.DarkYellow; fuck = ("卍"); break; } } return fuck;
上面的方法用于根据地图数组内元素具体的值,绘制不同图案到地图中。
飞行棋虽然代码不多,但是却将很多知识点融合在一起,可以达到系统复习的效果。