C#实现经典飞行棋游戏的示例代码

目录
  • 效果展示
  • 主函数
  • 场景类型枚举
  • 控制台基础设置
  • 开始及结束场景逻辑
  • 游戏场景逻辑
    • 固定打印的信息
    • 格子类型枚举和格子结构体
    • 地图结构体
    • 玩家和电脑结构体
    • 绘制玩家
    • 扔骰子逻辑

效果展示

主函数

     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;
                }

            }

        }

场景类型枚举

  enum E_SceneType
    {
        Begin,
        Game,
        End,
    }

控制台基础设置

static void ConsoleInit(int w, int h)
        {

            //控制台设置
            Console.CursorVisible = false;
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w, h);
        }

开始及结束场景逻辑

        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 ? "飞行棋" : "游戏结束");

            //当前选项的编号
            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;
            }

        }

游戏场景逻辑

        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)
        {
            //之后的游戏逻辑
            //玩家扔色子逻辑
            //检测输入
            Console.ReadKey(true);
            //扔色子的逻辑
            bool isGameOver = RandomMove(w, h, ref p, ref otherP, map);
            //绘制地图
            map.Draw();
            //绘制玩家
            DrawPlayer(map, p, otherP);
            //判断是否要结束游戏
            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("■:暂停,一回合不动");

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

            Console.SetCursorPosition(2, h - 8);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("×:时空隧道,随机倒退,暂停,交换位置");

            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("◎:玩家电脑重合");
        }

格子类型枚举和格子结构体

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

    /// <summary>
    /// 位置信息结构体
    /// </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;
        //构造函数
        public Grid(int x, int y, E_Grid_Type type)
        {
            pos.x = x;
            pos.y = y;
            _type = type;
        }

        //画一个格子
        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;
            }
        }
    }

地图结构体

    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
                {
                    //时空隧道
                    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();
            }
        }
    }

玩家和电脑结构体

    enum E_Player_Type
    {
        Player,
        Computer,
    }

    struct Player
    {
        public E_Player_Type type;
        public int nowIndex;
        //是否暂停的标识
        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)
        {
            //重合时
            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("◎");
            }
            //不重合时
            else
            {
                player.Draw(map);
                computer.Draw(map);
            }
        }

扔骰子逻辑

        //擦除提示的函数
        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>
        /// 扔色子函数
        /// </summary>>
        /// <param name="w">窗口的宽</param>
        /// <param name="h">窗口的高</param>
        /// <param name="p">扔色子的对象</param>
        /// <param name="map">地图信息</param>
        /// <returns>默认返回false 代表没有结束</returns>
        static bool RandomMove(int w, int h, ref Player p, ref Player otherP, Map map)
        {
            //擦除之前显示的提示信息
            ClearInfo(h);
            //根据扔色子的玩家类型,决定信息的颜色
            Console.ForegroundColor = p.type == E_Player_Type.Player ? ConsoleColor.Cyan : ConsoleColor.Magenta;

            //扔色子之前判断玩家是否处于暂停状态
            if(p.isPause)
            {
                Console.SetCursorPosition(2, h - 5);
                Console.Write("处于暂停状态,{0}需要暂停一回合", p.type == E_Player_Type.Player ? "你" : "电脑");
                Console.SetCursorPosition(2, h - 4);
                Console.Write("请按任意键,让{0}开始扔色子", p.type == E_Player_Type.Player ? "电脑" : "你");
                //停止暂停
                p.isPause = false;
                return false;
            }

            //扔色子目的是改变玩家或电脑的位置 计算位置的变化
            //扔色子 随机一个1到6的数字,加上去
            Random r = new Random();
            int randomNum = r.Next(1, 7);
            p.nowIndex += randomNum;

            //打印扔的点数
            Console.SetCursorPosition(2, h - 5);
            Console.Write("{0}扔出的点数为:{1}", p.type == E_Player_Type.Player ? "你" : "电脑", randomNum);

            //首先判断是否到终点了
            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("恭喜你,率先到达了终点");
                }
                else
                {
                    Console.Write("很遗憾,电脑率先到达了终点");
                }
                Console.SetCursorPosition(2, h - 3);
                Console.Write("请按任意键结束");
                return true;
            }
            else
            {
                //没有到终点 就判断当前对象到了一个什么类型的格子
                Grid grid = map.grids[p.nowIndex];

                switch(grid._type)
                {
                    case E_Grid_Type.Normal:
                        Console.SetCursorPosition(2, h - 4);
                        Console.Write("{0}到达了一个安全位置", p.type == E_Player_Type.Player ? "你" : "电脑");
                        Console.SetCursorPosition(2, h - 3);
                        Console.Write("请按任意键,让{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("请按任意键,让{0}开始扔色子", p.type == E_Player_Type.Player ? "电脑" : "你");
                        break;
                    case E_Grid_Type.Pause:
                        p.isPause = true;
                        Console.SetCursorPosition(2, h - 4);
                        Console.Write("{0}到达了暂停点,你需要暂停一回合", p.type == E_Player_Type.Player ? "你" : "电脑");
                        Console.SetCursorPosition(2, h - 3);
                        Console.Write("请按任意键,让{0}开始扔色子", p.type == E_Player_Type.Player ? "电脑" : "你");
                        break;
                    case E_Grid_Type.Tunnel:
                        Console.SetCursorPosition(2, h - 4);
                        Console.Write("{0}踩到了时空隧道", p.type == E_Player_Type.Player ? "你" : "电脑");

                        //随机
                        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("触发倒退5格");
                        }
                        else if(randomNum <= 60)
                        {
                            p.isPause = true;
                            Console.SetCursorPosition(2, h - 3);
                            Console.Write("触发暂停一回合");
                        }
                        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("请按任意键,让{0}开始扔色子", p.type == E_Player_Type.Player ? "电脑" : "你");
                        break;
                }
            }

            //默认没有结束
            return false;
        }

以上就是C#实现经典飞行棋游戏的示例代码的详细内容,更多关于C#飞行棋的资料请关注我们其它相关文章!

(0)

相关推荐

  • C#控制台实现简单飞行棋游戏

    本文实例为大家分享了C#控制台实现简单飞行棋游戏的具体代码,供大家参考,具体内容如下 需求分析 1.制作游戏头部:游戏头部介绍 2.绘制地图 使用一维数组装整个地图的路线 如果这个位置是0,绘制普通格子□ 如果这个位置是1,绘制幸运轮盘◎ 如果这个位置是2,绘制地雷★ 如果这个位置是3,绘制暂停▲ 如果这个位置是4,绘制时空隧道卍 规划幸运轮盘位置 int[] luckyturn = { 6, 23, 40, 55, 69, 83 }; 规划地雷的位置 int[] landMine = { 5,

  • C#实现飞行棋游戏

    飞行棋主要是讲的方法怎么应用,充分的去理解方法和方法的调用,整体收获还是很大的. 我想的是说一下整体的思路.在编程的时间里,逻辑是最重要的,先干嘛后干嘛,对吧. 直接上个飞行棋的图,大家看看. 先从哪里下手呢?画图!对先画图,画图先画表头,还是用流程图来吧 画图画好了,那么就是怎么掷骰子,然后不用的样式的格子都是什么功能. 这个实现功能,是我认为最麻烦的,一大技术难点. 下面是掷骰子的方法 简单来说就是用了一个大的 if else判断语句,来判断走到哪里,执行什么操作. public stati

  • C#实现winform版飞行棋

    本文实例为大家分享了C#实现winform版飞行棋的具体代码,供大家参考,具体内容如下 游戏界面 游戏规则: 1.两个人轮流掷骰子红人和绿人 2.投掷出2,4,6点出门,投掷出6点可以在出门后再次投掷行走 3.地图长度共100步 4.地图中除过普通地板之外,另设六种特殊功能地板 (1) 踩到香蕉皮,退6步 (2) 踩到时空,前进6步 (3) 踩到陷阱,暂停一回合 (4) 踩到星星,可以再投掷一次 (5) 踩到移魂大法,可以做出选择与对方互换位置 (6) 踩到手枪,可以击退对方3步 (7) 踩到大

  • C#实现控制台飞行棋小游戏

    本文实例为大家分享了C#实现控制台飞行棋小游戏的具体代码,供大家参考,具体内容如下 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace 飞行棋小游戏 { class Program { /// <summary> ///

  • C#学习笔记之飞行棋项目

    本文实例为大家分享了C#控制台实现飞行棋项目的具体代码,供大家参考,具体内容如下 飞行棋游戏介绍 此次编程实现的飞行棋和我们小时候玩的有些不一样,规则大致类似,但是我在学习过程中的飞行棋大大简化了过程:当前编写的程序适合双人玩(也可改成更多人),主要存在以下几种关卡: 幸运轮盘*:给玩家两种选择 1–交换位置:2–使另一玩家退6格 地雷$:玩家退6格 暂停&:玩家暂停一回合 时空隧道#:玩家前进10格 玩家互踩会使另一玩家退6格 玩家A和玩家B先到终点处获胜 飞行棋游戏编写 该游戏存在100个字

  • 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 str

  • C#控制台实现飞行棋小游戏

    本文实例为大家分享了C#控制台实现飞行棋小游戏的具体代码,供大家参考,具体内容如下 游戏标题 static void ShowTitle() { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(

  • C#实现简单的飞行棋游戏

    本文实例为大家分享了C#实现简单飞行棋游戏的具体代码,供大家参考,具体内容如下 下面展示 完整代码: namespace 飞行棋 { class Program { public static int[] Maps = new int[100]; public static int[] PlayerPos = new int[2]; public static string[] PlayerName = new string[2]; public static bool[] Flags = ne

  • C#实现经典飞行棋游戏的示例代码

    目录 效果展示 主函数 场景类型枚举 控制台基础设置 开始及结束场景逻辑 游戏场景逻辑 固定打印的信息 格子类型枚举和格子结构体 地图结构体 玩家和电脑结构体 绘制玩家 扔骰子逻辑 效果展示 主函数      static void Main(string[] args)         {             int w = 50;             int h = 30;             ConsoleInit(w, h);             E_SceneType

  • Java实现经典捕鱼达人游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 游戏窗体 鱼 鱼池类继承自Jpanel 总结 前言 <捕鱼达人>是一款以深海狩猎为题材的休闲竞技游戏.这是一场海底世界的远征,享受捕获大鱼的乐趣,但不是所有的鱼都是友善的,它们会用自己的方式保护自己,保卫属于自己的海底世界.当然,这里也是冒险与机遇共存的地方,诸多埋藏于海底的宝藏等待着被探寻. 游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 在鱼池中有很多鱼,鱼各自游动. 有一张渔网,随鼠标移动,点

  • C语言实现经典扫雷小游戏的示例代码

    目录 一.游戏简介 二.游戏实现 1.初始化棋盘 2.打印棋盘 3.布置雷 4.排查雷 三.源文件 1.game.h 2.game.c 3.Test.c 一.游戏简介 游戏初始界面有两个选择,选项“1”为开始游戏,选项“0”为退出游戏:选择开始游戏之后将会打印出9*9的棋盘,此时系统已经为游戏设置了10个雷,输入坐标之后将会打印出此坐标周围八个位置有几个雷,如果踩到了雷,那么游戏结束,打印出所有雷的位置. 二.游戏实现 1.初始化棋盘 void InitBoard(char board[ROWS

  • C#控制台实现飞行棋游戏

    本文实例为大家分享了C#实现飞行棋游戏的具体代码,供大家参考,具体内容如下 游戏截图: 管理类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class ProgramManage { //随机数 public static Random rd = new Ran

  • C/C++实现经典象棋游戏的示例代码

    目录 大体思路 效果展示 核心代码 大体思路 采用面相过程的设计方式实现,类似于我们平时做的课程设计,实现这样的小游戏无非就是多了图形处理库.这里使用的是acllib图形库. 设计这种小游戏,首先要从宏观上去认识:象棋,要有棋盘,要有棋子,棋子要移动. 对于棋盘,十行九列画出即可. 对于棋子,分黑红两方,按照指定位置画出. 如何移动棋子,我们有mouseEvent函数. 初始化棋盘棋子:initmap,initpaint 利用鼠标实现棋子移动分两步(mouseEvent): 第一次点击,记录点击

  • python实现飞行棋游戏

    本文实例为大家分享了python实现飞行棋的具体代码,供大家参考,具体内容如下 import random # 地图初始坐标 Maps = [0] *100 # 玩家A和玩家B的初始坐标 PlayerPos = [0]*2 # 存储玩家姓名 playerNames = [""] *2 # 俩个玩家行动的标记 Flags = [True]*2 # 封装一个不换行的print def print_end(num): print(num,end="") def games

  • c# 编写的简单飞行棋游戏

    目录 项目效果 实现代码 基于winform制作的图形界面程序 效果 代码 项目效果 实现代码 using System; namespace 飞行棋项目 { class Program { ///1.画游戏头 ///2.初始化地图 ///把整数数组中数字编程控制台中显示的特殊字符显示的过程,就是初始化地图 ///3.画地图 ///4.玩游戏 //我们用静态数组用来模拟全局变量,这个数组代表地图长度以及地图坐标 public static int[] Maps = new int[100]; /

  • JAVA实现经典扫雷游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 总结 前言 windows自带的游戏<扫雷>是陪伴了无数人的经典游戏,本程序参考<扫雷>的规则进行了简化,用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 1.要有难度等级,初级,中级,高级 2.由玩家逐个翻开方块,以找出所有地雷为最终游戏目标.如果玩家翻开的方块有地雷,则游戏结束 3.游戏主区域由很多个方格组成.使用鼠标左键随机点击一个方格,方格即被打开并显示出方格中的数字:方格中数字则表示其

随机推荐