C#控制台实现飞行棋小游戏

 更新时间:2021年07月21日 10:40:23   作者:王大瑜  
这篇文章主要为大家详细介绍了C#控制台实现飞行棋小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#控制台实现飞行棋小游戏的具体代码,供大家参考,具体内容如下

游戏标题

static void ShowTitle()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("*****************飞行棋*****************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("****************************************");
        }

游戏规则说明

1、玩家A和玩家B必须输入一个非纯数字的非空用户名
2、玩家A先掷骰子,AB玩家轮流投掷骰子
3游戏中“□”代表普通格子,“◎”代表幸运轮盘,“★”代表地雷,“▲”代表暂停,“卍”代表时空隧道
4、“□”对于玩家没有任何奖惩!
5、“◎”玩家具有两种选择:a.选择与对方交换位置;b.选择轰炸对方使对方倒退6步
6、“★”对于玩家惩罚使玩家倒退6步
7、“▲”惩罚玩家下一轮暂停操作
8、“卍”奖励玩家直接前进10步
9、果踩到对方则惩罚对方直接倒退6步

游戏的地图

地图共由100个格子组成,Z型实现,第一行从左往右30个格子,第一列往下5个,第二行从右往左30个格子,第二列往下5个第三行从左往右30个

1、声明一个int类型的一维数组,长度是100,每个位置的默认值是0
2、“□”普通格子也就是默认的值0代表
3、“◎”幸运轮盘使用数字1代表 {6,23,40,55,69,83}
4、“★”地雷使用数字2代表 {5,13,17,33,38,50,64,80,94}
5、“▲”暂停使用数字3代表 {9,27,60,93}
6、“卍”时空隧道使用数字4代表 {20,25,45,63,88,90}

绘制游戏规则

static void ShowRule()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("------------------------------------------------------------");
            Console.WriteLine("玩家【1】使用A表示");
            Console.WriteLine("玩家【2】使用B表示");
            Console.WriteLine("规则说明:");
            Console.WriteLine("1.玩家A先掷骰子,A、B玩家轮流投掷骰子");
            Console.WriteLine("2.“□”对于玩家没有任何奖惩!");
            Console.WriteLine("3.“◎”玩家具有两种选择:a.选择与对方交换位置;b.选择轰炸对方使对方倒退6步");
            Console.WriteLine("4.“★”对于玩家惩罚使玩家倒退6步");
            Console.WriteLine("5.“▲”惩罚玩家下一轮暂停操作");
            Console.WriteLine("6.“卍”奖励玩家直接前进10步");
            Console.WriteLine("7.如果踩到对方则惩罚对方直接倒退6步");
            Console.WriteLine("------------------------------------------------------------");
        }

初始化地图

static void InitialMap()
        {
            //确定“◎”=1
            int[] lunckturn = { 6, 23, 40, 55, 69, 83 };
            for (int i = 0; i < lunckturn.Length; i++)
            {
                int index = lunckturn[i];
                Maps[index] = 1;
            }
            //确定“★”=2
            int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };
            for (int i = 0; i < landmine.Length; i++)
            {
                Maps[landmine[i]] = 2;
            }
            //确定“▲”=3
            int[] pause = { 9, 27, 60, 93 };
            for (int i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;
            }
            //确定“卍”=4
            int[] timeTunnel = { 20, 25, 45, 63, 88, 90 };
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                Maps[timeTunnel[i]] = 4;
            }
        }

绘制地图

/// <summary>
        /// 绘制地图
        /// </summary>
        static void DrawMap()
        {
            //第一行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawString(i));
            }
            Console.WriteLine();
            //第一列
            for (int i = 30; i < 35; i++)
            {
                for (int k = 0; k < 29; k++)
                {
                    Console.Write(" ");
                }
                Console.Write(DrawString(i));
                Console.WriteLine();
            }
            //第二行
            for (int i = 64; i > 34; i--)
            {
                Console.Write(DrawString(i));
            }
            Console.WriteLine();
            //第二列
            for (int i = 65; i <70; i++)
            {
                Console.WriteLine(DrawString(i));
            }
            //第三行
            for (int i = 70; i < 100; i++)
            {
                Console.Write(DrawString(i));
            }
            Console.WriteLine();
        }

        /// <summary>
        /// 根据地图数组每个位置的值选择绘制的图案
        /// </summary>
        /// <param name="type">地图数组中位置数字代表地图位置索引</param>
        /// <returns>返回最终选择的图案</returns>
        static string DrawString(int index)
        {
            string str = "";
            //两名玩家的位置重合,并且保证两个玩家的位置都在地图中
            if (playerPosition[0]==playerPosition[1]&&playerPosition[0]==index)
            {
                Console.ForegroundColor = ConsoleColor.White;
                str = "<>";
            }
            else if (playerPosition[0]==index)
            {
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                str = "A";
            }
            else if (playerPosition[1]==index)
            {
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                str = "B";
            }
            else
            {
                switch (Maps[index])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "★";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.DarkBlue;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "卍";
                        break;
                    default:
                        break;
                }
            }
            return str;
        }

实现用户注册

 /// <summary>
        /// 地图
        /// </summary>
        static int[] Maps = new int[100];
        /// <summary>
        /// 用户姓名
        /// </summary>
        static string[] playerName = new string[2];
        /// <summary>
        /// 两名玩家的位置
        /// </summary>
        static int[] playerPosition = new int[2];
        static void Main(string[] args)
        {
            //游戏标题
            ShowTitle();
            #region///用户名注册
            Console.WriteLine("请两名玩家先进行用户注册!");
            Console.WriteLine("注册规则:不能为空,并且不能是纯数字,玩家AB姓名不能相同");
            Console.WriteLine("请输入玩家A的姓名:");
            string pattern = @"^\d+$";
            bool isok = true;
            while (isok)
            {
                playerName[0] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[0]))
                {
                    Console.WriteLine("玩家A的姓名不能为空,请重新输入");
                }
                else if (Regex.IsMatch(playerName[0], pattern))
                {
                    Console.WriteLine("玩家A的姓名不能是纯数字,请重新输入");
                }
                else
                {
                    isok = false;
                }
            }
            isok = true;
            Console.WriteLine("请输入玩家B的姓名:");
            while (isok)
            {
                playerName[1] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[1]))
                {
                    Console.WriteLine("玩家B的姓名不能为空,请重新输入");
                }
                else if(Regex.IsMatch(playerName[1], pattern))
                {
                    Console.WriteLine("玩家B的姓名不能是纯数字,请重新输入");
                }
                else if (playerName[0]==playerName[1])
                {
                    Console.WriteLine("玩家B的姓名已被占用,请重新输入");
                }
                else
                {
                    isok = false;
                }
            }
            #endregion
            Console.Clear();
            ShowTitle();
            ShowRule();
            InitialMap();
            DrawMap();
            Console.ReadLine();
        }

游戏逻辑

1、玩家A按下任意键先手掷骰子
2、掷完骰子出现随机的1-6步
3、按下任意键进行移动相对应的步数

 /// <summary>
        /// 地图
        /// </summary>
        static int[] Maps = new int[100];
        /// <summary>
        /// 用户姓名
        /// </summary>
        static string[] playerName = new string[2];
        /// <summary>
        /// 两名玩家的位置
        /// </summary>
        static int[] playerPosition = new int[2];
        /// <summary>
        /// 标记两个玩家谁该进行投掷筛子
        /// </summary>
        static bool[] playerFlag = new bool[2];
        static void Main(string[] args)
        {
            //游戏标题
            ShowTitle();
            #region///用户名注册
            Console.WriteLine("请两名玩家先进行用户注册!");
            Console.WriteLine("注册规则:不能为空,并且不能是纯数字,玩家AB姓名不能相同");
            Console.WriteLine("请输入玩家A的姓名:");
            string pattern = @"^\d+$";
            bool isok = true;
            while (isok)
            {
                playerName[0] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[0]))
                {
                    Console.WriteLine("玩家A的姓名不能为空,请重新输入");
                }
                else if (Regex.IsMatch(playerName[0], pattern))
                {
                    Console.WriteLine("玩家A的姓名不能是纯数字,请重新输入");
                }
                else
                {
                    isok = false;
                }
            }
            isok = true;
            Console.WriteLine("请输入玩家B的姓名:");
            while (isok)
            {
                playerName[1] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[1]))
                {
                    Console.WriteLine("玩家B的姓名不能为空,请重新输入");
                }
                else if(Regex.IsMatch(playerName[1], pattern))
                {
                    Console.WriteLine("玩家B的姓名不能是纯数字,请重新输入");
                }
                else if (playerName[0]==playerName[1])
                {
                    Console.WriteLine("玩家B的姓名已被占用,请重新输入");
                }
                else
                {
                    isok = false;
                }
            }
            #endregion
            Console.Clear();
            ShowTitle();
            ShowRule();
            InitialMap();
            DrawMap();
            //开始进入游戏
            //判断两名玩家都未到达终点则游戏继续
            while (playerPosition[0]<99&&playerPosition[1]<99)
            {
                if (playerFlag[0]==false)
                {
                    PlayGame(0);
                }
                else
                {
                    playerFlag[0] = false;
                }

                if (playerFlag[1]==false)
                {
                    PlayGame(1);
                }
                else
                {
                    playerFlag[1] = false;
                }

                if (playerPosition[0]>=99)
                {
                    Console.WriteLine("恭喜玩家【{0}】获胜!",playerName[0]);
                    break;
                }
                else if (playerPosition[1] >= 99)
                {
                    Console.WriteLine("恭喜玩家【{0}】获胜!", playerName[1]);
                    break;
                }
            }
            Console.ReadLine();
        }

4、移动完成之后判断这个格子的功能,并实现特殊格子的功能
5、按下任意键完成界面

/// <summary>
        /// 实现游戏功能的方法
        /// </summary>
        /// <param name="playerIndex">通过玩家索引判断本次游戏操作是由哪个玩家发起的</param>
        static void PlayGame(int playerIndex)
        {
            #region ///为了实现玩家掷骰子算出移动步数
            Random r = new Random();
            Console.WriteLine("本回合由玩家【{0}】按下任意键掷骰子!",playerName[playerIndex]);
            Console.ReadKey(true);
            int number=r.Next(1, 7);
            Console.WriteLine("玩家【{0}】掷出<{1}>点",playerName[playerIndex],number);
            Console.WriteLine("玩家【{0}】按下任意键开始移动!",playerName[playerIndex]);
            Console.ReadKey(true);
            playerPosition[playerIndex] += number;
            Console.WriteLine("玩家【{0}】移动完成!",playerName[playerIndex]);
            #endregion
            CheckPosition();
            #region///进行对移动结果的格子奖惩判断
            //先判断两个玩家是否踩到对方
            if (playerPosition[0]==playerPosition[1])
            {
                Console.WriteLine("玩家【{0}】踩到玩家【{1}】,玩家【{1}】退6格!",playerName[playerIndex],playerName[1-playerIndex]);
                playerPosition[1-playerIndex] -= 6;
            }
            else
            {
                switch (Maps[playerPosition[playerIndex]])
                {
                    //踩到普通格子
                    case 0:
                        Console.WriteLine("玩家【{0}】踩到安全地带!无奖惩!按下任意键刷新界面!",playerName[playerIndex]);
                        Console.ReadKey(true);
                        break;
                    //踩到幸运轮盘
                    case 1:
                        Console.WriteLine("玩家【{0}】踩到幸运轮盘!请选择:a--交换位置,b--轰炸对方",playerName[playerIndex]);
                        string type = Console.ReadLine();
                        while (true)
                        {
                            if (type=="a")
                            {
                                Console.WriteLine("玩家【{0}】选择与玩家【{1}】交换位置",playerName[playerIndex],playerName[1-playerIndex]);
                                int temp = playerPosition[0];
                                playerPosition[0] = playerPosition[1];
                                playerPosition[1] = temp;
                                Console.WriteLine("玩家【{0}】与玩家【{1}】交换位置完成,按下任意键刷新界面!", playerName[playerIndex], playerName[1 - playerIndex]);
                                Console.ReadKey(true);
                                break;
                            }
                            else if (type=="b")
                            {
                                Console.WriteLine("玩家【{ 0}】选择轰炸玩家【{ 1}】", playerName[playerIndex], playerName[1 - playerIndex]);
                                playerPosition[1-playerIndex] -= 6;
                                Console.WriteLine("玩家【{ 0}】轰炸玩家【{ 1}】完成,按下任意键刷新界面!", playerName[playerIndex], playerName[1 - playerIndex]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("请输入正确的指令!请选择:a--交换位置,b--轰炸对方");
                                type = Console.ReadLine();
                            }
                        }
                        break;
                        //踩到地雷
                    case 2:
                        Console.WriteLine("玩家【{0}】踩到地雷,退6格!按下任意键刷新界面",playerName[playerIndex]);
                        playerPosition[playerIndex] -= 6;
                        Console.ReadKey(true);
                        break;
                        //踩到暂停
                    case 3:
                        Console.WriteLine("玩家【{0}】踩到暂停,下一回合暂停!按下任意键刷新界面",playerName[playerIndex]);
                        playerFlag[playerIndex] = true;
                        break;
                        //踩到时空隧道
                    case 4:
                        Console.WriteLine("玩家【{0}】踩到时空隧道,前进10步!按下任意键刷新界面", playerName[playerIndex]);
                        playerPosition[playerIndex] += 10;
                        Console.ReadKey(true);
                        break;
                    default:
                        break;
                }
            }
            CheckPosition();
            Console.Clear();
            ShowTitle();
            ShowRule();
            InitialMap();
            DrawMap();
            #endregion
        }
        /// <summary>
        /// 断是否超越终点或者超出起点
        /// </summary>
        static void CheckPosition()
        {
            if (playerPosition[0] < 0)
            {
                playerPosition[0] = 0;
            }
            if (playerPosition[0] > 99)
            {
                playerPosition[0] = 99;
            }
            if (playerPosition[1] < 0)
            {
                playerPosition[1] = 0;
            }
            if (playerPosition[1] > 99)
            {
                playerPosition[1] = 99;
            }
        }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C# wpf使用GDI+实现截屏效果

    C# wpf使用GDI+实现截屏效果

    wpf做屏幕录制或者屏幕广播之类的功能时需要实现截屏,在C#中比较容易实现的截屏方法是使用GDI+,所以本文将展示一下如何使用GDI+实现截屏,需要的可以参考下
    2023-09-09
  • C# 使用BitBlt进行窗口抓图的示例

    C# 使用BitBlt进行窗口抓图的示例

    这篇文章主要介绍了C# 使用BitBlt进行窗口抓图的示例,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2021-01-01
  • C#中增强类功能的几种方式详解

    C#中增强类功能的几种方式详解

    这篇文章主要给大家介绍了关于C#中增强类功能的几种方式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2018-12-12
  • AOP从静态代理到动态代理(Emit实现)详解

    AOP从静态代理到动态代理(Emit实现)详解

    AOP为Aspect Oriented Programming的缩写,意思是面向切面编程的技术。下面这篇文章主要给大家介绍了关于AOP从静态代理到动态代理(Emit实现)的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-09-09
  • 浅谈C# 非模式窗体show()和模式窗体showdialog()的区别

    浅谈C# 非模式窗体show()和模式窗体showdialog()的区别

    下面小编就为大家带来一篇浅谈C# 非模式窗体show()和模式窗体showdialog()的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • C#中如何为枚举类型添加描述方法【小技巧】

    C#中如何为枚举类型添加描述方法【小技巧】

    相信很多人对枚举并不陌生,枚举可以很方便和直观的管理一组特定值。下面这篇文章主要给大家介绍了关于C#中如何为枚举类型添加描述方法的相关资料,需要的朋友可以参考下
    2019-02-02
  • c#接口使用示例分享

    c#接口使用示例分享

    接口与抽象类一样,也是表示某种规则,一旦使用了该规则,就必须实现相关的方法。对于C#语言而言,由于只能继承自一个父类,因此若有多个规则需要实现,则使用接口是个比较好的做法
    2014-02-02
  • c#转换全角半角方法示例

    c#转换全角半角方法示例

    这篇文章主要介绍了c#如何转换全角半角,大家可以看一下下面的代码方法,可以参考一下
    2013-12-12
  • C#利用File方法对文件的操作总结(字节写入和读取)

    C#利用File方法对文件的操作总结(字节写入和读取)

    使用C#语言中的File类我们能够非常轻松的使用一些文件操作的函数来完成对文件简单的读写操作,这篇文章主要给大家介绍了光宇C#利用File方法对文件的操作的相关资料,包括字节写入和读取,需要的朋友可以参考下
    2021-08-08
  • C#使用MailAddress类发送html格式邮件的实例代码

    C#使用MailAddress类发送html格式邮件的实例代码

    这篇文章主要介绍如何使用C#的MailAddress类发送邮件的方法,大家参考使用吧
    2013-11-11

最新评论