C#飛行棋小遊戲

2022-06-28 08:34:13 浏览数 (1)

飛行棋小遊戲

先展示結果:

自己覺得還可以,很考驗思路

代码语言:javascript复制
//用靜態字段模擬全局變量
        public static int[] Maps = new int[100];
        //聲明一個靜態數組用來存儲玩家A和玩家B的坐標
        static int[] PlayerPos = new int[2];
        //存储两个玩家的姓名
        static string[] PlayerNames = new string[2];
        //存儲兩個人的狀態==關於是否執行
        static bool[] Falgs = new bool[2];

main方法調用其他方法

代码语言:javascript复制
static void Main(string[] args)
        {
            Show();
            #region 輸入玩家姓名
            Console.WriteLine("請輸入玩家A的姓名");
            PlayerNames[0] = Console.ReadLine();
            while (PlayerNames[0] == " ")
            {
                Console.WriteLine("玩家的姓名不能為空,請再次輸入");
                PlayerNames[0] = Console.ReadLine();
            }
            Console.WriteLine("請輸入玩家B的姓名");
            PlayerNames[1] = Console.ReadLine();
            while (PlayerNames[1] == " " || PlayerNames[1] == PlayerNames[0])
            {
                if (PlayerNames[1] == " ")
                {
                    Console.WriteLine("玩家的姓名不能為空,請再次輸入");
                    PlayerNames[0] = Console.ReadLine();
                }
                Console.WriteLine("兩名玩家的姓名不能一樣,再從新輸入玩家B的姓名");
                PlayerNames[0] = Console.ReadLine();
            }
            #endregion
            //輸入姓名完成後清屏  然後從新打印遊戲頭
            Console.Clear();
            Console.WriteLine("{0}的戰士用A表示,{1}的戰士用B表示", PlayerNames[0], PlayerNames[1]);
            InitailMap();
            DrawMap();
            while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
            {
                if (Falgs[0] == false)
                {
                    PalyGame(0);
                }
                else
                {
                    Falgs[0] = false;
                }
                if (PlayerPos[0] >= 99)
                {

                    Console.WriteLine("   ");
                    Console.WriteLine("玩家{0}無恥的贏了玩家{1}", PlayerNames[0], PlayerNames[1]);
                    break;
                }
                if (Falgs[0] == false)
                {
                    PalyGame(1);
                }
                else
                {
                    Falgs[1] = false;
                }
                if (PlayerPos[1] >= 99)
                {

                    Console.WriteLine("   ");
                    Console.WriteLine("玩家{0}無恥的贏了玩家{1}", PlayerNames[1], PlayerNames[0]);
                    break;
                }
            }//while結尾
            Console.WriteLine("GAME OVER");
            Console.ReadKey();
        }

遊戲頭

代码语言:javascript复制
 //遊戲頭
        public static void Show()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("***************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("***************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("*****快樂的飛行棋小遊戲****");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("***************************");
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("***************************");
            Console.ForegroundColor = ConsoleColor.Green;
        }

//加載地圖设置地图难度

代码语言:javascript复制
public static void InitailMap()
        {//普通□
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸運輪盤☆
            for (int i = 0; i < luckyturn.Length; i  )
            {
                Maps[luckyturn[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;
            }
        }

//畫地圖

代码语言:javascript复制
 public static void DrawMap()
        {
            Console.WriteLine("普通路:□ 幸運輪盤:☆ 地雷:● 暫停:▲ 時空隧道:◎");
            #region //第一行
            for (int i = 0; i < 30; i  )
            {
                Console.Write(DrawStringMap(i));
            }//for
            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 < 70; i  )
            {
                Console.Write(DrawStringMap(i));
                Console.WriteLine();
            }
            #endregion
            #region //第五行竖行
            for (int i = 70; i < 100; i  )
            {
                Console.Write(DrawStringMap(i));
            }
            #endregion 
        }
代码语言:javascript复制
/// <summary>
        /// 从画图的方法中抽象的一个方法
        /// </summary>
        /// <param name="i">需要传进去的变量</param>
        /// <returns>是那些需要画出来的字符符号</returns>

//画笔特殊字符

代码语言:javascript复制
 public static void PalyGame(int playerNumber)
        {
            Random r = new Random();
            int rNumber = r.Next(1, 7);//產生1-6的隨機數
            Console.WriteLine(" ");
            Console.WriteLine("{0}按任意鍵開始擲骰子", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}擲出了{1}", PlayerNames[playerNumber], rNumber);
            PlayerPos[playerNumber]  = rNumber;
            ChangPos();
            Console.ReadKey(true);
            Console.WriteLine("{0}按任意鍵開始移動", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}移動完成", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            //玩家A可能踩到了玩家B 方塊 幸運羅盤 地雷 暫停 時空隧道
            if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退六格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[playerNumber]);
                PlayerPos[1 - playerNumber] -= 6;
                ChangPos();
                Console.ReadKey(true);
            }
            else//踩到了關卡
            {
                switch (Maps[PlayerPos[playerNumber]])//0 1 2 3 4
                {
                    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 = 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格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerPos[1 - playerNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1 - playerNumber] -= 6;
                                ChangPos();
                                Console.WriteLine("玩家{0}退了6格", PlayerPos[1 - playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能輸入1或者2 1---交換位置  2---轟炸對方");
                                input = Console.ReadLine();
                            }
                        }
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暫停,休息一回合", PlayerNames[playerNumber]);
                        Falgs[playerNumber] = true;
                        Console.ReadKey(true);
                        break;

                    case 4:
                        Console.WriteLine("玩家{0}踩到了時空隧道,前進10格", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber]  = 10;
                        ChangPos();
                        break;
                }//swith結尾
            }//else結尾
            ChangPos();
            Console.Clear();
            DrawMap();
        }

玩遊戲邏輯

代码语言:javascript复制
public static void PalyGame(int playerNumber)
        {
            Random r = new Random();
            int rNumber = r.Next(1, 7);//產生1-6的隨機數
            Console.WriteLine(" ");
            Console.WriteLine("{0}按任意鍵開始擲骰子", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}擲出了{1}", PlayerNames[playerNumber], rNumber);
            PlayerPos[playerNumber]  = rNumber;
            ChangPos();
            Console.ReadKey(true);
            Console.WriteLine("{0}按任意鍵開始移動", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}移動完成", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            //玩家A可能踩到了玩家B 方塊 幸運羅盤 地雷 暫停 時空隧道
            if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退六格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[playerNumber]);
                PlayerPos[1 - playerNumber] -= 6;
                ChangPos();
                Console.ReadKey(true);
            }
            else//踩到了關卡
            {
                switch (Maps[PlayerPos[playerNumber]])//0 1 2 3 4
                {
                    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 = 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格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerPos[1 - playerNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1 - playerNumber] -= 6;
                                ChangPos();
                                Console.WriteLine("玩家{0}退了6格", PlayerPos[1 - playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能輸入1或者2 1---交換位置  2---轟炸對方");
                                input = Console.ReadLine();
                            }
                        }
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暫停,休息一回合", PlayerNames[playerNumber]);
                        Falgs[playerNumber] = true;
                        Console.ReadKey(true);
                        break;

                    case 4:
                        Console.WriteLine("玩家{0}踩到了時空隧道,前進10格", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber]  = 10;
                        ChangPos();
                        break;
                }//swith結尾
            }//else結尾
            ChangPos();
            Console.Clear();
            DrawMap();
        }

當玩家坐標發生改變的時候調用

代码语言:javascript复制
public static void ChangPos()
        {
            if (PlayerPos[0] < 0)
            {
                PlayerPos[0] = 0;
            }
            if (PlayerPos[0] >= 99)
            {
                PlayerPos[0] = 99;
            }
            if (PlayerPos[1] < 0)
            {
                PlayerPos[1] = 0;
            }
            if (PlayerPos[1] >= 99)
            {
                PlayerPos[1] = 99;
            }
        }

0 人点赞