欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)經(jīng)典飛行棋游戲的示例代碼

 更新時(shí)間:2022年02月07日 16:26:01   作者:ymz123_  
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)經(jīng)典的飛行棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

效果展示

主函數(shù)  

? ? ?static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int w = 50;
? ? ? ? ? ? int h = 30;
? ? ? ? ? ? ConsoleInit(w, h);

? ? ? ? ? ? E_SceneType nowSceneType = E_SceneType.Begin;
? ? ? ? ? ? while (true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? switch (nowSceneType)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? case E_SceneType.Begin:
? ? ? ? ? ? ? ? ? ? ? ? Console.Clear();
? ? ? ? ? ? ? ? ? ? ? ? GameEndOrBegin(w, h, ref nowSceneType);
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case E_SceneType.Game:
? ? ? ? ? ? ? ? ? ? ? ? Console.Clear();
? ? ? ? ? ? ? ? ? ? ? ? GameScene(w, h, ref nowSceneType);
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case E_SceneType.End:
? ? ? ? ? ? ? ? ? ? ? ? Console.Clear();
? ? ? ? ? ? ? ? ? ? ? ? GameEndOrBegin(w, h, ref nowSceneType);
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

場(chǎng)景類型枚舉

  enum E_SceneType
    {
        Begin,
        Game,
        End,
    }

控制臺(tái)基礎(chǔ)設(shè)置

static void ConsoleInit(int w, int h)
? ? ? ? {

? ? ? ? ? ? //控制臺(tái)設(shè)置
? ? ? ? ? ? Console.CursorVisible = false;
? ? ? ? ? ? Console.SetWindowSize(w, h);
? ? ? ? ? ? Console.SetBufferSize(w, h);
? ? ? ? }

開始及結(jié)束場(chǎng)景邏輯

        static void GameEndOrBegin(int w, int h, ref E_SceneType nowSceneType)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(nowSceneType == E_SceneType.Begin ? w / 2 - 3 : w / 2 - 4, 8);
            Console.Write(nowSceneType == E_SceneType.Begin ? "飛行棋" : "游戲結(jié)束");
            
            //當(dāng)前選項(xiàng)的編號(hào)
            int count = 0;
            bool IsOver = false;

            while (true)
            {
                Console.SetCursorPosition(nowSceneType ==E_SceneType.Begin? w/2-4:w/2-5, 11);
                Console.ForegroundColor = count == 0 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write(nowSceneType == E_SceneType.Begin? "游戲開始":"回到主菜單");

                Console.SetCursorPosition(w/2-4, 13);
                Console.ForegroundColor = count == 1 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write("退出游戲");

                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.W:
                        --count;
                        if (count < 0)
                        {
                            count = 0;
                        }
                        break;
                    case ConsoleKey.S:
                        ++count;
                        if (count > 1)
                        {
                            count = 1;
                        }
                        break;
                    case ConsoleKey.J:
                        if(count == 0)
                        {
                            nowSceneType = nowSceneType ==E_SceneType.Begin? E_SceneType.Game:E_SceneType.Begin;
                            IsOver = true;
                        }
                        else
                        {
                            Environment.Exit(0);
                        }
                        break;
                }
                if (IsOver)
                    break;
            }

        }

游戲場(chǎng)景邏輯

        static void GameScene(int w, int h, ref E_SceneType nowSceneType)
        {
            DrawWall(w, h);
            Map map = new Map(14, 3, 80);
            map.Draw();

            Player player = new Player(0, E_Player_Type.Player);
            Player computer = new Player(0, E_Player_Type.Computer);
            DrawPlayer(map, player, computer);

            while (true)
            {
                if (PlayerRandomMove(w, h, ref player, ref computer, map, ref nowSceneType))
                {
                    break;
                }
                if (PlayerRandomMove(w, h, ref computer, ref player, map, ref nowSceneType))
                {
                    break;
                }
            }

        }

        static bool PlayerRandomMove(int w, int h, ref Player p, ref Player otherP, Map map, ref E_SceneType nowSceneType)
        {
            //之后的游戲邏輯
            //玩家扔色子邏輯
            //檢測(cè)輸入
            Console.ReadKey(true);
            //扔色子的邏輯
            bool isGameOver = RandomMove(w, h, ref p, ref otherP, map);
            //繪制地圖
            map.Draw();
            //繪制玩家
            DrawPlayer(map, p, otherP);
            //判斷是否要結(jié)束游戲
            if(isGameOver)
            {
                //卡住程序 讓玩家按任意鍵
                Console.ReadKey(true);
                nowSceneType = E_SceneType.End;
            }
            return isGameOver;
        }

固定打印的信息

        static void DrawWall(int w, int h)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            //橫著的墻
            for (int i = 0; i < w; i+=2)
            {
                //最上面一行
                Console.SetCursorPosition(i, 0);
                Console.Write("■");

                //中間一行
                Console.SetCursorPosition(i, h-6);
                Console.Write("■");
                Console.SetCursorPosition(i, h - 11);
                Console.Write("■");

                //最下面一行
                Console.SetCursorPosition(i, h-1);
                Console.Write("■");
            }

            //豎著的墻
            for(int i = 0; i < h; i++)
            {
                //左邊的墻
                Console.SetCursorPosition(0, i);
                Console.Write("■");

                //右邊的墻
                Console.SetCursorPosition(w-2, i);
                Console.Write("■");
            }

            Console.SetCursorPosition(2, h - 5);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("按任意鍵開始扔色子");

            Console.SetCursorPosition(2, h - 10);
            Console.Write("□:普通格子");

            Console.SetCursorPosition(2, h - 9);
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("■:暫停,一回合不動(dòng)");

            Console.SetCursorPosition(22,h - 9);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("●:炸彈,倒退5格");

            Console.SetCursorPosition(2, h - 8);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("×:時(shí)空隧道,隨機(jī)倒退,暫停,交換位置");

            Console.SetCursorPosition(2, h - 7);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("★:玩家  ");

            Console.SetCursorPosition(11, h - 7);
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write("▲:電腦  ");

            Console.SetCursorPosition(20, h - 7);
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("◎:玩家電腦重合");
        }

格子類型枚舉和格子結(jié)構(gòu)體  

    enum E_Grid_Type
    {
        Normal,
        Boom,
        Pause,
        Tunnel,
    }

    /// <summary>
    /// 位置信息結(jié)構(gòu)體
    /// </summary>
    struct Vector2
    {
        public int x;
        public int y;

        public Vector2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    struct Grid
    {
        //格子的類型
        public E_Grid_Type _type;
        //格子的位置
        public Vector2 pos;
        //構(gòu)造函數(shù)
        public Grid(int x, int y, E_Grid_Type type)
        {
            pos.x = x;
            pos.y = y;
            _type = type;
        }
        
        //畫一個(gè)格子
        public void Draw()
        {
            Console.SetCursorPosition(pos.x, pos.y);
            switch(_type)
            {
                case E_Grid_Type.Normal:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("□");
                    break;
                case E_Grid_Type.Boom:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("●");
                    break;
                case E_Grid_Type.Pause:
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("■");
                    break;
                case E_Grid_Type.Tunnel:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("×");
                    break;
            }
        }
    }

地圖結(jié)構(gòu)體

    struct Map
    {
        public Grid[] grids;
        public Map(int x, int y, int num)
        {
            grids = new Grid[num];
            int indexX = 0;
            int indexY = 0;
            int stepNum = 2;

            Random r = new Random();
            int randomNum;
            for(int i = 0; i < num; i++)
            {
                randomNum = r.Next(0, 101);
                if(randomNum < 85 || i == 0 || i == num - 1)
                {
                    //普通格子
                    grids[i]._type = E_Grid_Type.Normal;
                }
                else if(randomNum < 90 && randomNum >=85)
                {
                    //炸彈
                    grids[i]._type = E_Grid_Type.Boom;
                }
                else if(randomNum < 95 && randomNum >=90)
                {
                    //暫停
                    grids[i]._type = E_Grid_Type.Pause;
                }
                else
                {
                    //時(shí)空隧道
                    grids[i]._type = E_Grid_Type.Tunnel;
                }

                grids[i].pos = new Vector2(x, y);

                if(indexX == 10)
                {
                    y += 1;
                    indexY++;
                    if(indexY == 2)
                    {
                        indexX = 0;
                        indexY = 0;
                        stepNum = -stepNum;
                    }
                }
                else
                {
                    x += stepNum;
                    indexX++;
                }
            }
        }
        public void Draw()
        {
            for (int i = 0; i < grids.Length; i++)
            {
                grids[i].Draw();
            }
        }
    }

玩家和電腦結(jié)構(gòu)體

    enum E_Player_Type
    {
        Player,
        Computer,
    }

    struct Player
    {
        public E_Player_Type type;
        public int nowIndex;
        //是否暫停的標(biāo)識(shí)
        public bool isPause;

        public Player(int index, E_Player_Type type)
        {
            nowIndex = index;
            this.type = type;
            isPause = false;
        }
        
        public void Draw(Map mapInfo)
        {
            //從傳入的地圖中得到格子信息
            Grid grid = mapInfo.grids[nowIndex];
            Console.SetCursorPosition(grid.pos.x, grid.pos.y);
            switch(type)
            {
                case E_Player_Type.Player:
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.Write("★");
                    break;
                case E_Player_Type.Computer:
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.Write("▲");
                    break;
            }
        }
    }

繪制玩家

        static void DrawPlayer(Map map, Player player, Player computer)
        {
            //重合時(shí)
            if(player.nowIndex == computer.nowIndex)
            {
                //得到重合的位置
                Grid grid = map.grids[player.nowIndex];
                Console.SetCursorPosition(grid.pos.x, grid.pos.y);
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.Write("◎");
            }
            //不重合時(shí)
            else
            {
                player.Draw(map);
                computer.Draw(map);
            }
        }

扔骰子邏輯

        //擦除提示的函數(shù)
        static void ClearInfo(int h)
        {
            Console.SetCursorPosition(2, h - 5);
            Console.Write("                                             ");
            Console.SetCursorPosition(2, h - 4);
            Console.Write("                                             ");
            Console.SetCursorPosition(2, h - 3);
            Console.Write("                                             ");
            Console.SetCursorPosition(2, h - 2);
            Console.Write("                                             ");
        }

        /// <summary>
        /// 扔色子函數(shù)
        /// </summary>>
        /// <param name="w">窗口的寬</param>
        /// <param name="h">窗口的高</param>
        /// <param name="p">扔色子的對(duì)象</param>
        /// <param name="map">地圖信息</param>
        /// <returns>默認(rèn)返回false 代表沒有結(jié)束</returns>
        static bool RandomMove(int w, int h, ref Player p, ref Player otherP, Map map)
        {
            //擦除之前顯示的提示信息
            ClearInfo(h);
            //根據(jù)扔色子的玩家類型,決定信息的顏色
            Console.ForegroundColor = p.type == E_Player_Type.Player ? ConsoleColor.Cyan : ConsoleColor.Magenta;

            //扔色子之前判斷玩家是否處于暫停狀態(tài)
            if(p.isPause)
            {
                Console.SetCursorPosition(2, h - 5);
                Console.Write("處于暫停狀態(tài),{0}需要暫停一回合", p.type == E_Player_Type.Player ? "你" : "電腦");
                Console.SetCursorPosition(2, h - 4);
                Console.Write("請(qǐng)按任意鍵,讓{0}開始扔色子", p.type == E_Player_Type.Player ? "電腦" : "你");
                //停止暫停
                p.isPause = false;
                return false;
            }

            //扔色子目的是改變玩家或電腦的位置 計(jì)算位置的變化
            //扔色子 隨機(jī)一個(gè)1到6的數(shù)字,加上去
            Random r = new Random();
            int randomNum = r.Next(1, 7);
            p.nowIndex += randomNum;

            //打印扔的點(diǎn)數(shù)
            Console.SetCursorPosition(2, h - 5);
            Console.Write("{0}扔出的點(diǎn)數(shù)為:{1}", p.type == E_Player_Type.Player ? "你" : "電腦", randomNum);

            //首先判斷是否到終點(diǎn)了
            if(p.nowIndex >= map.grids.Length - 1)
            {
                p.nowIndex = map.grids.Length - 1;
                Console.SetCursorPosition(2, h - 4);
                if(p.type == E_Player_Type.Player)
                {
                    Console.Write("恭喜你,率先到達(dá)了終點(diǎn)");
                }
                else
                {
                    Console.Write("很遺憾,電腦率先到達(dá)了終點(diǎn)");
                }
                Console.SetCursorPosition(2, h - 3);
                Console.Write("請(qǐng)按任意鍵結(jié)束");
                return true;
            }
            else
            {
                //沒有到終點(diǎn) 就判斷當(dāng)前對(duì)象到了一個(gè)什么類型的格子
                Grid grid = map.grids[p.nowIndex];

                switch(grid._type)
                {
                    case E_Grid_Type.Normal:
                        Console.SetCursorPosition(2, h - 4);
                        Console.Write("{0}到達(dá)了一個(gè)安全位置", p.type == E_Player_Type.Player ? "你" : "電腦");
                        Console.SetCursorPosition(2, h - 3);
                        Console.Write("請(qǐng)按任意鍵,讓{0}開始扔色子", p.type == E_Player_Type.Player ? "電腦" : "你");
                        break;
                    case E_Grid_Type.Boom:
                        p.nowIndex -= 5;
                        if(p.nowIndex < 0)
                        {
                            p.nowIndex = 0;
                        }
                        Console.SetCursorPosition(2, h - 4);
                        Console.Write("{0}踩到了炸彈,退后5格", p.type == E_Player_Type.Player ? "你" : "電腦");
                        Console.SetCursorPosition(2, h - 3);
                        Console.Write("請(qǐng)按任意鍵,讓{0}開始扔色子", p.type == E_Player_Type.Player ? "電腦" : "你");
                        break;
                    case E_Grid_Type.Pause:
                        p.isPause = true;
                        Console.SetCursorPosition(2, h - 4);
                        Console.Write("{0}到達(dá)了暫停點(diǎn),你需要暫停一回合", p.type == E_Player_Type.Player ? "你" : "電腦");
                        Console.SetCursorPosition(2, h - 3);
                        Console.Write("請(qǐng)按任意鍵,讓{0}開始扔色子", p.type == E_Player_Type.Player ? "電腦" : "你");
                        break;
                    case E_Grid_Type.Tunnel:
                        Console.SetCursorPosition(2, h - 4);
                        Console.Write("{0}踩到了時(shí)空隧道", p.type == E_Player_Type.Player ? "你" : "電腦");

                        //隨機(jī)
                        randomNum = r.Next(1, 91);
                        if(randomNum <= 30)
                        {
                            //倒退
                            p.nowIndex -= 5;
                            if(p.nowIndex < 0)
                            {
                                p.nowIndex = 0;
                            }
                            Console.SetCursorPosition(2, h - 5);
                            Console.Write("觸發(fā)倒退5格");
                        }
                        else if(randomNum <= 60)
                        {
                            p.isPause = true;
                            Console.SetCursorPosition(2, h - 3);
                            Console.Write("觸發(fā)暫停一回合");
                        }
                        else
                        {
                            int tmp = p.nowIndex;
                            p.nowIndex = otherP.nowIndex;
                            otherP.nowIndex = tmp;
                            Console.SetCursorPosition(2, h - 3);
                            Console.Write("驚喜,雙方交換位置");
                        }

                        Console.SetCursorPosition(2, h - 2);
                        Console.Write("請(qǐng)按任意鍵,讓{0}開始扔色子", p.type == E_Player_Type.Player ? "電腦" : "你");
                        break;
                }
            }

            //默認(rèn)沒有結(jié)束
            return false;
        }

以上就是C#實(shí)現(xiàn)經(jīng)典飛行棋游戲的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#飛行棋的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#零基礎(chǔ)開發(fā)中最重要的概念總結(jié)

    C#零基礎(chǔ)開發(fā)中最重要的概念總結(jié)

    這篇文章主要為大家詳細(xì)介紹了C#零基礎(chǔ)開發(fā)中最重要的一些概念,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,需要的可以參考一下
    2023-02-02
  • C#利用Task實(shí)現(xiàn)任務(wù)超時(shí)多任務(wù)一起執(zhí)行的方法

    C#利用Task實(shí)現(xiàn)任務(wù)超時(shí)多任務(wù)一起執(zhí)行的方法

    這篇文章主要給大家介紹了關(guān)于C#利用Task實(shí)現(xiàn)任務(wù)超時(shí),多任務(wù)一起執(zhí)行的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來(lái)一起看看吧。
    2017-12-12
  • C#使用StreamWriter寫入文件的方法

    C#使用StreamWriter寫入文件的方法

    這篇文章主要介紹了C#使用StreamWriter寫入文件的方法,涉及C#中StreamWriter類操作文件的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • C#添加、獲取、刪除PDF附件實(shí)例代碼

    C#添加、獲取、刪除PDF附件實(shí)例代碼

    這篇文章主要介紹了如何在C#添加、獲取、刪除PDF附件,文中代碼非常詳細(xì),快來(lái)和小編一起學(xué)習(xí)吧
    2020-05-05
  • .NET C#利用ZXing生成、識(shí)別二維碼/條形碼

    .NET C#利用ZXing生成、識(shí)別二維碼/條形碼

    ZXing是一個(gè)開放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),它包含了聯(lián)系到其他語(yǔ)言的端口。這篇文章主要給大家介紹了.NET C#利用ZXing生成、識(shí)別二維碼/條形碼的方法,文中給出了詳細(xì)的示例代碼,有需要的朋友們可以參考借鑒。
    2016-12-12
  • c#實(shí)現(xiàn)適配器模式的項(xiàng)目實(shí)踐

    c#實(shí)現(xiàn)適配器模式的項(xiàng)目實(shí)踐

    適配器模式將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另一個(gè)接口,使得原本由于接口不兼容而不能一起工作的那些類可以一起工作,本文主要介紹了c#實(shí)現(xiàn)適配器模式的項(xiàng)目實(shí)踐,感興趣的可以一起來(lái)了解一下
    2023-08-08
  • 三十分鐘快速掌握C# 6.0知識(shí)點(diǎn)

    三十分鐘快速掌握C# 6.0知識(shí)點(diǎn)

    這篇文章主要介紹了C# 6.0的相關(guān)知識(shí)點(diǎn),文中介紹的非常詳細(xì),通過(guò)這篇文字可以讓大家在三十分鐘內(nèi)快速的掌握C# 6.0,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-03-03
  • C#圖像亮度調(diào)式與偽彩色圖的處理教程(推薦)

    C#圖像亮度調(diào)式與偽彩色圖的處理教程(推薦)

    下面小編就為大家推薦一篇C#圖像亮度調(diào)式與偽彩色圖的處理教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • C#支付寶掃碼支付代碼完整版

    C#支付寶掃碼支付代碼完整版

    這篇文章主要為大家詳細(xì)介紹了C#支付寶掃碼支付代碼的完整版本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • C#操作SQLite方法實(shí)例詳解

    C#操作SQLite方法實(shí)例詳解

    這篇文章主要介紹了C#操作SQLite方法,以實(shí)例形式詳細(xì)分析了C#操作SQLite的連接、查詢、插入、修改等相關(guān)技巧,需要的朋友可以參考下
    2015-06-06

最新評(píng)論