C#示例(一):飞行棋游戏

发布时间:2023年12月26日

1、先看一下实现效果

输入连个玩家的姓名
在这里插入图片描述

两个玩家分别用字母A和字母B表示
在这里插入图片描述
按下任意键开始掷骰子、根据骰子走对应的步数…

在这里插入图片描述

2、绘制游戏头

/// <summary>
        /// 画游戏头
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Blue;//设置颜色
            Console.WriteLine("欢迎来到不写八个飞行棋");
            Console.WriteLine("▼▼▼▼▼");
        }

在这里插入图片描述

3、输入玩家姓名

#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[1] == "")
                {
                    Console.WriteLine("玩家B的姓名不能为空,请重新输入:");
                    PlayerNames[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("玩家B的姓名不能与玩家A的姓名相同,请重新输入:");
                    PlayerNames[1] = Console.ReadLine();
                }
            }
            #endregion
            //玩家姓名输入之后,我们首先应该清屏
            Console.Clear();

在这里插入图片描述

4、初始化地图

/// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitailMap()
        {
            int[] LuckTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘
            for (int i = 0; i < LuckTurn.Length; i++)
            {
                int index = LuckTurn[i];//获取幸运轮盘的下标
                Maps[index] = 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;
            }
        }

5、绘制地图

/// <summary>
        /// 画地图
        /// </summary>
        public static void DrawMap()
        {
            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));
            }
            #endregion

            Console.WriteLine();

        }//DrawMap

6、开始玩游戏

/// <summary>
        /// 玩游戏
        /// </summary>
        public static void PlayGame(int PlayerNumber)
        {
            Random rd = new Random();
            int rNumber = rd.Next(1, 7);
            Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[PlayerNumber]);
            Console.ReadKey(true);//设置其参数为true则玩家按下的键不会再控制台显示
            Console.WriteLine("{0}掷出了{1}", PlayerNames[PlayerNumber], rNumber);
            PlayerPops[PlayerNumber] += rNumber;
            ChangePops();
            Console.ReadKey(true);
            Console.WriteLine("{0}按任意键开始行动", PlayerNames[PlayerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}行动完了", PlayerNames[PlayerNumber]);
            Console.ReadKey(true);

            if (PlayerPops[PlayerNumber] == PlayerPops[1-PlayerNumber])//踩到了另外一个玩家
            {
                Console.WriteLine("{0}玩家踩到了{1}玩家,{2}玩家退六格", PlayerNames[PlayerNumber], PlayerNames[1-PlayerNumber], PlayerNames[1-PlayerNumber]);
                PlayerPops[1 - PlayerNumber] -= 6;
                ChangePops();
                Console.ReadKey(true);
            }
            else//踩到了关卡
            {
                switch (Maps[PlayerPops[PlayerNumber]])
                {
                    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 = PlayerPops[PlayerNumber];
                                PlayerPops[PlayerNumber] = PlayerPops[1 - PlayerNumber];
                                PlayerPops[1 - PlayerNumber] = temp;
                                Console.WriteLine("交换完成!!!按任意键继续游戏");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退六格", PlayerNames[PlayerNumber], PlayerNames[1 - PlayerNumber], PlayerNames[1 - PlayerNumber]);
                                Console.ReadKey(true);
                                PlayerPops[1 - PlayerNumber] -= 6;
                                ChangePops();
                                Console.WriteLine("玩家{0}退了六格", PlayerNames[1 - PlayerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能输入1或2 1--交换位置  2--轰炸对方");
                                input = Console.ReadLine();
                            }
                        }
                        break;
                    case 2: Console.WriteLine("玩家{0}踩到了地雷,退六格", PlayerNames[PlayerNumber]);
                        PlayerPops[PlayerNumber] -= 6;
                        ChangePops();
                        Console.ReadKey(true);
                        break;
                    case 3: Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[PlayerNumber]);
                        Flags[PlayerNumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4: Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[PlayerNumber]);
                        PlayerPops[PlayerNumber] += 10;
                        ChangePops();
                        Console.ReadKey(true);
                        break;
                }//switch
            }//else
            ChangePops();
            Console.Clear();
            DrawMap();
        }

7、整体代码

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04飞行棋游戏
{
    public class Program
    {
        static int[] Maps = new int[100];
        //声明一个静态数组来存储玩家A和玩家B的坐标
        static int[] PlayerPops = new int[2];
        //存储两个玩家的姓名
        static string[] PlayerNames = new string[2];
        //两个玩家的标记
        static bool[] Flags = new bool[2];//flags[0]是玩家A的标记,默认是flase,flags[1]是玩家B的标记,默认是flase
        
        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[1] == "")
                {
                    Console.WriteLine("玩家B的姓名不能为空,请重新输入:");
                    PlayerNames[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("玩家B的姓名不能与玩家A的姓名相同,请重新输入:");
                    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 (PlayerPops[0] < 99 && PlayerPops[1] < 99)
            {
                if (Flags[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    Flags[0] = false;
                }
                if (PlayerPops[0] >= 99)
                {
                    Console.WriteLine("玩家{0}赢了玩家{1}", PlayerNames[0], PlayerNames[1]);
                    break;
                }

                if (Flags[1] == false)
                {
                    PlayGame(1);
                }
                else
                {
                    Flags[1] = false;
                }
                if (PlayerPops[1] >= 99)
                {
                    Console.WriteLine("玩家{0}赢了玩家{1}", PlayerNames[1], PlayerNames[0]);
                    break;
                }
            }//while
            Win();
            Console.ReadKey();
        }
        /// <summary>
        /// 画游戏头
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Blue;//设置颜色
            Console.WriteLine("欢迎来到不写八个飞行棋");
            Console.WriteLine("▼▼▼▼▼");
        }

        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitailMap()
        {
            int[] LuckTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘
            for (int i = 0; i < LuckTurn.Length; i++)
            {
                int index = LuckTurn[i];//获取幸运轮盘的下标
                Maps[index] = 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;
            }
        }

        /// <summary>
        /// 画地图
        /// </summary>
        public static void DrawMap()
        {
            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));
            }
            #endregion

            Console.WriteLine();

        }//DrawMap

        /// <summary>
        /// 从画地图的方法中抽象出来的一个方法
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string DrawStringMap(int i)
        {
            string str = "";
            #region 画图
            if (PlayerPops[0] == PlayerPops[1] && PlayerPops[1] == i)
            {
                str = "<>";
            }
            else if (PlayerPops[0] == i)
            {
                str = "A";
            }
            else if (PlayerPops[1] == i)
            {
                str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str="◎";//幸运轮盘
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        str="★";//地雷
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str="▼";//暂停
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Black;
                        str="▓";//时空隧道
                        break;
                }//switch
            }//else
            return str;
            #endregion
        }

        /// <summary>
        /// 玩游戏
        /// </summary>
        public static void PlayGame(int PlayerNumber)
        {
            Random rd = new Random();
            int rNumber = rd.Next(1, 7);
            Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[PlayerNumber]);
            Console.ReadKey(true);//设置其参数为true则玩家按下的键不会再控制台显示
            Console.WriteLine("{0}掷出了{1}", PlayerNames[PlayerNumber], rNumber);
            PlayerPops[PlayerNumber] += rNumber;
            ChangePops();
            Console.ReadKey(true);
            Console.WriteLine("{0}按任意键开始行动", PlayerNames[PlayerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}行动完了", PlayerNames[PlayerNumber]);
            Console.ReadKey(true);

            if (PlayerPops[PlayerNumber] == PlayerPops[1-PlayerNumber])//踩到了另外一个玩家
            {
                Console.WriteLine("{0}玩家踩到了{1}玩家,{2}玩家退六格", PlayerNames[PlayerNumber], PlayerNames[1-PlayerNumber], PlayerNames[1-PlayerNumber]);
                PlayerPops[1 - PlayerNumber] -= 6;
                ChangePops();
                Console.ReadKey(true);
            }
            else//踩到了关卡
            {
                switch (Maps[PlayerPops[PlayerNumber]])
                {
                    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 = PlayerPops[PlayerNumber];
                                PlayerPops[PlayerNumber] = PlayerPops[1 - PlayerNumber];
                                PlayerPops[1 - PlayerNumber] = temp;
                                Console.WriteLine("交换完成!!!按任意键继续游戏");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退六格", PlayerNames[PlayerNumber], PlayerNames[1 - PlayerNumber], PlayerNames[1 - PlayerNumber]);
                                Console.ReadKey(true);
                                PlayerPops[1 - PlayerNumber] -= 6;
                                ChangePops();
                                Console.WriteLine("玩家{0}退了六格", PlayerNames[1 - PlayerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能输入1或2 1--交换位置  2--轰炸对方");
                                input = Console.ReadLine();
                            }
                        }
                        break;
                    case 2: Console.WriteLine("玩家{0}踩到了地雷,退六格", PlayerNames[PlayerNumber]);
                        PlayerPops[PlayerNumber] -= 6;
                        ChangePops();
                        Console.ReadKey(true);
                        break;
                    case 3: Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[PlayerNumber]);
                        Flags[PlayerNumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4: Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[PlayerNumber]);
                        PlayerPops[PlayerNumber] += 10;
                        ChangePops();
                        Console.ReadKey(true);
                        break;
                }//switch
            }//else
            ChangePops();
            Console.Clear();
            DrawMap();
        }

        /// <summary>
        /// 当玩家坐标改变的时候
        /// </summary>
        public static void ChangePops()
        {
            if (PlayerPops[0] < 0)
            {
                PlayerPops[0] = 0;
            }

            if (PlayerPops[0] > 99)
            {
                PlayerPops[0] = 99;
            }

            if (PlayerPops[1] < 0)
            {
                PlayerPops[1] = 0;
            }

            if (PlayerPops[1] > 99)
            {
                PlayerPops[1] = 99;
            }
        }

        public static void Win()
        {
            Console.WriteLine("胜利胜利");
        }
    }
}

文章来源:https://blog.csdn.net/WwLK123/article/details/135214811
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。