重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
使用C#怎么编写一个飞行棋游戏?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
目前创新互联公司已为近千家的企业提供了网站建设、域名、网络空间、网站改版维护、企业网站设计、澜沧网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。管理类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class ProgramManage { //随机数 public static Random rd = new Random(); //定义地图大小 public static int[] Maps = new int[100]; // public static bool[] PlayerPause = {false,false }; //定义玩家A和玩家B的坐标,0是玩家A的坐标;1是玩家B的坐标 public static int[] PlayerPos = new int[2]; //存储两个玩家的姓名 public static string[] PlayerName = new string[2]; //定义名字错误类型 public static string[] Error = {"正确","不能有数字","重名了","不能为空" }; //画游戏头 public static void GanmeShow() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("*************************"); Console.WriteLine("********飞行棋游戏*******"); Console.WriteLine("*************************"); Console.ForegroundColor = ConsoleColor.Green; } //定义地图关卡布局 public static void InitailMap() { int[] luckturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎ for (int i = 0; i < luckturn.Length; i++) { Maps[luckturn[i]] = 1; } int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷× for (int i = 0; i < landMine.Length; i++) { Maps[landMine[i]] = 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; } } //定义地图元素。输入坐标值,方法内部判断当前坐标应当放什么符号,返回符号值 public static string MapElement(int i) { string Element = "0"; //如果玩家AB坐标相同并都在当前位置则画一个“<>” if (PlayerPos[0] == i && PlayerPos[1] == i) { Element="<>"; } //有A画A,有B画B,都没有画地图 else if (PlayerPos[0] == i) { Element="A"; } else if (PlayerPos[1] == i) { Element="B"; } else { switch (Maps[i]) { case 0: Element = "□"; break; case 1: Element = "◎"; break; case 2: Element = "×"; break; case 3: Element = "▲"; break; case 4: Element = "卍"; break; } } return Element; } //绘制地图 public static void DrawMap() { Console.ForegroundColor = ConsoleColor.White; //第一横行 for (int i = 0; i < 30; i++) { Console.Write(MapElement(i)); } //第一竖行 for (int i = 30; i < 35; i++) { Console.WriteLine(); Console.Write(" "); Console.Write(MapElement(i)); } //第二横行 Console.WriteLine(); for (int i = 64; i > 34; i--) { Console.Write(MapElement(i)); } //第二竖行 for (int i = 65; i < 70; i++) { Console.WriteLine(); Console.Write(MapElement(i)); } //第三横行 Console.WriteLine(); for (int i = 70; i < 100; i++) { Console.Write(MapElement(i)); } Console.WriteLine(); } //判断姓名是否符合要求 public static int NameJudge(string str) { int result = 0; //判断是否为空 if (str == "") { result = 3; return result; } else { result = 0; } //判断是否是字符串 for (int i = 0; i < str.Length; i++) { if (char.IsNumber(str,i)) { result = 1; return result; } else { result = 0; } } //判断是否重名 for (int i = 0; i < PlayerName.Length; i++) { if (PlayerName[i]==str) { result = 2; return result; } else { result = 0; } } return result; } //投骰子 public static void Play(int player) { PlayerPos[player] += rd.Next(1, 7); } //坐标不能出地图 public static void Limit(int player) { if (PlayerPos[player] < 0) { PlayerPos[player] = 0; } else if (PlayerPos[player] > 99) { PlayerPos[player] = 99; } } //执行规则 public static void GameRule(int player) { //踩到对手 if (PlayerPos[player]==PlayerPos[1-player]) { PlayerPos[1 - player] -= 6; Console.WriteLine("{0}踩到对手,对手退6格", PlayerName[player]); } //什么也没踩到 else if (Maps[PlayerPos[player]] == 0) { Console.WriteLine("{0}玩家前进了,什么也没踩到",PlayerName[player]); } //幸运轮盘 else if (Maps[PlayerPos[player]] == 1) { Console.WriteLine("1,与对手交换位置。2,让对手退回原点"); string chose; while (true) { chose = Console.ReadLine(); if (chose == "1") { int tem = PlayerPos[player]; PlayerPos[player] = PlayerPos[1 - player]; PlayerPos[1 - player] = tem; Console.WriteLine("{0}与对手交换了位置", PlayerName[player]); break; } else if (chose == "2") { PlayerPos[1 - player] = 0; Console.WriteLine("{0}把对手打回原位", PlayerName[player]); break; } else { Console.WriteLine("请重新输入"); } } } //地雷 else if (Maps[PlayerPos[player]] == 2) { PlayerPos[player] -= 6; Console.WriteLine("{0}踩到地雷了,倒退6格", PlayerName[player]); } //暂停 else if (Maps[PlayerPos[player]] == 3) { PlayerPause[player] = true; Console.WriteLine("{0}暂停一回合", PlayerName[player]); } //时空隧道 else if (Maps[PlayerPos[player]] == 4) { PlayerPos[player] += 10; Console.WriteLine("{0}前进10格", PlayerName[player]); } } //判断输赢 public static string Winner() { if (PlayerPos[0]>PlayerPos[1]) { return PlayerName[0]; } else { return PlayerName[1]; } } } }
实现类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //初始化标题 ProgramManage.GanmeShow(); //初始化地图关卡 ProgramManage.InitailMap(); //输入名字 string name; for (int i = 0; i < ProgramManage.PlayerName.Length; i++) { Console.WriteLine("请输入玩家{0}的名字", i+1); name = Console.ReadLine(); while (ProgramManage.NameJudge(name)!=0) { Console.WriteLine("名字格式不对,{0},请重新输入", ProgramManage.Error[ProgramManage.NameJudge(name)]); name = Console.ReadLine(); } ProgramManage.PlayerName[i] = name; } //刷新界面 Console.Clear(); ProgramManage.GanmeShow(); Console.WriteLine("玩家A名字是{0},玩家B名字{1}", ProgramManage.PlayerName[0], ProgramManage.PlayerName[1]); ProgramManage.DrawMap(); //游戏主循环 //初始化玩家A开始 int player = 0; //两个玩家都没在终点的时候不停的玩游戏 while (ProgramManage.PlayerPos[0]<99&&ProgramManage.PlayerPos[1]<99) { //判断当前玩家是否暂停,如果暂停了就停一回合 if (ProgramManage.PlayerPause[player]==false) { Console.WriteLine("轮到玩家{0}敲回车投骰子", ProgramManage.PlayerName[player]); Console.ReadKey(); //玩家player投骰子 ProgramManage.Play(player); ProgramManage.Limit(player); //清空界面 Console.Clear(); //执行规则,如果踩到了关卡,一直执行,直到踩到空白点位置 while (true) { if (ProgramManage.Maps[ProgramManage.PlayerPos[player]] != 0) { ProgramManage.GameRule(player); ProgramManage.Limit(player); } ProgramManage.GameRule(player); break; } ProgramManage.Limit(player); //变化玩家 player = 1 - player; //刷新地图 ProgramManage.DrawMap(); } else { //当前玩家恢复暂停 ProgramManage.PlayerPause[player] = false; //下一回合让给另一个玩家 player = 1 - player; continue; } } //游戏结束 Console.WriteLine("玩家{0}赢了", ProgramManage.Winner()); Console.ReadKey(); } } }
看完上述内容,你们掌握使用C#怎么编写一个飞行棋游戏的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联成都网站设计公司行业资讯频道,感谢各位的阅读!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。