第10天c#实现飞行棋
作者:互联网
代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace week2 { class Program { /// <summary> /// 坐标轴 /// </summary> struct Pos { public int X; //x轴坐标 public int Y; //y轴坐标 } /// <summary> /// 事件类型 /// </summary> enum MapType { none_, //无事件 mine_, //地雷 退6格 spatio_, //时空隧道 进10格 debar_, //暂停1回合 swap_ //幸运转盘 p1和pc交换位置 } /// <summary> /// 地图元素 /// </summary> struct Map { public Pos pos; //方格所在位置的坐标 public string icon; //绘制方格的图标 方块□ 地雷● 时空隧道卍 暂停▲ 幸运转盘◎ public MapType mapType; //事件类型 } /// <summary> /// 玩家 /// </summary> /// <param name="args"></param> struct Player { public Pos pos; //玩家坐标 public string icon; //玩家的图标 ★ public int index; //玩家所在的地图元素的位置 public bool isDebar; //玩家是否暂停 true表示暂停 false表示否 public int id; //玩家编号 } /// <summary> /// 围墙 /// </summary> struct Wall { public Pos pos; //围墙坐标 public string icon; //围墙图标 # } /// <summary> /// 创建围墙 /// </summary> /// <param name="walls"></param> static void CreatWall(Wall[,] walls) { Console.ForegroundColor = ConsoleColor.Green; //给围墙设置绿色 for (int i = 0; i < walls.GetLength(0); i++) { for (int j = 0; j < walls.GetLength(1); j++) { if (i == 0||i==walls.GetLength(0)-1) //第0行和最后1行设置围墙 { Console.Write("#"); } else if(j==0||j==walls.GetLength(1)-1 || j==walls.GetLength(1)-30) //第0列、最后1列、47列设置围墙 { Console.Write("#"); } else if(i==walls.GetLength(0)/2-1 && j>walls.GetLength(1)-30) //右侧中间隔开 { Console.Write("#"); } else { Console.Write(" "); } } Console.WriteLine(); } Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("图案说明:无事件□ 地雷● 时空隧道卍 暂停▲ 幸运转盘◎"); Console.ForegroundColor = ConsoleColor.Gray; //创建好围墙后颜色设为默认 } /// <summary> /// 创建地图元素 /// </summary> /// <param name="maps"></param> static void CreatMap(Map[] maps) { Pos startPos; //设置起点坐标 startPos.X = 2; //起点x周为2 startPos.Y = 2; //起点y轴为2 maps[0].pos = startPos; //起点坐标赋值给地图第一个元素坐标 for (int i = 1; i < 19; i++) //创建第一条路元素 x轴 { maps[i].pos.X = maps[i - 1].pos.X + 1; maps[i].pos.Y = maps[i - 1].pos.Y; maps[i].icon = "□"; maps[i].mapType = MapType.none_; } for (int i = 19; i < 19*2; i++) //创建第二条路,y轴 { maps[i].pos.X = maps[i - 1].pos.X; maps[i].pos.Y = maps[i - 1].pos.Y+1; maps[i].icon = "□"; maps[i].mapType = MapType.none_; } for (int i = 19*2; i < 19*3-1; i++) //创建第3条路,x轴反方向 { maps[i].pos.X = maps[i - 1].pos.X-1; maps[i].pos.Y = maps[i - 1].pos.Y; maps[i].icon = "□"; maps[i].mapType = MapType.none_; } for (int i = 19*3-1; i < maps.Length; i++) //创建第4条路,y轴反方向 { maps[i].pos.X = maps[i - 1].pos.X; maps[i].pos.Y = maps[i - 1].pos.Y-1; maps[i].icon = "□"; maps[i].mapType = MapType.none_; } } /// <summary> /// 绘制地图 /// </summary> /// <param name="args"></param> static void DropMap(Map[] maps) { for (int i = 0; i < maps.Length; i++) { maps[i].icon = GetIcon(maps[i]); //获取地图元素的图标 Console.SetCursorPosition(maps[i].pos.X * 2, maps[i].pos.Y); //设置光标位置 Console.Write(maps[i].icon); //绘制地图元素 } Console.SetCursorPosition(40, 23); //光标位置移至下方 } /// <summary> /// 获取图形 /// </summary> /// <param name="map"></param> static string GetIcon(Map map) { switch (map.mapType) //根据地图元素的类型,返回不同的元素图标 { case MapType.none_: map.icon = "□"; break; case MapType.mine_: map.icon = "●"; break; case MapType.spatio_: map.icon = "卍"; break; case MapType.debar_: map.icon = "▲"; break; case MapType.swap_: map.icon = "◎"; break; default: break; } return map.icon; } /// <summary> /// 设置随机事件 /// </summary> /// <param name="args"></param> static void MapEvent(Map[] maps, Random r) { //设置地图元素类型 for (int i = 0; i < maps.Length; i++) { int rn = r.Next(0, 101); if (rn > 90) { maps[i].mapType = MapType.mine_; //地雷概率10% } else if (rn > 85) { maps[i].mapType = MapType.spatio_; //时空隧道 5% } else if (rn > 80) { maps[i].mapType = MapType.swap_; //幸运转盘5% } else if (rn > 70) { maps[i].mapType = MapType.debar_; //暂停10% } else { maps[i].mapType = MapType.none_; } } } /// <summary> /// 创建角色 /// </summary> /// <param name="players"></param> /// <param name="map"></param> static void CreatPlayer(Player[] players, Map[] map) { for (int i = 0; i < players.Length; i++) { players[i].pos = map[0].pos; //定义玩家起始位置坐标为地图元素第0个坐标 players[i].id = i+1; //定义玩家编号 players[i].icon = "★"; //玩家图标 players[i].isDebar = false; //是否暂停 false为否 true为暂停 players[i].index = 0; //玩家当前所在位置 } } /// <summary> /// 绘制角色 /// </summary> /// <param name="args"></param> static void DropPlayer(Player[] players, Map[] maps) { for (int i = 0; i < players.Length; i++) { Console.SetCursorPosition(maps[players[i].index].pos.X * 2, maps[players[i].index].pos.Y); switch(players[i].id) { case 1: Console.ForegroundColor = ConsoleColor.Yellow; //第一个玩家颜色 Console.Write(players[i].icon); //绘制玩家图标 Console.ForegroundColor = ConsoleColor.Gray; //清除颜色 break; case 2: Console.ForegroundColor = ConsoleColor.Red; //第2个玩家颜色 Console.Write(players[i].icon); //绘制第2个玩家图标 Console.ForegroundColor = ConsoleColor.Gray; //清除颜色 break; } } Console.SetCursorPosition(40, 22); } /// <summary> /// 角色移动 /// </summary> /// <param name="args"></param> static void MovePlayer(Map[] maps , ref Player player,int num) { player.index += num; //玩家索引位置往前移动几个位置 //判断界限 if(player.index>=maps.Length-1) { player.index = maps.Length - 1; //当玩家所以位置超过地图元素的最后一个位置,则定义玩家索引位置为元素的末尾 } else if(player.index<=0) { player.index = 0; //当玩家所以位置小于地图元素的第一个位置,则定义玩家索引位置为地图元素的起始 } else { player.index = player.index; } } /// <summary> /// 交换位置 /// </summary> /// <param name="maps"></param> /// <param name="players"></param> /// <param name="player"></param> static void SwapPos(Map[] maps, Player[] players, ref Player player) { for (int i = 0; i < players.Length; i++) { if (player.id == players[i].id) { #region 交换位置提示信息 Console.SetCursorPosition(50, 5); //提示信息 Console.WriteLine("{0}踩中了幸运轮盘,", player.id); int swapId = i == 0 ? players[i + 1].id : players[i - 1].id; //判断玩家id Console.SetCursorPosition(50, 5); Console.WriteLine("和玩家{0}交换了位置", swapId); #endregion int tmp = player.index; //定义暂存变量接收当前玩家的索引 player.index = i == 0 ? players[i + 1].index : players[i - 1].index; //判断当前玩家是不是玩家数组的第一个便利出来的, if (i == 0) //如果是,则跟后一个玩家交换,如果不是,则跟前一个玩家交换 { players[i + 1].index = tmp; } else { players[i - 1].index = tmp; } } } DropMap(maps); //重置地图 DropPlayer(players, maps); //重置玩家 Console.WriteLine("\a"); //提示交换音 } static void MoveDrop(Map[] maps, Player[] players, ref Player player, int num) { if (num >= 0) { if (player.isDebar == false) { num = player.index + num - maps.Length + 1 >= 0 ? maps.Length -1-player.index : num; //判断最后一步,如果骰子点数大于所剩格子,则有几个格子走几步 for (int j = 0; j < num; j++) { //TODO:玩家移动 MovePlayer(maps, ref player, 1); //实现玩家一步一步前进或倒退的效果 DropMap(maps); DropPlayer(players, maps); Console.WriteLine("\a"); Thread.Sleep(300); } } } else { num = player.index+num <= 0 ? player.index : -num; //判断玩家所在位置距离起点是否大于所需要退的格子数,如果不足,则退回起点 //num = -num; for (int j = 0; j < num; j++) { //TODO:玩家移动 MovePlayer(maps, ref player, -1); //实现玩家一步一步前进或倒退的效果 DropMap(maps); DropPlayer(players, maps); Console.WriteLine("\a"); Thread.Sleep(300); } } } /// <summary> /// 判断是否胜出 /// </summary> /// <param name="args"></param> static bool IsWin(ref Player player, Map[] maps) { if(player.index==maps.Length-1) //判断玩家是否到达终点 { Console.SetCursorPosition(50,5); Console.WriteLine("玩家{0}获得了胜利",player.id); return true; //如果是则返回true } else { return false; //如果不是则返回false } } /// <summary> /// 突发事件 /// </summary> /// <param name="args"></param> static void GameEvent(Player[] players, ref Player player, Map[] maps,ref bool isGameOver) { Map map = maps[player.index]; //接收玩家所在的地图元素 switch (map.mapType) { case MapType.none_: //无事件发生 break; case MapType.mine_: //踩中地雷 Clear(); //清除突发状况前的游戏情况 #region 踩中地雷提示 Console.SetCursorPosition(50, 5); Console.WriteLine("玩家{0}踩到了地雷,",player.id); Console.SetCursorPosition(50, 6); Console.WriteLine("后退了6格"); #endregion MoveDrop(maps,players, ref player, -6); //调用方法后退6格 isGameOver = IsWin(ref player, maps); //判断该玩家是否胜出 if (isGameOver == true) { Clear(); //清屏,退出 break; } GameEvent(players, ref player, maps,ref isGameOver); //判断是否再次有突发事件 break; case MapType.spatio_: //踩中时空隧道 Clear(); //清除突发状况前的游戏情况 #region 时空隧道提示信息 Console.SetCursorPosition(50, 5); Console.WriteLine("玩家{0}踩到了时光隧道,", player.id); Console.SetCursorPosition(50, 6); Console.WriteLine("前进了10格"); #endregion MoveDrop(maps, players, ref player, 10); //调用方法前进10格 isGameOver = IsWin(ref player, maps); //判断该玩家是否胜出 if (isGameOver == true) //如果胜出,清屏退出 { Clear(); break; } GameEvent(players, ref player, maps,ref isGameOver); //如果没胜出,判断是否再次有突发事件 break; case MapType.debar_: //暂停事件 Clear(); //清除突发状况前的游戏情况 if (player.isDebar==false) //判断是否是暂停状态,如果不是,则状态转为暂停 { #region MyRegion Console.SetCursorPosition(50, 5); Console.WriteLine("玩家{0}踩到了暂停,", player.id); Console.SetCursorPosition(50, 6); Console.WriteLine("暂停1回合"); #endregion player.isDebar = true; } else //如果不是,则清除暂停状态 { #region 暂停结束提示 Console.SetCursorPosition(50, 5); Console.WriteLine("玩家{0}解除了暂停", player.id); Console.SetCursorPosition(50, 6); Console.WriteLine("{0,27}"," "); #endregion player.isDebar = false; //状态改为非暂停 } break; case MapType.swap_: Clear(); //清除突发状况前的游戏情况 SwapPos(maps, players, ref player); //GameEvent(players, ref player, maps); break; default: break; } } /// <summary> /// 清理上一回合信息 /// </summary> /// <param name="args"></param> static void Clear() { Console.SetCursorPosition(50, 2); Console.WriteLine("{0,27}", " "); Console.SetCursorPosition(50, 5); Console.WriteLine("{0,27}", " "); Console.SetCursorPosition(50, 6); Console.WriteLine("{0,27}", " "); } /// <summary> /// 创建骰子 /// </summary> /// <param name="args"></param> static string[,] CreatDice(int num) { string[,] one = { {" ", " ", " " ," "," "}, {" ", " ", " " ," "," "}, {" ", " ", " " ," "," "}, {" ", " ", "■■" ," "," "}, {" ", " ", "■■" ," "," "}, {" ", " ", " " ," "," "}, {" ", " ", " " ," "," "}, {" ", " ", " " ," "," "}, }; string[,] two = { {" ", " ", " " ," "," "}, {" ", " ", " " ," "," "}, {" ", " ", " " ," "," "}, {" ", "■■", " " ,"■■"," "}, {" ", "■■", " " ,"■■"," "}, {" ", " ", " " ," "," "}, {" ", " ", " " ," "," "}, {" ", " ", " " ," "," "}, }; string[,] three = { {"■■", " ", " " ," "," "}, {"■■", " ", " " ," "," "}, {" ", " ", " " ," "," "}, {" ", " ", "■■" ," "," "}, {" ", " ", "■■" ," "," "}, {" ", " ", " " ," "," "}, {" ", " ", " " ," ","■■"}, {" ", " ", " " ," ","■■"}, }; string[,] four = { {" ", " ", " " ," "," "}, {" ", " ", " " ," "," "}, {" ", "■■", " " ,"■■"," "}, {" ", "■■", " " ,"■■"," "}, {" ", " ", " " ," "," "}, {" ", "■■", " " ,"■■"," "}, {" ", "■■", " " ,"■■"," "}, {" ", " ", " " ," "," "}, }; string[,] five = { {"■■", " ", " " ," ","■■"}, {"■■", " ", " " ," ","■■"}, {" ", " ", " " ," "," "}, {" ", " ", "■■" ," "," "}, {" ", " ", "■■" ," "," "}, {" ", " ", " " ," "," "}, {"■■", " ", " " ," ","■■"}, {"■■", " ", " " ," ","■■"}, }; string[,] six = { //{"■■", " ", "■■" }, //{"■■", " ", "■■" }, //{" ", " ", " " }, //{"■■", " ", "■■" }, //{"■■", " ", "■■" }, //{" ", " ", " " }, //{"■■", " ", "■■" }, //{"■■", " ", "■■" }, {" ", "■■", " " ,"■■"," "}, {" ", "■■", " " ,"■■"," "}, {" ", " ", " " ," "," "}, {" ", "■■", " " ,"■■"," "}, {" ", "■■", " " ,"■■"," "}, {" ", " ", " " ," "," "}, {" ", "■■", " " ,"■■"," "}, {" ", "■■", " " ,"■■"," "}, }; switch (num) { case 1: return one; break; case 2: return two; break; case 3: return three; break; case 4: return four; break; case 5: return five; break; default: return six; break; } } /// <summary> /// 绘制骰子 /// </summary> /// <param name="args"></param> static void DropDice(string[,] dice_, int num) { if (num == 1 || num == 4) { Console.ForegroundColor = ConsoleColor.Red; //如果是1点和4点,骰子是红色 } else { Console.ForegroundColor = ConsoleColor.Blue; //如果不是1和4点,则都是蓝色 } for (int j = 0; j < dice_.GetLength(0); j++) { Console.SetCursorPosition(53, 13 + j); for (int k = 0; k < dice_.GetLength(1); k++) //遍历二维数组,绘制骰子 { Console.Write(dice_[j, k]); } Console.WriteLine(); } Console.ForegroundColor = ConsoleColor.Gray; } /// <summary> /// 开机画面 /// </summary> /// <param name="args"></param> static void StartGame() { Console.SetCursorPosition(10, 3); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine(" iv r: :X :QBq"); Console.WriteLine(" isdMBBBZr .BBBB :SBBBB. DBB BBD .rXQBr"); Console.WriteLine(" BBBBBQBBBBJ MBQQsi XBBBg25. BR gBQJMBQBBJ"); Console.WriteLine(" LBP: BB Q 7BBv :J iv7qvBS:. .J"); Console.WriteLine(" IBrBBQ B5 :BM i5BBBBBQ 7BBBBBQBi7.BBiiBQBQ:"); Console.WriteLine(" BBs. BBBB.PBBQBBBuJ5 KD:BZ . iRBBBdXBgP"); Console.WriteLine(" Bg5YiLi 5BBB 2: B: BBRX Bsr7.B"); Console.WriteLine(" :B BBB. BB 7B .BQ BB..BB B:.:UB"); Console.WriteLine(" SQ7 : bB. LBB gBY BB BUiIBB.vr"); Console.WriteLine(" iBBP: .7 QB2 BBB .QB .Bv YuBBgQBBBB."); Console.WriteLine(" SBBBBBBBQ BQB QBBB QBB rBQ iMR :QL "); Console.WriteLine(" r1dRBB gBBI :BE YBI: BBg .BB .BB "); Console.WriteLine(" :"); Console.SetCursorPosition(32, 21); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("请按任意键开始"); Console.SetCursorPosition(32, 25); } static void Main(string[] args) { //TODO:定义开机画面 StartGame(); //1:定义坐标结构体:x轴 y轴 //2:定义事件的枚举类型:方块none_ 地雷mine_ 时空隧道spatio_ 暂停debar_ 幸运转盘swap_ //3:定义地图方格结构体:坐标,图标,事件类型 方块□ 地雷● 时空隧道卍 暂停▲ 幸运转盘◎ //4:定义玩家结构体:坐标,图标,所在位置,是否暂停,编号id //5:定义围墙结构体:坐标,图标 Console.ReadKey(true); Console.Clear(); //6:制作围墙 Wall[,] walls = new Wall[24,78]; CreatWall(walls); //创建围墙 //7:制作地图 先全部实现无特殊事件 一维数组存储地图 Map[] maps = new Map[74]; CreatMap(maps); //调用方法绘制围墙 //8:遍历地图,给地图添加随机事件 Random r = new Random(); MapEvent(maps, r); //9:绘制地图 DropMap(maps); //调用方法绘制地图 //10:绘制玩家 Player[] players = new Player[2]; //创建玩家数组 CreatPlayer(players,maps); //调用方法创建角色 DropPlayer(players, maps); //调用方法绘制角色 bool isGameOver = false; //11:开始游戏 右侧上显示玩家事件,右下显示掷骰子点数 while (true) { //遍历每个玩家流程 for (int i = 0; i < players.Length; i++) { Console.SetCursorPosition(50, 1); Console.WriteLine("玩家{0}请投掷", players[i].id); //提示哪个玩家开始投掷 if (players[i].isDebar==true) { Console.SetCursorPosition(50, 5); Console.WriteLine("玩家{0}无法行动", players[i].id); //提示哪个玩家开始投掷 GameEvent(players, ref players[i], maps,ref isGameOver); players[i].isDebar = false; continue; } Console.ReadKey(true); //按任意键投掷 Clear(); //清理上回合信息 int num = r.Next(1, 7); //骰子随机数 string[,] dice_ = CreatDice(num); //创建骰子 DropDice(dice_, num); //绘制骰子 Console.SetCursorPosition(50, 2); Console.WriteLine("玩家{0}投出了{1}点", players[i].id, num); //提示玩家投出几点 MoveDrop(maps, players, ref players[i], num); //12:判断是否有玩家胜出 isGameOver = IsWin(ref players[i], maps); //判断该玩家到终点 if (isGameOver==true) { Console.SetCursorPosition(50, 1); Console.WriteLine("玩家{0}获得胜利",players[i].id); Clear(); break; //退出for循环 } //13:实现突发事件逻辑 GameEvent(players, ref players[i], maps, ref isGameOver); //突发事件 if (isGameOver == true) //再次判断是否玩家胜出 { Console.SetCursorPosition(50, 1); Console.WriteLine("玩家{0}获得胜利", players[i].id); Clear(); break; //退出for循环 } //踩中玩家后退6格事件 if (i==0) //判断当前玩家是1还是2号 { if (players[i].index == players[i + 1].index && players[i+1].index!=0) //判断玩家所在索引是否跟另一个玩家相同,而且不在起点 { #region 互踩提示信息 Console.SetCursorPosition(50, 5); Console.WriteLine("玩家{0}踩到了玩家{1},", players[i].id, players[i + 1].id); Console.SetCursorPosition(50, 6); Console.WriteLine("玩家{0}倒退了6格", players[i + 1].id); #endregion MoveDrop(maps, players, ref players[i+1], -6); //被踩玩家后退6格 GameEvent(players, ref players[i+1], maps,ref isGameOver); //判断后退后的突发事件 } } else { if (players[i].index == players[i - 1].index && players[i - 1].index != 0) //2号玩家流程 { #region 互踩提示信息 Console.SetCursorPosition(50, 5); Console.WriteLine("玩家{0}踩到了玩家{1},", players[i].id, players[i - 1].id); Console.SetCursorPosition(50, 6); Console.WriteLine("玩家{0}倒退了6格", players[i - 1].id); #endregion MoveDrop(maps, players, ref players[i - 1], -6); GameEvent(players, ref players[i-1], maps,ref isGameOver); } } } //结束游戏 if (isGameOver == true) { break; } } Console.ReadKey(); } } }
结果:
标签:10,Console,maps,c#,飞行棋,玩家,player,players,WriteLine 来源: https://www.cnblogs.com/yifengs/p/14057357.html