C#学习笔记之飞行棋项目

本文实例为大家分享了C#控制台实现飞行棋项目的具体代码,供大家参考,具体内容如下
飞行棋游戏介绍
此次编程实现的飞行棋和我们小时候玩的有些不一样,规则大致类似,但是我在学习过程中的飞行棋大大简化了过程;当前编写的程序适合双人玩(也可改成更多人),主要存在以下几种关卡:
幸运轮盘*:给玩家两种选择 1–交换位置;2–使另一玩家退6格
地雷$:玩家退6格
暂停&:玩家暂停一回合
时空隧道#:玩家前进10格
玩家互踩会使另一玩家退6格
玩家A和玩家B先到终点处获胜
飞行棋游戏编写
该游戏存在100个字符串,在进行编程时刻对照每一横行和竖行来进行设计,并且玩家A与玩家B名字不能为空(但是光声明==“”不能实现该功能,目前还未解决该问题),不能相同
每个关卡的位置被固定(也可随机)

具体的步骤在注释中都有,附代码:
(觉得有用可以给我多多点赞)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GameDesign{class Program{//模拟全局变量--以便于多个函数使用static int[] Maps = new int[100]; //声明一个静态数组来存储玩家A和B的坐标static int[] PlayerPos = new int[2]; //声明一个静态数组来存储两个玩家的姓名static string[] PlayNames = new string[2]; //声明bool类型来标记两个玩家static bool[] PlayerFlags = new bool[2]; static void Main(string[] args){GameShow(); // 存在一个问题:当字符串为“”时游戏不能辨别,此时玩家姓名仍为空#region 输入玩家姓名Console.WriteLine("请输入玩家A的姓名"); PlayNames[0] = Console.ReadLine(); while (PlayNames[0] == " "){Console.WriteLine("玩家A的姓名不能为空,请重新输入"); PlayNames[0] = Console.ReadLine(); }Console.WriteLine("请输入玩家B的姓名"); PlayNames[1] = Console.ReadLine(); while (PlayNames[1] == "" || PlayNames[0] == PlayNames[1]){if (PlayNames[1] == ""){Console.WriteLine("玩家B的姓名不能为空,请重新输入"); PlayNames[1] = Console.ReadLine(); }else{Console.WriteLine("玩家A与玩家B的姓名不能相同,请重新输入"); PlayNames[1] = Console.ReadLine(); }}#endregion//玩家姓名输入Ok后,对其进行清屏Console.Clear(); //清屏GameShow(); Console.WriteLine("{0}的士兵用A表示", PlayNames[0]); Console.WriteLine("{0}的士兵用B表示", PlayNames[1]); //画地图之前,首先初始化地图OriginalMap(Maps); DrawMap(Maps); //游戏终止条件while (PlayerPos[0] <= 99 && PlayerPos[1] <= 99){if (PlayerFlags[0] == false){PlayGame(0); }else{PlayerFlags[0] = false; }if (PlayerPos[0] >= 99){Console.WriteLine("玩家{0}赢了玩家{1}", PlayNames[0], PlayNames[1]); break; }if (PlayerFlags[1] == false){PlayGame(1); }else{PlayerFlags[1] = false; }if (PlayerPos[1] >= 99){Console.WriteLine("玩家{0}赢了玩家{1}", PlayNames[1], PlayNames[0]); break; }//whileConsole.ReadKey(); }}/// /// 绘制游戏头/// static void GameShow(){Console.ForegroundColor=ConsoleColor.Blue; //用来设置颜色Console.WriteLine("--------------------------------"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("--------------------------------"); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("--------------------------------"); Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("-------欢迎来到Laser飞行棋------"); Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("--------------------------------"); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("--------------------------------"); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("--------------------------------"); }/// /// 初始化地图设置/// /// static void OriginalMap(int[] Maps){/** 初始化地图的过程---即将数组中数字变成字符串的过程* 存在五种特殊关卡,可利用数组中不同数字来代替* 0---代表正常,1---幸运轮盘,2---地雷,3---暂停,4---时空隧道* 地图中特殊关卡的坐标* 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,83,90}; */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, 83, 90 }; // 标记不同关卡的位置,分别用不同的数字表示for (int i = 0; i < luckyTurn.Length; i++){int n = luckyTurn[i]; Maps[n] = 1; //Maps[luckyTurn[i]]=1; }for (int i = 0; i < landMine.Length; i++){int n = landMine[i]; Maps[n] = 2; }for (int i = 0; i < pause.Length; i++){int n = pause[i]; Maps[n] = 3; }for (int i = 0; i < timeTunnel.Length; i++){int n = timeTunnel[i]; Maps[n] = 4; }}/// /// 绘制地图/// /// static void DrawMap(int[] Maps){Console.WriteLine("图例:幸运轮盘*,地雷$,暂停&,时空隧道#"); #region 绘制第一横行//画第一横行for (int i = 0; i < 30; i++){Console.Write(DrawStringMap(i)); }Console.WriteLine(); #endregion//画完每一行后需要换行#region 绘制第一竖行for (int i = 30; i < 35; i++){for (int j = 0; j <= 28 ; 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)); }Console.WriteLine(); #endregion}/// /// 初始化地图中的字符串/// /// /// static string DrawStringMap(int i){string str = ""; //首先确定玩家A和玩家B的坐标,如果坐标相同,且均处于地图上(令玩家坐标等于i),画static void PlayGame(int playernumber){Console.WriteLine("{0}按任意键开始掷骰子", PlayNames[playernumber]); Console.ReadKey(true); //对比Console.Readkey() 你可以发现不同效果Random r = new Random(); int step = r.Next(1, 7); //左闭右开区间PlayerPos[playernumber] += step; Console.WriteLine("{0}掷出了{1}", PlayNames[playernumber],step); Console.ReadKey(true); Console.WriteLine("{0}按任意键开始行动", PlayNames[playernumber]); Console.ReadKey(true); Console.WriteLine("{0}行动完了", PlayNames[playernumber]); //玩家A可能踩到 玩家B 幸运轮盘 地雷 暂停 时空隧道if (PlayerPos[playernumber] == PlayerPos[1- playernumber])//1-playernumber(使两者为相反数){Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayNames[playernumber], PlayNames[1- playernumber], PlayNames[1 - playernumber]); }else//踩到了关卡{switch (Maps[PlayerPos[playernumber]]){case 0:Console.WriteLine("玩家{0}踩到方块,安全", PlayNames[playernumber]); Console.ReadKey(true); break; case 1:Console.WriteLine("玩家{0}踩到幸运轮盘,请选择 1--交换位置 ,2--轰炸对方(对方退6格)", PlayNames[playernumber]); Console.ReadKey(true); string input = Console.ReadLine(); while (true){if (input == "1"){Console.WriteLine("玩家{0}与玩家{1}交换位置", PlayNames[playernumber], PlayNames[1- playernumber]); Console.ReadKey(true); int temp = PlayerPos[playernumber]; PlayerPos[playernumber] = PlayerPos[1- playernumber]; PlayerPos[1- playernumber] = temp; Console.WriteLine("交换完成,按任意键继续游戏"); Console.ReadKey(true); break; }else if (input == "2"){Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", PlayNames[playernumber], PlayNames[1- playernumber], PlayNames[1 - playernumber]); Console.ReadKey(true); PlayerPos[1- playernumber] -= 6; Console.WriteLine("玩家{0}退6格", PlayerPos[1- playernumber]); //注意是玩家{0},而不要输入玩家{1}Console.ReadKey(true); break; }else{Console.WriteLine("只能输入1或者2:1--交换位置 ,2--轰炸对方(对方退6格),请重新输入"); Console.ReadKey(true); input = Console.ReadLine(); }}break; case 2:Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerPos[playernumber]); PlayerPos[playernumber] -= 6; Console.ReadKey(true); break; case 3:Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerPos[playernumber]); PlayerFlags[playernumber] = true; Console.ReadKey(true); break; case 4:Console.WriteLine("玩家{0}踩到了时空隧道,"); PlayerPos[playernumber] += 10; Console.ReadKey(true); break; }//switch}//if-elseChangePos(playernumber); Console.Clear(); DrawMap(Maps); }/// /// 当玩家坐标改变时使用,用以限定范围/// /// static void ChangePos(int playnumber){if(PlayerPos[playnumber] <0){PlayerPos[playnumber] = 0; }if(PlayerPos[playnumber] >= 99){PlayerPos[playnumber] = 99; }}}}

【C#学习笔记之飞行棋项目】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读