没有Console.SetCursorPosition(?,?)写入控制台屏幕

时间:2018-02-10 17:38:55

标签: c# console

有没有办法在控制台窗口中的特定坐标中编写字符串而不使用Console.SetCursorPosition(?,?)

2 个答案:

答案 0 :(得分:2)

解决这个问题的一般想法:

  • 计算当前"框架"你的游戏,只是文字的行,完全
  • 清除控制台
  • 打印整个"框架"到控制台
  • 等待输入,修改游戏内对象,重复。

,例如,我写了以下#34;控制台游戏":播放器是" *",其余的充满了#34; - "。窗口适应自动调整大小。玩家可以使用箭头键(左,右,上,下)向各个方向移动。没有完成错误检查。

它是一个基本设置,它使用StringBuilder个对象数组来进行简单的字符串修改(strings是不可变的)。写一般的DrawTextInFramebuffer(string text, int x, int y, framebuffer)会有意义。

编辑:此外,强制性视频建议:https://www.youtube.com/watch?v=KkMZI5Jbf18,使用控制台窗口的Retro-Racing游戏和C ++中的彩色块,尽管C#实现也是可能的。

enter image description here

using System;
using System.Linq;
using System.Text;

namespace StackOverflowTesting
{
    class Program
    {
        /* Player position */
        static int PlayerX = 1;
        static int PlayerY = 1;

        static string ComputeFrameBuffer()
        {
            //What are the current dimensions of the console window
            int consoleWindowHeight = Console.WindowHeight;
            int consoleWindowWidth = Console.WindowWidth - 1;  //-1 prevents line overflow

            //Compute framebuffer line-wise
            var lines = new StringBuilder[consoleWindowHeight];

            for(int y = 0; y < consoleWindowHeight; y++)
            {
                //Create the line as a repetition of consoleWindowWidth spaces or other filler.
                lines[y] = new StringBuilder(string.Join("", Enumerable.Repeat("-", consoleWindowWidth)));
                for (int x = 0; x < consoleWindowWidth; x++) 
                {
                    //What do we need to draw at this (x,y) position? is the player here? 
                    if(PlayerX == x && PlayerY == y)
                    {
                        //Yes. Use a '*' for the player "sprite"..
                        lines[y][x] = '*';
                    }
                }
            }
            //Concatinate all lines
            return string.Join("\n", lines.Select(l => l.ToString()));
        }

        static void Main(string[] args)
        {
            bool runGame = true;
            while (runGame)
            {
                //Render current frame
                string frame = ComputeFrameBuffer();
                Console.Clear();
                Console.Write(frame);

                //Grab next user input
                var pressedKey = Console.ReadKey(false);
                //Handle stuff
                switch (pressedKey.Key)
                {
                    case ConsoleKey.LeftArrow:
                        PlayerX--;
                        break;
                    case ConsoleKey.RightArrow:
                        PlayerX++;
                        break;
                    case ConsoleKey.UpArrow:
                        PlayerY--; //Coordinate system is upper left = (0,0). Downwards increases Y.
                        break;
                    case ConsoleKey.DownArrow:
                        PlayerY++;
                        break;
                    case ConsoleKey.Escape:
                        runGame = false;
                        break;
                }
                //clamp coordinates to be always within bounds
                int maxY = Console.WindowHeight;
                int maxX = Console.WindowWidth - 1; 
                if (PlayerX < 0) PlayerX = 0;
                if (PlayerX >= maxX) PlayerX = maxX - 1;
                if (PlayerY < 0) PlayerY = 0;
                if(PlayerY >= maxY) PlayerY = maxY - 1;
            }
        }
    }
}

答案 1 :(得分:0)

要控制字符在可查看控制台框内的绘制,只需将switch语句替换为以下字符:

df['x'] = df.groupby((df['number'].bfill()[::-1]//10).diff().ne(0).cumsum())['number'].transform(min)


  date  number      x
0  2019   150.0  150.0
1  2018     NaN  115.0
2  2017   118.0  115.0
3  2016     NaN  115.0
4  2015   115.0  115.0
5  2014   107.0  100.0
6  2013   105.0  100.0
7  2012     NaN  100.0
8  2011   100.0  100.0

​