iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#实现飞行棋优化版
  • 448
分享到

C#实现飞行棋优化版

2024-04-02 19:04:59 448人浏览 独家记忆
摘要

本文实例为大家分享了C#实现飞行棋优化版的具体代码,供大家参考,具体内容如下 代码如下 using System; namespace ConsoleApp1 { en

本文实例为大家分享了C#实现飞行棋优化版的具体代码,供大家参考,具体内容如下

代码如下


using System;

namespace ConsoleApp1
{
    enum E_Gezi
    {
        Simple,
        Boom,
        Pause,
        TimeTravel,
    }
    enum E_PlayerType
    {
        MianPlayer,
        Computer,
    }
    enum E_SceneType
    {
        Begin,
        Game,
        End,
    }
    struct Vector2
    {
        public int x;
        public int y;
        public Vector2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    struct Gezi
    {
        public E_Gezi type;
        public Vector2 pos;
        public Gezi(int x, int y, E_Gezi gezi)
        {
            pos = new Vector2(x, y);
            type = gezi;
        }
        public void Draw()
        {
            Console.SetCursorPosition(pos.x, pos.y);
            switch (type)
            {
                case E_Gezi.Simple:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("□");
                    break;
                case E_Gezi.Boom:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("●");
                    break;
                case E_Gezi.Pause:
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    Console.Write("Ⅱ");
                    break;
                case E_Gezi.TimeTravel:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("D ");
                    break;
            }
        }
    }
    struct Map
    {
        public Gezi[] gezis;
        public Map(int x, int y, int num)
        {
            this.gezis = new Gezi[num];
            Random r = new();
            int ra;
            int stepNum = 2;
            int indexX = 0;
            int indexY = 0;
            for (int i = 0; i < gezis.Length; i++)
            {
                //随机有物体生成
                ra = r.Next(1, 101);
                if (ra >= 1 && ra < 85 || i == 0 || i == gezis.Length - 1)
                {
                    gezis[i].type = E_Gezi.Simple;
                }
                else if (ra >= 85 && ra < 90)
                {
                    gezis[i].type = E_Gezi.Pause;
                }
                else if (ra >= 90 && ra < 95)
                {
                    gezis[i].type = E_Gezi.Boom;
                }
                else
                {
                    gezis[i].type = E_Gezi.TimeTravel;
                }
                gezis[i].pos = new Vector2(x, y);
                //一行有十个 然后让y加2次 
                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 < gezis.Length; i++)
            {
                gezis[i].Draw();
            }
        }
    }
    struct Player
    {
        public E_PlayerType type;
        public int indexStep;
        //这里有一种优化方案 就是直接把坐标变成索引 并且内置一个bool变量判断玩家是否被暂停
        //之后用索引配合map来绘制地图
        public bool isPause;
        public Player(int index, E_PlayerType type)
        {
            indexStep = index;
            this.type = type;
            isPause = false;
        }
        public void Draw(Map map)
        {
            Console.SetCursorPosition(map.gezis[indexStep].pos.x, map.gezis[indexStep].pos.y);
            switch (type)
            {
                case E_PlayerType.MianPlayer:
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("★");
                    break;
                case E_PlayerType.Computer:
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.Write("▲");
                    break;
            }
        }
    }
    class Program
    {
        static void Main()
        {
            //初始化设置
            Console.Setwindowsize(50, 40);
            Console.SetBufferSize(50, 40);
            Console.CursorVisible = false;
            E_SceneType type = E_SceneType.Begin;
            while (true)
            {
                switch (type)
                {
                    case E_SceneType.Begin:
                        Console.Clear();
                        GameScence(ref type);
                        break;
                    case E_SceneType.Game:
                        Console.Clear();
                        DrawWall();
                        Map map = new(12, 3, 80);
                        map.Draw();
                        //绘制玩家相关
                        Player player = new(0, E_PlayerType.MianPlayer);
                        Player computer = new(0, E_PlayerType.Computer);
                        DrawPlayer( player, computer, map);
                        while (true)
                        {
                            if(!InputSelection(ref player,ref computer,map,ref type))
                            {
                                break;
                            }
                            if(!InputSelection(ref computer, ref player, map, ref type))
                            {
                                break;
                            }
                        }
                        break;
                    case E_SceneType.End:
                        Console.Clear();
                        GameOverScence(ref type);
                        break;
                }
            }
        }
        static void GameScence(ref E_SceneType type)
        {
            //标题
            string title = "飞行棋";
            string startGame = "开始游戏";
            string quitGame = "结束游戏";
            //选择的索引
            int selectIndex = 0;
            //选择
            Console.SetCursorPosition(25 - title.Length, 10);
            Console.Write(title);
            //跳出while
            bool isquit = false;
            while (true)
            {
                if (isquit)
                {
                    break;
                }
                Console.SetCursorPosition(25 - startGame.Length, 13);
                Console.ForegroundColor = selectIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write(startGame);
                Console.SetCursorPosition(25 - quitGame.Length, 15);
                Console.ForegroundColor = selectIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write(quitGame);
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.W:
                        selectIndex = 0;
                        break;
                    case ConsoleKey.S:
                        selectIndex = 1;
                        break;
                    case ConsoleKey.J:
                        if (selectIndex == 0)
                        {
                            type = E_SceneType.Game;
                            isquit = true;
                        }
                        else if (selectIndex == 1)
                        {
                            Environment.Exit(0);
                        }
                        break;
                }
            }
        }
        static void GameOverScence(ref E_SceneType type)
        {
            //标题
            string Endtitle = "游戏结束";
            string returnMain = "回到主菜单";
            string quitGame1 = "结束游戏";
            //选择的索引
            int selectIndex = 0;
            Console.SetCursorPosition(25 - Endtitle.Length, 10);
            Console.Write(Endtitle);
            //跳出while
            bool isquit = false;
            while (true)
            {
                if (isquit)
                {
                    break;
                }
                Console.SetCursorPosition(25 - returnMain.Length, 13);
                Console.ForegroundColor = selectIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write(returnMain);
                Console.SetCursorPosition(25 - quitGame1.Length, 15);
                Console.ForegroundColor = selectIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write(quitGame1);
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.W:
                        selectIndex = 0;
                        break;
                    case ConsoleKey.S:
                        selectIndex = 1;
                        break;
                    case ConsoleKey.J:
                        if (selectIndex == 0)
                        {
                            type = E_SceneType.Begin;
                            isquit = true;
                        }
                        else if (selectIndex == 1)
                        {
                            Environment.Exit(0);
                        }
                        break;
                }
            }
        }
        static void DrawWall()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            for (int i = 0; i < 50 - 2; i += 2)
            {
                Console.SetCursorPosition(i, 0);
                Console.Write("■");
            }
            for (int i = 0; i < 50 - 2; i += 2)
            {
                Console.SetCursorPosition(i, 39);
                Console.Write("■");
            }
            for (int i = 0; i < 50 - 2; i += 2)
            {
                Console.SetCursorPosition(i, 34);
                Console.Write("■");
            }
            for (int i = 0; i < 50 - 2; i += 2)
            {
                Console.SetCursorPosition(i, 29);
                Console.Write("■");
            }
            for (int i = 0; i < 40; i++)
            {
                Console.SetCursorPosition(0, i);
                Console.Write("■");
            }
            for (int i = 0; i < 40; i++)
            {
                Console.SetCursorPosition(46, i);
                Console.Write("■");
            }
            Console.SetCursorPosition(2, 30);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("□: 普通格子");
            Console.SetCursorPosition(2, 31);
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.Write("Ⅱ: 暂停,一回合不动");
            Console.SetCursorPosition(24, 31);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("●: 炸弹,倒退5格");
            Console.SetCursorPosition(2, 32);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("D : 时空隧道,随机倒退,暂停,换位置");
            Console.SetCursorPosition(2, 33);
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("★:玩家");
            Console.SetCursorPosition(10, 33);
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write("▲:电脑");
            Console.SetCursorPosition(18, 33);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("C :电脑玩家重合");
            Console.SetCursorPosition(2, 35);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("按任意键开始扔筛子");
        }
        //优化二 专门写一个函数 绘制玩家        //重合的时候 和 不重合的时候分别画
        static void DrawPlayer(Player player, Player player1,Map map)
        {
            if(player.indexStep == player1.indexStep)
            {
                Console.SetCursorPosition(map.gezis[player.indexStep].pos.x, map.gezis[player.indexStep].pos.y);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("C ");
            }
            else
            {
                Console.SetCursorPosition(map.gezis[player.indexStep].pos.x, map.gezis[player.indexStep].pos.y);
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("★");
                Console.SetCursorPosition(map.gezis[player1.indexStep].pos.x, map.gezis[player1.indexStep].pos.y);
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.Write("▲");
            }
        }
        //优化3 扔色子函数判断是否游戏结束
        //优化4 通过函数包裹来重用玩家输入和电脑输入
        static bool InputSelection(ref Player player,ref Player player1, Map map,ref E_SceneType type)
        {
            if (player.isPause)
            {
                player.isPause = false;
            }
            else
            {
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.A:
                    case ConsoleKey.B:
                    case ConsoleKey.C:
                    case ConsoleKey.D:
                    case ConsoleKey.E:
                    case ConsoleKey.F:
                    case ConsoleKey.G:
                    case ConsoleKey.H:
                    case ConsoleKey.I:
                    case ConsoleKey.J:
                    case ConsoleKey.K:
                    case ConsoleKey.L:
                    case ConsoleKey.M:
                    case ConsoleKey.N:
                    case ConsoleKey.O:
                    case ConsoleKey.P:
                    case ConsoleKey.Q:
                    case ConsoleKey.R:
                    case ConsoleKey.S:
                    case ConsoleKey.T:
                    case ConsoleKey.U:
                    case ConsoleKey.V:
                    case ConsoleKey.W:
                    case ConsoleKey.X:
                    case ConsoleKey.Y:
                    case ConsoleKey.Z:
                        ThrowShaiZi(ref player, ref player1, map, ref type);
                        break;
                }
            }
            if(type == E_SceneType.End)
            {
                return false;
            }
            return true;
        }
        static void ThrowShaiZi(ref Player player, ref Player computer, Map map,ref E_SceneType type)
        {
            Random r = new();
            player.indexStep += r.Next(1, 7);
            for (int i = 0; i < map.gezis.Length; i++)
            {
                if(player.indexStep >= map.gezis.Length - 1)
                {
                    player.indexStep = map.gezis.Length - 1;
                    type = E_SceneType.End;
                }
                if (player.indexStep == i)
                {
                    switch (map.gezis[i].type)
                    {
                        case E_Gezi.Simple:
                            break;
                        case E_Gezi.Boom:
                            player.indexStep -= 5;
                            break;
                        case E_Gezi.Pause:
                            player.isPause = true;
                            break;
                        case E_Gezi.TimeTravel:
                            if(r.Next(1,4) == 1)
                            {
                                player.indexStep -= 5;
                            }
                            else if(r.Next(1, 4) == 2)
                            {
                                player.isPause = true;
                            }
                            else if(r.Next(1, 4) == 3)
                            {
                                int temp = computer.indexStep;
                                computer.indexStep = player.indexStep;
                                player.indexStep = temp;
                            }
                            break;
                    }
                    break;
                }
            }
        }
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: C#实现飞行棋优化版

本文链接: https://www.lsjlt.com/news/125544.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#实现飞行棋优化版
    本文实例为大家分享了C#实现飞行棋优化版的具体代码,供大家参考,具体内容如下 代码如下 using System; namespace ConsoleApp1 { en...
    99+
    2024-04-02
  • C#实现winform版飞行棋
    本文实例为大家分享了C#实现winform版飞行棋的具体代码,供大家参考,具体内容如下 游戏界面 游戏规则: 1、两个人轮流掷骰子红人和绿人 2、投掷出2,4,6点出门,投掷出6...
    99+
    2024-04-02
  • 怎么用C#实现winform版飞行棋
    本篇内容介绍了“怎么用C#实现winform版飞行棋”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!本文实例为大家分享了C#实现winform...
    99+
    2023-06-20
  • C#实现飞行棋游戏
    飞行棋主要是讲的方法怎么应用,充分的去理解方法和方法的调用,整体收获还是很大的。 我想的是说一下整体的思路。在编程的时间里,逻辑是最重要的,先干嘛后干嘛,对吧。 直接上个飞行棋的图,...
    99+
    2024-04-02
  • C#实现简单的飞行棋游戏
    本文实例为大家分享了C#实现简单飞行棋游戏的具体代码,供大家参考,具体内容如下 下面展示 完整代码: namespace 飞行棋 { class Program ...
    99+
    2024-04-02
  • C#实现简单飞行棋小游戏
    本文实例为大家分享了C#实现简单飞行棋小游戏的具体代码,供大家参考,具体内容如下 目标:实现飞行棋游戏基础功能 玩家在地图触发道具: 1、获得道具,可以进行一次选择 1–交换位置 2...
    99+
    2024-04-02
  • C#控制台实现飞行棋小游戏
    本文实例为大家分享了C#控制台实现飞行棋小游戏的具体代码,供大家参考,具体内容如下 游戏标题 static void ShowTitle() { ...
    99+
    2024-04-02
  • C#实现简单的飞行棋小游戏
    本文实例为大家分享了C#实现简单飞行棋小游戏的具体代码,供大家参考,具体内容如下 1.玩家姓名的输入 2.对屏幕进行清屏 3.初始化地图 4.玩家A和玩家B玩游戏 using S...
    99+
    2024-04-02
  • C#实现控制台飞行棋小游戏
    本文实例为大家分享了C#实现控制台飞行棋小游戏的具体代码,供大家参考,具体内容如下 using System; using System.Collections.Generic;...
    99+
    2024-04-02
  • C#控制台实现简单飞行棋游戏
    本文实例为大家分享了C#控制台实现简单飞行棋游戏的具体代码,供大家参考,具体内容如下 需求分析 1.制作游戏头部:游戏头部介绍 2.绘制地图 使用一维数组装整个地图的路线 如果这...
    99+
    2024-04-02
  • C#如何实现简单的飞行棋游戏
    这篇文章主要为大家展示了“C#如何实现简单的飞行棋游戏”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C#如何实现简单的飞行棋游戏”这篇文章吧。下面展示 完整代码:namespace 飞...
    99+
    2023-06-20
  • C#如何实现简单的飞行棋小游戏
    这篇文章主要介绍了C#如何实现简单的飞行棋小游戏,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体内容如下玩家姓名的输入对屏幕进行清屏初始化地图玩家A和玩家B玩游戏using...
    99+
    2023-06-21
  • C#如何实现控制台飞行棋小游戏
    这篇文章将为大家详细讲解有关C#如何实现控制台飞行棋小游戏,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。本文实例为大家分享了C#实现控制台飞行棋小游戏的具体代码,供大家参考,具体内容如下using&nbs...
    99+
    2023-06-20
  • C#实现经典飞行棋游戏的示例代码
    目录效果展示主函数  场景类型枚举控制台基础设置开始及结束场景逻辑游戏场景逻辑固定打印的信息格子类型枚举和格子结构体  地图结构体玩家和电脑结...
    99+
    2024-04-02
  • C#如何用代码实现飞行棋简单小游戏
    本篇内容主要讲解“C#如何用代码实现飞行棋简单小游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#如何用代码实现飞行棋简单小游戏”吧!目标:实现飞行棋游戏基础功能玩家在地图触发道具:获得道具...
    99+
    2023-06-14
  • C#实现经典飞行棋游戏的脚本怎么写
    今天小编给大家分享一下C#实现经典飞行棋游戏的脚本怎么写的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。效果展示主函数&nbs...
    99+
    2023-06-29
  • c# 编写的简单飞行棋游戏
    目录项目效果实现代码基于winform制作的图形界面程序效果代码项目效果 实现代码 using System; namespace 飞行棋项目 { class Pr...
    99+
    2024-04-02
  • C#学习笔记之飞行棋项目
    本文实例为大家分享了C#控制台实现飞行棋项目的具体代码,供大家参考,具体内容如下 飞行棋游戏介绍 此次编程实现的飞行棋和我们小时候玩的有些不一样,规则大致类似,但是我在学习过程中的飞...
    99+
    2024-04-02
  • C语言实现简单版三子棋
    本文实例为大家分享了C语言实现简单版三子棋的具体代码,供大家参考,具体内容如下 游戏的主函数设计: 1.打印出可以让玩家选择游戏开始和退出的菜单。 2.如果玩家选择1,则游戏开始,并...
    99+
    2024-04-02
  • C++实现五子棋游戏(注释版)
    本文实例为大家分享了C++实现五子棋游戏的具体代码,供大家参考,具体内容如下 本程序是在vs2015环境下编译运行vs左上角:文件–新建–项目–W...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作