C#實現簡單的飛行棋小遊戲

本文實例為大傢分享瞭C#實現簡單飛行棋小遊戲的具體代碼,供大傢參考,具體內容如下

1.玩傢姓名的輸入

2.對屏幕進行清屏

3.初始化地圖

4.玩傢A和玩傢B玩遊戲

using System;
namespace homework
{
    class data
    {
        //靜態字段來模擬全局變量
        static int[] Maps = new int[100];
        //聲明一個靜態數組用來存儲玩傢A跟玩傢B的坐標
        static int[] PlayerPos = new int[2];
        static string[] PlayerNames = new string[2];
        //兩個玩傢的標記
        static bool[] Flags = new bool[2];//flags[0]默認是false 
        public 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[0] == "")
                {
                    Console.WriteLine("玩傢A的姓名不能為空,請重新輸入");
                    PlayerNames[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("玩傢B的姓名不能與玩傢A相同");
                    PlayerNames[1] = Console.ReadLine();
                }
            }
            #endregion
            //對屏幕進行清屏
            #region 
            Console.Clear();//清屏
            GameShow();
            Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
            Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
            #endregion
            //畫地圖之前 初始化地圖
            #region 
            InitailMap();
            DrawMap();
            #endregion
            //當玩傢A跟玩傢B沒有一個人在終點時候 兩個玩傢不停地去玩遊戲
            #region 
            while (PlayerPos[0] < 99 && PlayerPos[0] < 99)
            {
                if (Flags[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    Flags[0] = false;
                }
                if (PlayerPos[0] > 99)
                {
                    Console.WriteLine("玩傢{0}贏瞭玩傢{1}", PlayerNames[0], PlayerNames[1]);
                    break;
                }
                if (Flags[1] == false)
                {
                    PlayGame(1);
                }
                else
                {
                    Flags[1] = false;
                }
                if (PlayerPos[0] > 99)
                {
                    Console.WriteLine("玩傢{0}贏瞭玩傢{1}", PlayerNames[0], PlayerNames[1]);
                    break;
                }
            }//while
            #endregion
            Console.WriteLine("遊戲結束!!!");
            Console.ReadLine();
        }
        //畫地圖頭
        #region 
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("*************************************");
            Console.WriteLine("*************************************");
            Console.WriteLine("****************飛行棋***************");
            Console.WriteLine("*************************************");
            Console.WriteLine("*************************************");
        }
        #endregion
        //初始化地圖
        #region 
        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 = { 2,3,4,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;
            }
        }
        #endregion
        //畫地圖
        #region 
        public static void DrawMap()
        {
            Console.WriteLine("圖例:幸運圓圈:○  地雷:☆  暫停:▽  時空隧道:◎");
            //第一橫行
            for (int i = 0; i < 30; i++)
            {
                //如果玩傢A跟玩傢B的坐標相同,畫一個尖括號
                Console.Write(DrawStringMap(i));
            }//for
            Console.WriteLine();
            // 第一豎行
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j <= 28; j++)
                {
                    Console.Write("  ");
                }
                Console.WriteLine(DrawStringMap(i));
            }
            //第二橫行
            for (int i = 64; i >= 35; i--)
            {
                Console.Write(DrawStringMap(i));
            }
            //畫第二行 需要進行換行
            Console.WriteLine();
            //第二豎行
            for (int i = 65; i <= 69; i++)
            {
                Console.WriteLine(DrawStringMap(i));
            }
            //第三橫行
            for (int i = 70; i <= 99; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
        }
        #endregion
        //畫地圖中抽象出一個方法
        #region
        public static string DrawStringMap(int i)
        {
            string str = "";
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == i)
            {
                str = "<>";
            }
            else if (PlayerPos[0] == i)
            {
                str = "A";
            }
            else if (PlayerPos[1] == i)
            {
                str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0: str = "□"; break;
                    case 1: str = "○"; break;
                    case 2: str = "☆"; break;
                    case 3: str = "▽"; break;
                    case 4: str = "◎"; break;
                }//switch
            }//else
            return str;
        }
        #endregion
        //雙方擲骰子過程
        #region 
        public static void PlayGame(int playerNumber)
        {
            Random r = new Random();
            int rNumber = r.Next(1, 7);
            Console.WriteLine("{0}按人任意鍵開始擲骰子", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}擲骰子擲出瞭{1}", PlayerNames[playerNumber], rNumber);
            PlayerPos[playerNumber] += rNumber;
            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},玩傢{1}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
                PlayerPos[1 - playerNumber] -= 6;
                Console.ReadKey(true);
            }
            else//踩到關卡
            {
                //玩傢坐標
                switch (Maps[PlayerPos[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 = 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},玩傢{1}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1] -= 6;
                                Console.WriteLine("玩傢{0}退瞭6格", PlayerNames[1 - playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("隻能輸入1或者2 1--交換位置  2--轟炸對方");
                                input = Console.ReadLine();
                            }
                        }
                        Console.ReadKey(true);
                        break;
                    case 2:
                        Console.WriteLine("玩傢{0}踩到瞭地雷,退6格", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[1 - playerNumber] -= 6;
                        Console.WriteLine("玩傢{0}退瞭6格", PlayerNames[playerNumber]);
                        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]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber] += 10;
                        break;
                }//switch
            }//else
            ChangePos();
            Console.Clear();
            DrawMap();
        }
        #endregion
        //當玩傢坐標發生改變需要判斷兩者是否超出范圍
        #region 
        public static void ChangePos()
        {
            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;
            }
        }
        #endregion
    }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: