骑士飞行棋 C#代码详解
最近看见一个骑士飞行棋的小游戏代码,感觉这个代码中将大多数C#的基础知识都运用到了,是一个新手检验学习成果的有效方法,特此将这个代码整理一遍。这是一个控制台程序。这是代码下载地址,代码中的注释非常详细介绍了每段代码的作用:
http://files.cnblogs.com/files/xiaohua92/%E9%AA%91%E5%A3%AB%E9%A3%9E%E8%A1%8C%E6%A3%8B.zip
首先要了解一下 骑士飞行棋 这个游戏的游戏规则:
1)参与游戏的一共有两个人,暂且称之为A和B.两个人轮流掷筛子,根据掷得的点数在地图上移动
2)地图上有5中元素,分别是无效果:□ 幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卍 。其中当走到□时候没有任何特殊效果,当走到幸运轮盘的时候会让你选择是要跟对手交换位置还是要轰炸对手(轰炸效果为让对手后退6格),当走到地雷的时候会自动倒退6格,当走到暂时的时候下一次暂停掷筛子一次,当走到时空隧道的时候会前进10格
3)如果A踩到了B,既A在B之后到了B所在的位置,此时B将要退回原点
4)为了调试方便,在代码中给游戏加了一个作弊按钮:在掷筛子的时候,按Tab键之后再按F1键的话,会让玩家自主输入想要掷的点数
代码构成:
一共设立了5个类,分别是Program(Main函数所在类)drawMap(用来绘制地图)aseUse(一些必要的基本函数)Initial(初始化姓名以及地图等)match(比赛逻辑) 。其中:
Program类:定义了公共的静态变量以及Main函数
drawMap类:定义了drawMaps()函数(用来绘制地图),getMapString()函数(主要用来在绘制地图的时候,确定当前位置图标)
baseUse类:定义了ShowUI()函数(用来绘制飞行棋的名称),ReadInt(int min, int max)函数(用来读取用户输入的数字), checkPos()函数(用来检查玩家当前位 置是否超出边界)
Initial类:InitialMap()函数(用来初始化地图信息),InitialName()函数(用来记录玩家输入的用户名)
match类:Action(int playerNumber)类(用来玩家掷筛子之后的逻辑判断)
下面附上所有代码 :
Program类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 骑士飞行棋 7 { 8 class Program 9 { 10 //在下面的数组存储我们游戏地图各各关卡 11 //数组的下标为0的元素对应地图上的第1格 下标为1的元素对应元素第2格...下标为n的元素对应n+1格 12 //在数组中 1:表示幸运轮盘 ◎ 13 // 2: 表示地雷 ☆ 14 // 3: 表示暂停 ▲ 15 // 4: 表示时空隧道 卍 16 // 0: 表示普通 □ 17 public static int[] map = new int[100];//地图中一共100个点 18 public static string[] names = new string[2];//names[0]中存储玩家A的姓名,names[1]中存储玩家B的姓名 19 public static int[] playerPos = { 0, 0 };//playerPos[0]中存储玩家A的位置,playerPos[1]中存储玩家B的位置 20 public static int step = 0;//用于存放产生的随机数 21 public static string input = "";//用于存储用户的输入 22 public static string msg = "";//用于存储当用户踩到某个关卡,输出的话 23 public static bool[] isStop = { false, false };//isStop[0]表示玩家A是否上一次走到暂停,isStop[1]表示玩家B是否上一次走到暂停 24 public static Random r = new Random(); 25 26 27 static void Main(string[] args) 28 { 29 baseUse.ShowUI(); 30 Initial.InitialName(); 31 Console.Clear(); 32 baseUse.ShowUI(); 33 Console.WriteLine("对战开始......"); 34 Console.WriteLine("{0}用A来表示", names[0]); 35 Console.WriteLine("{0}用B来表示", names[1]); 36 Console.WriteLine("如果AB在同一位置,用<>表示"); 37 Initial.InitialMap(); 38 drawMap.drawMaps(); 39 Console.WriteLine("开始游戏......"); 40 while (playerPos[0] < 99 && playerPos[1] < 99) 41 { 42 match.Action(0);//玩家A掷筛子 43 if (Program.playerPos[0] < 99)//当玩家没有胜利的时候,玩家B可以继续掷筛子 44 match.Action(1);//玩家B掷筛子 45 } 46 Console.ReadKey(); 47 } 48 } 49 }
drawMap类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 骑士飞行棋 7 { 8 class drawMap 9 { 10 public static void drawMaps() 11 { 12 Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卍 ");//向玩家解释各个图标的含义 13 //绘制第一行 14 for(int i=0;i<30;i++) 15 Console.Write(getMapString(i)); 16 Console.WriteLine(); 17 //绘制右边边第一列 18 for (int i = 30; i < 35; i++) 19 { 20 for (int j = 0; j < 29; j++) 21 Console.Write(" "); 22 Console.Write(getMapString(i)); 23 Console.WriteLine(); 24 } 25 //绘制第二行 26 for (int i = 63; i >= 34; i--) 27 Console.Write(getMapString(i)); 28 Console.WriteLine(); 29 //绘制左边第一列 30 for (int i = 65; i < 69; i++) 31 { 32 33 Console.Write(getMapString(i)); 34 Console.WriteLine(); 35 } 36 //绘制第三行 37 for (int i = 69; i < 100; i++) 38 Console.Write(getMapString(i)); 39 Console.WriteLine(); 40 } 41 42 static string getMapString(int pos) 43 { 44 string result = ""; 45 if (Program.playerPos[0] == pos&&Program.playerPos[1]==pos) 46 { 47 Console.ForegroundColor = ConsoleColor.Yellow; 48 result = "<>"; 49 } 50 else if (Program.playerPos[0] == pos) 51 { 52 Console.ForegroundColor = ConsoleColor.Yellow; 53 result = "A"; 54 } 55 else if (Program.playerPos[1] == pos) 56 { 57 Console.ForegroundColor = ConsoleColor.Yellow; 58 result = "B"; 59 } 60 else 61 { 62 switch (Program.map[pos]) 63 { 64 case 0: 65 Console.ForegroundColor = ConsoleColor.White;//设置控制台当前位置的颜色为白色 66 result = "□"; 67 break; 68 case 1: 69 Console.ForegroundColor = ConsoleColor.Red; 70 result = "◎"; 71 break; 72 case 2: 73 Console.ForegroundColor = ConsoleColor.Green; 74 result = "☆"; 75 break; 76 case 3: 77 Console.ForegroundColor = ConsoleColor.Blue; 78 result = "▲"; 79 break; 80 case 4: 81 Console.ForegroundColor = ConsoleColor.DarkBlue; 82 result = "卍"; 83 break; 84 } 85 } 86 return result; 87 } 88 } 89 }