首页 > 作文

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

更新时间:2023-04-04 22:30:46 阅读: 评论:0

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

效果展示

主函数

  static void main(string[] args)    {      int w = 50;      int h = 30;      consoleinit(w, h);      e_scenetype nowscenetype = e_scenetype.begin;      while (true)      {        switch (nowscenetype)        {          ca e_scenetype.begin:            console.clear();            gameendorbegin(w, h, ref nowscenetype);            break;          ca e_scenetype.game:            console.clear();            gamescene(w, h, ref nowscenetype);            break;          ca 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 = fal;      console.twindowsize(w, h);      console.tbuffersize(w, h);    }

开始及结束场景逻辑

        static void gameendorbegin(int w, int h, ref e_scenetype nowscenetype)        {            console.foregroundcolor = consolecolor.white;            console.tcursorposition(nowscenetype == e_scenetype.begin ? w / 2 - 3 : w / 2 - 4, 8);            console.write(nowscenetype == e_scenetype.begin ? "飞行棋" : "游戏结束");                        //当前选项的编号            int count = 0;            bool isover = fal;            while (true)            {                console.tcursorposition(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.tcursorposition(w/2-4, 13);                console.foregroundcolor = count == 1 ? consolecolor.red : consolecolor.white;                console.write("退出游戏");                switch (console.readkey(true).key)                {                    ca consolekey.w:                        --count;                        if (count < 0)                        {                            count = 0;                        }                        break;                    ca consolekey.s:                        ++count;                        if (count > 1)                        {                            count = 1;                        }                        break;                    ca consolekey.j:                        if(count == 0)                        {                            nowscenetype = nowscenetype ==e_scenetype.begin? e_scenetype.game:e_scenetype.begin;                            isover = true;                        }                        el                        {                            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.tcursorposition(i, 0);                console.write("■");                //中间一行                console.tcu世界分几大洲rsorposition(i, h-6);                console.write("■");                console.tcursorposition(i, h - 11);                console.write("■");                //最下面一行                console.tcursorposition(i, h-1);                console.write("■");            }            //竖着的墙            for(int i = 0; i < h; i++)            {                //左边的墙                console.tcursorposition(0, i);                console.write("■");                //右边的墙                console.tcursorposition(w-2, i);                console.write("■");            }            console.tcursorposition(2, h - 5);            console.foregroundcolor = consolecolor.white;            console.write("按任意键开始扔色子");            console.tcursorposition(2, h - 10);            console.write("□:普通格子");            console.tcursorposition(2, h - 9);            console.foregroundcolor = consolecolor.blue;            console.write("■:暂停,一回合不动");            console.tcursorposition(22,h - 9);            console.foregroundcolor = consolecolor.red;            console.write("●:炸弹,倒退5格");            console.tcursorposition(2, h - 8);            console.foregroundcolor = consolecolor.white;            console.write("×:时空隧道,随机倒退,暂停,交换位置");            console.tcursorposition(2, h - 7);            console.foregroundcolor = consolecolor.cyan;            console.write(":玩家  ");            console.tcursorposition(11, h - 7);            console.fore环境保护措施groundcolor = consolecolor.magenta;            console.write("▲:电脑  ");            console.tcursorposition(20, h - 7);            console.foregroundcolor = consolecolor.blue;            console.write("◎:玩家电脑重合");        }

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

    enum e_grid_type    {        normal,        boom,        pau,        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.tcursorposition(pos.x, pos.y);            switch(_type)            {                ca e_grid_type.normal:                    console.foregroundcolor = consolecolor.white;                    console.write("□");                    break;                ca e_grid_type.boom:                    console.foregroundcolor = consolecolor.red;                    console.write("●");                    break;                ca e_grid_type.pau:                    console.foregroundcolor = consolecolor.blue;                    console.write("■");                    break;                ca 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;                }                el if(randomnum < 90 && randomnum >=85)                {                    //炸弹                    grids[i]._type = e_grid_type.boom;                }                el if(randomnum < 95 && randomnum >=90)                {                    //暂停                    grids[i]._type = e_grid_type.pau;                }                el                {                    //时空隧道                    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;                    }                }                el                {                    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 ispau;        public player(int index, e_player_type type)        {            nowindex = index;            this.type = type;            ispau = fal;        }                public void draw(map mapinfo)        {            //从传入的地图中得到格子信息            grid grid = mapinfo.grids[nowindex];            console.tcursorposition(grid.pos.x, grid.pos.y);            switch(type)            {                ca e_player_type.player:                    console.foregroundcolor = consolecolor.cyan;                    console.write("");                    break;                ca 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.tcursorposition(grid.pos.x, grid.pos.y);                console.foregroundcolor = consolecolor.darkgreen;                console.write("◎");            }            //不重合时            el            {                player.draw(map);                computer.draw(map);            }        }

扔骰子逻辑

        //擦除提示的函数        static void clearinfo(int h)        {            console.tcursorposition(2, h - 5);            console.write("                                             ");            console.tcursorposition(2, h - 4);            console.write("                                             ");            console.tcursorposition(2, h - 3);            console.write("                                             ");            console.tcursorposition(2, h - 2);            console.write("                                             ");        }        /// <summary>        /// 扔色子函数        /// </summary>>        /// <param name="w">窗口的宽</param>        /// <param name="h">窗口的高</param>        /// <param name="p">扔色子的对象</param>        /// <param name="map">地图信息</param>        /// <returns>默认返回fal 代表没有结束</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.ispau)            {                console.tcursorposition(2, h - 5);                console.write("处于暂停状态,{0}需要暂停一回合", p.type == e_player_type.player ? "你" : "电脑");                console.tcursorposition(2, h - 4)秋分的诗句;                console.write("请按任意键,让{0}开始扔色子", p.type == e_player_type.player ? "电脑" : "你");                //停止暂停                p.ispau = fal;                return fal;            }            //扔色子目的是改变玩家或电脑的位置 计算位置的变化            //扔色子 随机一个1到6的数字,加上去            random r = new random();            int randomnum = r.next(1, 7);            p.nowindex += randomnum;            //打印扔的点数            console.tcursorposition(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.tcursorposition(2, h - 4);                if(p.type == e_player_type.player)                {                    console.write("恭喜你,率先到达了终点");                }                el                {                    console.write("很遗憾,电脑率先到达了终点");                }                console.tcursorposition(2, h - 3);                console.write("请按任意键结束");                return true;            }            el            {                //没有到终点 就判断当前对象到了一个什么类型的格子                grid grid = map.grids[p.nowindex];                switch(grid._type)                {                    ca e_grid_type.normal:                        console.tcursorposition(2, h - 4);                        console.write("{0}到达了一个安全位置", p.type == e_player_type.player ? "你" : "电脑");                        console.tcursorposition(2, h - 3);                        console.write("请按任意键,让{0}开始扔色子", p.type == e_player_type.player ? "电脑" : "你");                        break;                    ca e_grid_type.boom:                        p.nowindex -= 5;                        if(p.nowindex < 0)                        {                            p.nowindex = 0;                        }                        console.tcursorposition(2, h - 4);                        console.write("{0}踩到了炸弹,退后5格", p.type == e_player_type.player ? "你" : "电脑");                        console.tcursorposition(2, h - 3);                        console.write("请按任意键,让{0}开始扔色子", p.type == e_player_type.player ? "电脑" : "你");                        break;                    ca e_grid_type.pau:                        p.ispau = true;                        console.tcursorposition(2, h - 4);                        console.write("{0}到达了暂停点,你需要暂停一回合", p.type == e_player_type.player ? "你" : "电脑");                        console.tcursorposition(2, h - 3);                        console.write("请按任意键,让{0}开始扔色子", p.type == e_player_type.player ? "电脑" : "你");                        break;                    ca e_grid_type.tunnel:                        console.tcursorposition(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.tcursorposition(2, h - 5);                            console.write("触发倒退5格");                        }                        el if(randomnum <= 60)                        {                            p.ispau = true;                            console.tcursorposition(2, h - 3);                            console.write("触发暂停一回合");                        }                        el                        {                            int tmp = p.nowindex;                            p.nowindex = otherp.nowindex;                            otherp.nowindex = tmp;                            console.tcursorposition(2, h - 3);                            console.write("惊喜,双方交换位置");                        }                        console.tcursorposition(2, h - 2);                        console.write("请按任意键,让{0}开始扔色子", p.type == e_player_type.player ? "电脑" : "你");                        break;                }            }            //默认没有结束            return fal;        }

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

本文发布于:2023-04-04 22:30:44,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/64621ee875c72d511b57d6e8f21f81bd.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:C#实现经典飞行棋游戏的示例代码.doc

本文 PDF 下载地址:C#实现经典飞行棋游戏的示例代码.pdf

上一篇:壳聚糖季铵盐
下一篇:返回列表
标签:色子   格子   电脑   玩家
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图