飞行棋

代码
using System;
using System.Runtime.InteropServices.Marshalling;
using System.Security.Cryptography.X509Certificates;

namespace 实践
{
    enum E_SceneType
    {
        Begin,
        Game,
        End,
    }
    enum E_Grid_Type
    {
        Normal,
        Bomb,
        Pause,
        Tunnel,
    }
    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;
        //外部传入的时候位置包含x,y,所以需要三个变量,而直接用Vector pos我现在不知道怎么传入变量
        public Grid (E_Grid_Type type,int x,int y )
        {
            this.type = type;
            pos.x = x;
            pos.y = y;
        }
        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.Bomb:
                    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.DarkYellow;
                    Console.Write("※");
                    break;
            }
        }
        
    }
    struct Map
    {
        public Grid[] grids;
        public Map(int x ,int y ,int num)
        {
            grids = new Grid[num];
            int indexX = 0, indexY = 0, xStep = 2;
            for(int i = 0; i < num; i++)
            {
                Random r = new Random();
                int prop = r.Next(0,101);
                //首尾格子必须是安全格子
                if (prop < 80||i==0||i==num-1)
                {
                    grids[i].type =E_Grid_Type.Normal;
                }
                else if (prop >= 80&&prop<90)
                {
                    grids[i].type = E_Grid_Type.Bomb;
                }
                else if (prop >=90&&prop<95)
                {
                    grids[i].type = E_Grid_Type.Tunnel;
                }
                else
                {
                    grids[i].type = E_Grid_Type.Pause;
                }
                //确定格子位置
                grids[i].pos=new Vector2 (x,y);
                if (indexX == 10)
                {
                    y++;
                    indexY++;
                    if (indexY == 2)
                    {
                        indexY = 0;
                        indexX = 0;
                        xStep=-xStep;
                        //indexY也需要归零
                    }
                }
                else 
                {
                    //用步长来处理
                    x += xStep;
                    indexX++;
                }
            }
        }
        public void Draw()
        {
            for(int i = 0;i<grids.Length;i++)
            {
                grids[i].Draw();
            }
        }
    }
    enum E_PlayerType
    {
        Player,
        Com,
    }
    struct Player
    {
        public E_PlayerType type;
        //用地图索引来确定玩家位置
        public int nowIndex;
        public bool isPause;
        public Player(int index,E_PlayerType type)
        {
            this.type = type;
            nowIndex = index;
            this.isPause = false;
        }
        //设置位置,但是是通过地图索引得到位置,所以必须要先得到地图,函数方法中可传入需要的参数
        public void Draw(Map mapInfo)
        {
            Grid grid = mapInfo.grids[nowIndex];
            Console.SetCursorPosition(grid.pos.x,grid.pos.y);
            switch (type)
            {
                case E_PlayerType.Player:
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.Write("☆");
                    break;
                case E_PlayerType.Com:
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.Write("▲");
                    break;
            }
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            int w = 50, h = 32;
            Console.CursorVisible=false;
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w, h);
            E_SceneType nowGame = E_SceneType.Begin;
            while (true)
            {
                switch (nowGame)
                {
                    case E_SceneType.Begin:
                        BeginS(w, h, ref nowGame);
                        break;
                    case E_SceneType.Game:
                        GameS(w, h, ref nowGame);
                        break;
                    case E_SceneType.End:
                        EndS(w, h, ref nowGame);
                        break;
                }
            }

        }
        //开始场景逻辑
        static void BeginS(int w,int h ,ref E_SceneType nowGame)
        {
            Console.Clear();
            int beginSelect = 0;
            bool isQuitBegin=false;
            while (true)
            {
                if (isQuitBegin)
                {
                    break;
                }
                Console.SetCursorPosition(w / 2 - 3, 5);
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("飞行棋");
                Console.SetCursorPosition(w / 2 - 4, 10);
                Console.ForegroundColor = beginSelect == 0 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write("开始游戏");
                Console.ForegroundColor = beginSelect == 1 ? ConsoleColor.Red : ConsoleColor.White;
                Console.SetCursorPosition(w / 2 - 4, 12);
                Console.Write("退出游戏");
                //Console.ReadKey(true).Key+ConsoleKey.W可以直接检测W键输入,无论大小写
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.W:
                        beginSelect--;
                        if(beginSelect < 0)
                        {
                            beginSelect = 0;
                        }
                        break;
                    case ConsoleKey.S:
                        beginSelect++;
                        if (beginSelect>1)
                        {
                            beginSelect = 1;
                        }
                        break;
                    case ConsoleKey.J:
                        if(beginSelect==0)
                        {
                            nowGame = E_SceneType.Game;
                            isQuitBegin = true;
                        }
                        else
                        {
                            Environment.Exit(0);
                        }
                        break;
                        
                }
            }
        }
        //游戏场景逻辑
        static void DrawWall(int w, int h)
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Red;
            for (int i = 0; i < w; i+=2)
            {
                Console.SetCursorPosition(i, 0);
                Console.Write("■");
                Console.SetCursorPosition(i, h-1);
                Console.Write("■");
                Console.SetCursorPosition(i, h - 5);
                Console.Write("■");
                Console.SetCursorPosition(i, h - 9);
                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-8);
            Console.ForegroundColor= ConsoleColor.White;
            Console.Write("□:安全位置");
            Console.SetCursorPosition(17, h - 8);
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("||:时空裂缝,暂停一回合");
            Console.SetCursorPosition(2, h - 7);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("×:炸弹,倒退五格");
            Console.SetCursorPosition(2, h - 6);
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write("※:隧道,交换双方位置");
            Console.SetCursorPosition(23, h - 7);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("☆:玩家");
            Console.SetCursorPosition(33, h - 7);
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write("▲:电脑");
            Console.SetCursorPosition(26, h - 6);
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.Write("★:双方重合");
        }
        static void DrawPlayer(Player player,Player com,Map map)
        {
            if (player.nowIndex == com.nowIndex)
            {
                Grid grid = map.grids[com.nowIndex];
                Console.SetCursorPosition(grid.pos.x,grid.pos.y);
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.Write("★");
            }
            else
            {
                player.Draw(map);
                com.Draw(map);
            }
        }
        static bool RollDice(int w,int h,ref Player p1,ref Player p2,Map map)
        {
            if (p1.isPause)
            {
                Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
                Console.SetCursorPosition(2, h - 4);
                Console.Write("{0}受到了时空裂缝的影响,暂停一回合", p1.type == E_PlayerType.Player ? "你" : "电脑");
                Console.SetCursorPosition(2, h - 3);
                Console.Write("按任意键继续");
                p1.isPause = false;
                return false;
            }
            Random r= new Random();
            int dice = r.Next(1,7);
            p1.nowIndex += dice;
            Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
            Console.SetCursorPosition(2, h - 4);
            Console.Write("{0}掷出了{1}点",p1.type== E_PlayerType.Player?"你":"电脑",dice);
            if (p1.nowIndex >= map.grids.Length-1)
            {
                p1.nowIndex = map.grids.Length-1;
                if(p1.type== E_PlayerType.Player)
                {
                    Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
                    Console.SetCursorPosition(2, h - 4);
                    Console.Write("恭喜你率先到达终点");
                    Console.SetCursorPosition(2, h - 3);
                    Console.Write("按任意键退出游戏");
                }
                else
                {
                    Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
                    Console.SetCursorPosition(2, h - 4);
                    Console.Write("很遗憾,你输掉了游戏");
                    Console.SetCursorPosition(2, h - 3);
                    Console.Write("按任意键退出游戏");
                }
                return true;
            }
            else
            {
                Grid grid=map.grids[p1.nowIndex];
                switch (grid.type)
                {
                    case E_Grid_Type.Normal:
                        Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
                        Console.SetCursorPosition(2, h - 3);
                        Console.Write("{0}到达了安全区域", p1.type == E_PlayerType.Player ? "你" : "电脑");
                        Console.SetCursorPosition(2, h - 2);
                        Console.Write("按任意键继续");
                        break;
                    case E_Grid_Type.Bomb:
                        p1.nowIndex -= 5;
                        if (p1.nowIndex <= 0)
                        {
                            p1.nowIndex = 0;
                        }
                        Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
                        Console.SetCursorPosition(2, h - 3);
                        Console.Write("{0}踩到了炸弹,倒退五格", p1.type == E_PlayerType.Player ? "你" : "电脑");
                        Console.SetCursorPosition(2, h - 2);
                        Console.Write("按任意键继续");
                        break;
                    case E_Grid_Type.Pause:
                        p1.isPause = true;
                        Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
                        Console.SetCursorPosition(2, h - 3);
                        Console.Write("{0}受到了时空裂缝的影响", p1.type == E_PlayerType.Player ? "你" : "电脑");
                        Console.SetCursorPosition(2, h - 2);
                        Console.Write("按任意键继续");
                        break;
                    case E_Grid_Type.Tunnel:
                        int temp = p1.nowIndex;
                        p1.nowIndex = p2.nowIndex;
                        p2.nowIndex = temp;
                        Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
                        Console.SetCursorPosition(2, h - 3);
                        Console.Write("{0}进入了隧道,双方位置交换", p1.type == E_PlayerType.Player ? "你" : "电脑");
                        Console.SetCursorPosition(2, h - 2);
                        Console.Write("按任意键继续");
                        break;
                }
            }
            return false;
        }
        static void InfoClear(int w ,int h)
        {
            Console.SetCursorPosition(2, h - 2);
            Console.Write("                                               ");
            Console.SetCursorPosition(2,h-3);
            Console.Write("                                               ");
            Console.SetCursorPosition(2, h - 4);
            Console.Write("                                               ");
        }
        static void GameS(int w ,int h,ref E_SceneType nowGame)
        {
            Console.Clear();
            DrawWall(w, h);
            Map map = new Map(14, 4, 80);
            map.Draw();
            Player player = new Player(0, E_PlayerType.Player);
            Player com = new Player(0, E_PlayerType.Com);
            DrawPlayer(player, com, map);
            bool isGameOver = false;
            while (true)
            {
                Console.ReadKey(true);
                InfoClear(w, h);
                isGameOver = RollDice(w, h, ref player, ref com, map);
                map.Draw();
                DrawPlayer(player, com, map);
                if (isGameOver)
                {
                    Console.ReadKey(true);
                    nowGame = E_SceneType.End;
                    break;
                }
                Console.ReadKey(true);
                InfoClear(w, h);
                isGameOver = RollDice(w, h, ref com, ref player, map);
                map.Draw();
                DrawPlayer(player, com, map);
                if (isGameOver)
                {
                    Console.ReadKey(true);
                    nowGame = E_SceneType.End;
                    break;
                }
            }
        }
        static void EndS(int w, int h, ref E_SceneType nowGame)
        {
            Console.Clear();
            int endSelect = 0;
            bool isQuitEnd = false;
            while (true)
            {
                if (isQuitEnd)
                {
                    break;
                }
                Console.SetCursorPosition(w / 2 - 4, 5);
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("游戏结束");
                Console.SetCursorPosition(w / 2 - 4, 10);
                Console.ForegroundColor = endSelect == 0 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write("重新开始");
                Console.ForegroundColor = endSelect == 1 ? ConsoleColor.Red : ConsoleColor.White;
                Console.SetCursorPosition(w / 2 - 4, 12);
                Console.Write("退出游戏");
                //Console.ReadKey(true).Key+ConsoleKey.W可以直接检测W键输入,无论大小写
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.W:
                        endSelect--;
                        if (endSelect < 0)
                        {
                            endSelect = 0;
                        }
                        break;
                    case ConsoleKey.S:
                        endSelect++;
                        if (endSelect > 1)
                        {
                            endSelect = 1;
                        }
                        break;
                    case ConsoleKey.J:
                        if (endSelect == 0)
                        {
                            nowGame = E_SceneType.Begin;
                            isQuitEnd = true;
                        }
                        else
                        {
                            Environment.Exit(0);
                        }
                        break;

                }
            }
        }
    }
}
posted @   cannedmint  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示