黑马程序员_骑士飞行棋学习

Pelagia ·
更新时间:2024-11-11
· 888 次阅读

  在本节的学习中,完成了一个简单的骑士飞行棋的控制台小游戏,游戏的规则是比较简单的,主要是有两个玩家,如果谁先到达终点算是谁胜出。   终实现的效果图如下:

  在本节的学习中主要总结一下练习过程终点难点和重点   1.首先在绘制图形的前期,我们应初始化图形,所以选取了整型数组来存储 各个关卡的位置: //1.幸运轮盘 ◎  //2.地雷 ★   //3.暂停 ▲  //4.时空隧道 ? //5.普通 □   并将关卡在图形数组中的位置对应的值修改为对应的初始化值。

/// <summary> /// 对地图中的关卡初始化 /// </summary> static void InitialMap() { int[] luckyTurn = { 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 i = 0; i < luckyTurn.Length; i++) { int pos = luckyTurn[i]; map[pos] = 1; } for (int i = 0; i < landMine.Length; i++) { int pos = landMine[i]; map[pos] = 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; } }
  其中,全局变量的定义有四个:
static int[] map = new int[100];          //地图数组 static int[] playerPos = new int[2];       //玩家的位置A,B static bool[] isStop = { false, false };  //A B 是否走到了暂停 static string[] players = new string[2];  //玩家的姓名

  2.绘制图形时,我们是一行一行的绘制图形,找到每行的开始编号和结束编号,通过遍历map数组进行绘制,绘制的主要逻辑代码如下:   在绘制过程中,我们应先考虑玩家A或B的位置,然后根据关卡进行输出结果:

 

/// <summary> /// 设置每个位置的图形 /// </summary> /// <param name="i"></param> /// <returns></returns> static string GetMapString(int i) { string result = ""; if (playerPos[0] == i && playerPos[1] == i) { Console.ForegroundColor = ConsoleColor.DarkCyan; result = "<>"; } else if (playerPos[0] == i) { Console.ForegroundColor = ConsoleColor.DarkCyan; result = "A"; } else if (playerPos[1] == i) { Console.ForegroundColor = ConsoleColor.DarkCyan; result = "B"; } else { switch (map[i]) { case 0: Console.ForegroundColor = ConsoleColor.White; result = "□"; break; case 1: Console.ForegroundColor = ConsoleColor.Green; result = "◎"; break; case 2: Console.ForegroundColor = ConsoleColor.Red; result = "★"; break; case 3: Console.ForegroundColor = ConsoleColor.Yellow; result = "▲"; break; case 4: Console.ForegroundColor = ConsoleColor.Magenta; result = "?"; break; } } return result; }

  3.后要考虑的是玩家A和B的行动如何进行代码的实现,   具体的代码实现如下:

/// <summary> /// 执行AB动作 /// </summary> /// <param name="playernumber"></param> static void Action(int playernumber) { #region 玩家掷骰子 Random rand = new Random();   //产生随机数 int step = 0;  //存储临时产生的随机数 string msg = "";   //关卡提示消息 Console.WriteLine("{0}按任意键开始掷骰子", players[playernumber]); ConsoleKeyInfo rec = Console.ReadKey(true); step = rand.Next(1, 7); msg = ""; if (rec.Key == ConsoleKey.Backspace && (rec.Modifiers == ConsoleModifiers.Shift|rec.Modifiers == ConsoleModifiers.Control)) { ConsoleKeyInfo rec2 = Console.ReadKey(true); if (rec2.Key == ConsoleKey.Tab) { step = 30; } } Console.WriteLine("{0}掷出了{1}", players[playernumber], step); Console.WriteLine("按任意键进行行动........"); Console.ReadKey(true); playerPos[playernumber] = playerPos[playernumber] + step; CheckPos(); //玩家A踩到了玩家B,B返回原点 if (playerPos[playernumber] == playerPos[1 - playernumber]) { playerPos[1 - playernumber] = 0; msg = string.Format("{0}踩到了{1},{1}退回原点", players[playernumber], players[1 - playernumber]); } else   //没踩到的时候判断是否有其他的关卡 { switch (map[playerPos[playernumber]]) { case 0: //普通 break; case 1: //幸运轮盘 Console.Clear(); DrawMap(); Console.WriteLine("你走到了幸运轮盘,请选择运气:"); Console.WriteLine("1---交换位置  2---轰炸对方"); int userSelect = ReadInt(1, 2); if (userSelect == 1) { playerPos[playernumber] = playerPos[playernumber] - playerPos[1 - playernumber]; playerPos[1 - playernumber] = playerPos[1 - playernumber] + playerPos[playernumber]; playerPos[playernumber] = playerPos[1 - playernumber] - playerPos[playernumber]; msg = string.Format("{0}选择了交换位置", players[playernumber]); } else { //轰炸 playerPos[1] -= 6; msg = string.Format("{0}轰炸了{1},{1}退6格", players[playernumber], players[1 - playernumber]); } break; case 2: //地雷 playerPos[0] -= 6; msg = string.Format("{0}踩到了地雷,{0}退6格", players[playernumber]); break; case 3: //暂停 isStop[0] = true; msg = string.Format("{0}走到了暂停", players[playernumber]); break; case 4: //时空隧道 playerPos[0] += 10; msg = string.Format("{0}进入时空隧道,爽死了,{0}前进10格", players[playernumber]); break; } } CheckPos(); Console.Clear(); DrawMap(); Console.WriteLine(); if (msg != "") { Console.WriteLine(msg); } Console.WriteLine(" {0}掷出了{1},行动完成", players[playernumber], step); Console.WriteLine("{0}的位置为{1}", players[playernumber], playerPos[playernumber] + 1); Console.WriteLine("{0}的位置为{1}", players[1 - playernumber], playerPos[1 - playernumber] + 1); #endregion }

  4.在行动的代码中,有一个比较重要的方法,为让用户输入某个范围的整数,如果输入格式不正确会要求重新输入:   具体代码如下:

 

/// <summary> /// 输入min 到max 之间的数 /// </summary> /// <param name="min"></param> /// <param name="max"></param> /// <returns></returns> static int ReadInt(int min, int max) { while (true) { try { int number = Convert.ToInt32(Console.ReadLine()); if (number < min || number > max) { Console.WriteLine("只能输入{0}-{1}之间的数字,请重新输入!",min,max); continue; } return number; } catch { Console.WriteLine("请输入数字!!"); } } }
   

 



黑马程序员 飞行棋 程序 学习 程序员

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章