Пример #1
0
        /// <summary>
        /// 创建图形容器对象
        /// </summary>
        /// <param name="graphics">图形对象</param>
        /// <param name="lines">字符行数组</param>
        /// <param name="ignore">是否忽略每行尾部空格</param>
        /// <returns>图形容器对象</returns>
        public static GraphicContainer CreatContainerByLines(this Graphics graphics, string[] lines, bool ignore = false)
        {
            string[] ls = new string[lines.Length];
            if (ignore)
            {
                for (int i = 0; i < ls.Length; i++)
                {
                    ls[i] = lines[i].TrimEnd(); //去除尾部空格
                }
            }
            else
            {
                for (int i = 0; i < ls.Length; i++)
                {
                    ls[i] = lines[i];
                }
            }

            List <GraphicGrid> grids = new List <GraphicGrid>();

            if (graphics.CharWidth == CharWidth.Double)
            {
                grids = graphics.CreatGridByStrings(Vector2.Zero, ls, Colour.Gray, Colour.Black);
            }
            else if (graphics.CharWidth == CharWidth.Single)
            {
                grids = graphics.CreatGridByStrings1(Vector2.Zero, ls, Colour.Gray, Colour.Black);
            }
            GraphicContainer container = new GraphicContainer(grids);

            return(container);
        }
Пример #2
0
        /// <summary>
        /// 运行ILoveDestroy的小游戏
        /// </summary>
        public static void ILoveDestroy()
        {
            string[] lines = new string[]
            {
                @"              ,----------------,              ,---------,",
                @"         ,-----------------------,          ,""        ,""|",
                @"       ,""                      ,""|        ,""        ,""  |",
                @"      +-----------------------+  |      ,""        ,""    |",
                @"      |  .-----------------.  |  |     +---------+      |",
                @"      |  |                 |  |  |     | -==----'|      |",
                @"      |  | I LOVE DESTROY! |  |  |     |         |      |",
                @"      |  | By Charlie      |  |  |/----|`---=    |      |",
                @"      |  | C:\>_           |  |  |   ,/|==== ooo |      ;",
                @"      |  |                 |  |  |  // |(((( [33]|    ,"" ",
                @"      |  `-----------------'  |,"" .;'| |((((     |  ,""   ",
                @"      +-----------------------+  ;;  | |         |,""     ",
                @"         /_)______________(_/  //'   | +---------+       ",
                @"    ___________________________/___  `,                  ",
                @"   /  oooooooooooooooo  .o.  oooo /,   \,""-----------    ",
                @"  / ==ooooooooooooooo==.o.  ooo= //   ,`\--{)B     ,""    ",
                @" /_==__==========__==_ooo__ooo=_/'   /___________,""      ",
                @"                                                         "
            };
            //根据字符串数组初始化宽高
            Resources.GetLinesSize(lines, out int width, out int height);
            //初始化控制台
            Graphics graphics = RuntimeEngine.Construct2(ConsoleType.Default,
                                                         true, false, (short)width, (short)height, CharWidth.Single);
            //变量定义
            GraphicContainer computer  = null;
            Vector2          cursorPos = new Vector2(15, 8);
            char             c         = '\0';
            bool             __        = true;
            float            timer     = 0;
            float            interval  = 0.5f;
            int    limit   = 11;           //最多打印11个英文字符
            int    counter = 0;            //计数器
            string str     = string.Empty; //输入字符
            //键盘
            List <ConsoleKey> keyboard = new List <ConsoleKey>();

            for (int i = 48; i < 91; i++)
            {
                keyboard.Add((ConsoleKey)i);
            }
            keyboard.Add(ConsoleKey.Spacebar);
            keyboard.Add(ConsoleKey.Backspace);
            keyboard.Add(ConsoleKey.Enter);
            //指令集
            Dictionary <string, Action> commands = new Dictionary <string, Action>();

            commands.Add("exit", () => { RuntimeEngine.Exit(); });
            //开始生命周期
            RuntimeEngine.Start(
                () =>
            {
                //Start
                computer = graphics.CreatContainerByLines(lines);
                computer.SetColor(Colour.Black, Colour.White);
            },
                () =>
            {
                //Update
                timer += Time.DeltaTime;
                if (timer >= interval)
                {
                    timer = 0;
                    if (__)
                    {
                        c = '_';
                    }
                    else
                    {
                        c = '\0';
                    }
                    __ = !__;
                }
                foreach (ConsoleKey item in keyboard)
                {
                    //输入指令
                    if (item == ConsoleKey.Enter)
                    {
                        if (Input.GetKeyDown(item))
                        {
                            string lowerCase = str.ToLower();
                            if (commands.ContainsKey(lowerCase))
                            {
                                //执行命令
                                commands[lowerCase]();
                            }
                        }
                    }
                    else if (item == ConsoleKey.Backspace)
                    {
                        if (Input.GetKey(item))
                        {
                            if (counter > 0)
                            {
                                counter--;
                                //删除字符
                                str = str.Substring(0, counter);
                                //删除字符图像
                                GraphicGrid before = computer.GraphicGrids[
                                    cursorPos.Y * width + cursorPos.X];
                                before.Left = new CharInfo('\0', Colour.Black, Colour.White);
                                //光标后退一格
                                cursorPos.X--;
                            }
                        }
                    }
                    else
                    {
                        if (Input.GetKeyDown(item))
                        {
                            if (counter < limit)
                            {
                                counter++;
                                //新增字符
                                str += (char)item;
                                //新增字符图形
                                GraphicGrid before = computer.GraphicGrids[
                                    cursorPos.Y * width + cursorPos.X];
                                before.Left = new CharInfo((char)item, Colour.Black, Colour.White);
                                //光标前进一格
                                cursorPos.X++;
                            }
                        }
                    }
                }
                //设置光标
                GraphicGrid graphicGrid = computer.GraphicGrids[
                    cursorPos.Y * width + cursorPos.X];
                graphicGrid.Left = new CharInfo(c, Colour.Black, Colour.White);
                //执行渲染指令
                graphics.PreRender();
                graphics.Render();
            }
                ,
                () =>
            {
                //Destroy
            }, 30);
        }