实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _10飞行棋 { class Program { //我们用静态字段来模拟全局变量 static int[] Maps = new int[100]; //声明一个静态数组用来存储玩家A和玩家B的坐标 static int[] PlayPos = new int[2]; // 两个玩家 //存储两个玩家的姓名 static string[] PlayerNames = new string[2]; static bool[] Flags = new bool[2];//Flags[0]默认是false,Flages[1]默认也是false static void Main(string[] args) { GameShow(); #region 输入玩家姓名 Console.WriteLine("请输入玩家A的姓名?"); PlayerNames[0] = Console.ReadLine(); while (PlayerNames[0] == "") { Console.WriteLine("玩家A的姓名不能为空,请重新输入……"); PlayerNames[0] = Console.ReadLine(); } Console.WriteLine("请输入玩家B的姓名?"); PlayerNames[1] = Console.ReadLine(); while (PlayerNames[1] == ""||PlayerNames[1]==PlayerNames[0]) { if (PlayerNames[0] == PlayerNames[1]) { Console.WriteLine("玩家B与玩家A的姓名不能重复,请重新输入玩家B的姓名?"); PlayerNames[1] = Console.ReadLine(); } if (PlayerNames[1] == "") { Console.WriteLine("玩家B的姓名不能为空,请重新输入?"); PlayerNames[1] = Console.ReadLine(); } } #endregion //当玩家姓名输入完毕以后我们首先要做的事情应该是清屏,然后将游戏头重新调用一遍,实现局部刷新的效果 Console.Clear();//清屏 GameShow(); Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]); Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]); InitailMap(); //注意 一定要先初始化地图,如果不初始化地图,那么将全部都是框框…… DrawMap(); //当玩家A和玩家B没有一个人在终点的时候,两个玩家不停的去玩游戏 while (PlayPos[0] < 99 && PlayPos[1] < 99) { if (Flags[0] == false) { PlayGame(0); } else { Flags[0] = false; } if(PlayPos[0]>=99) { Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[0], PlayerNames[1]); break; } if (Flags[1] == false) { PlayGame(1); } else { Flags[1] = false; } if (PlayPos[1] >= 99) { Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[1], PlayerNames[0]); break; } //PlayGame(1); }//while Win(); Console.ReadKey(); } /// <summary> /// 胜利 /// </summary> public static void Win() { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(" ◆ "); Console.WriteLine(" ■ ◆ ■ ■"); Console.WriteLine(" ■■■■ ■ ■ ◆■ ■ ■ ■"); Console.WriteLine(" ■ ■ ■ ■ ◆ ■ ■ ■ ■"); Console.WriteLine(" ■ ■ ■■■■■■ ■■■■■■■ ■ ■ ■"); Console.WriteLine(" ■■■■ ■ ■ ●■● ■ ■ ■"); Console.WriteLine(" ■ ■ ■ ● ■ ● ■ ■ ■"); Console.WriteLine(" ■ ■ ■■■■■■ ● ■ ● ■ ■ ■"); Console.WriteLine(" ■■■■ ■ ● ■ ■ ■ ■ ■"); Console.WriteLine(" ■ ■ ■ ■ ■ ■ ■ ■"); Console.WriteLine(" ■ ■ ■ ■ ■ ■ "); Console.WriteLine(" ■ ■ ■ ■ ● ■ "); Console.WriteLine(" ■ ■■ ■■■■■■ ■ ● ●"); Console.ResetColor(); } /// <summary> /// 游戏头 /// </summary> public static void GameShow() { #region 游戏头 Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("*******孤行良版飞行棋*****"); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("**************************"); #endregion } /// <summary> /// 初始化地图 /// </summary> public static void InitailMap() { int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎ for (int i = 0; i < luckyturn.Length; i ) { //int index = luckyturn[i]; //Maps[index] = 1; //上面两句的简写 Maps[luckyturn[i]] = 1; } int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆ for (int i = 0; i < landMine.Length; i ) { int index=landMine[i]; Maps[index] = 2; } int[] pause = { 9, 27, 60, 93 };//暂停▲ for (int i = 0; i < pause.Length; i ) { Maps[pause[i]] = 3; } int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道§ for (int i = 0; i < timeTunnel.Length; i ) { Maps[timeTunnel[i]] = 4; } } /// <summary> /// 画地图 /// </summary> public static void DrawMap() { Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐"); #region 第一横行 for (int i = 0; i < 30; i ) { Console.Write(DrawStringMap(i)); }//for #endregion #region 第一竖行 //画完第一横行后 应该换行 Console.WriteLine(); for (int i = 30; i < 35; i ) { for (int j = 0; j < 29; j ) { Console.Write(" "); } Console.Write(DrawStringMap(i)); Console.WriteLine(); } #endregion #region 第二横行 for (int i = 64; i >= 35; i--) { Console.Write(DrawStringMap(i)); } //画完第二横行应该换行 Console.WriteLine(); #endregion #region 第二竖行 for (int i = 65; i <= 69; i ) { Console.WriteLine(DrawStringMap(i)); } #endregion #region 第三横行 for (int i = 70; i <= 99; i ) { Console.Write(DrawStringMap(i)); } #endregion //画完最后一行应该换行 Console.WriteLine(); } //DrawMap方法的结尾 /// <summary> /// 从画地图的方法中抽象出来的一个方法 /// </summary> /// <param name="i"></param> /// <returns></returns> public static string DrawStringMap(int i) { string str = ""; #region 画图 //如果玩家A跟玩家B的坐标相同,并且都在这个地图上,画一个尖括号 if (PlayPos[0] == PlayPos[1] && PlayPos[0] == i) { str = "<>"; } else if (PlayPos[0] == i) { str = "A"; } else if (PlayPos[1] == i) { str = "B"; } else { switch (Maps[i]) { case 0: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("□"); //框框 break; case 1: Console.ForegroundColor = ConsoleColor.Green; Console.Write("◎"); //幸运轮盘 break; case 2: Console.ForegroundColor = ConsoleColor.Red; Console.Write("☆"); // 地雷 break; case 3: Console.ForegroundColor = ConsoleColor.Blue; Console.Write("▲"); //暂停 break; case 4: Console.ForegroundColor = ConsoleColor.White; Console.Write("卐"); //时空隧道 break; }//switch }//else_2 return str; #endregion } /// <summary> /// 玩游戏 /// </summary> /// <param name="playerNumber"></param> public static void PlayGame(int playerNumber) { Random r = new Random(); int rNumber = r.Next(1, 7); Console.WriteLine("{0}按任意键执骰子",PlayerNames[playerNumber]); Console.ReadKey(true); Console.WriteLine("{0}掷出了{1}", PlayerNames[playerNumber],rNumber); PlayPos[playerNumber] = rNumber; ChangPos(); Console.ReadKey(true); Console.WriteLine("{0}行动完了", PlayerNames[playerNumber]); Console.ReadKey(true); //玩家A有可能踩到玩家B 方块 幸运轮盘 地雷 时空隧道 if (PlayPos[playerNumber] == PlayPos[1 - playerNumber]) { Console.WriteLine("玩家{0}踩到玩家{1},玩家{2}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[1 - playerNumber]); PlayPos[1 - playerNumber] -= 6; ChangPos(); Console.ReadKey(true); } else//踩到了关卡 { //玩家坐标 switch (Maps[PlayPos[playerNumber]]) //0 1 2 3 4 { case 0: Console.WriteLine("玩家{0}踩到了方块,安全", PlayerNames[playerNumber]); Console.ReadKey(true); break; case 1: Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择1--交换位置 2--轰炸对方", PlayerNames[playerNumber]); string input = Console.ReadLine(); while (true) { if (input == "1") { Console.WriteLine("玩家{0}选择跟玩家{1}交换位置", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]); Console.ReadKey(true); int temp = PlayPos[playerNumber]; PlayPos[playerNumber] = PlayPos[1 - playerNumber]; PlayPos[1 - playerNumber] = temp; Console.WriteLine("交换完成!!!按任意键游戏继续……"); Console.ReadKey(true); break; } else if (input == "2") { Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[1 - playerNumber]); Console.ReadKey(true); PlayPos[1 - playerNumber] -= 6; ChangPos(); Console.WriteLine("玩家{0}退格成功", PlayerNames[1 - playerNumber]); Console.ReadKey(true); break; } else { Console.WriteLine("只能输入1或者2 1--交换位置 2--轰炸对方"); input = Console.ReadLine(); Console.ReadKey(true); } } break; case 2: Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerNames[playerNumber]); Console.ReadKey(true); PlayPos[playerNumber] -= 6; ChangPos(); Console.WriteLine("玩家{0}退格成功 ", PlayerNames[playerNumber]); break; case 3: Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[playerNumber]); Flags[playerNumber] = true; Console.ReadKey(true); break; case 4: Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[playerNumber]); PlayPos[playerNumber] = 10; ChangPos(); Console.ReadKey(true); break; }//swich }//else ChangPos();//perfect Console.Clear(); DrawMap(); } /// <summary> /// 限制位置,当玩家坐标发生改变的时候调用 /// </summary> public static void ChangPos() { if(PlayPos[0]<0) { PlayPos[0] = 0; } if (PlayPos[0] > 99) { PlayPos[0] = 99; } if (PlayPos[1] < 0) { PlayPos[1] = 0; } if (PlayPos[1] > 99) { PlayPos[1] = 99; } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论