控制台动画

Mar*_*kus 48 c#

我只是想知道如何创建简单的动画,如在C#控制台应用程序上闪烁,移动东西.这有什么特别的方法吗?

小智 52

传统控制台微调器:

    static void Main(string[] args)
    {
        ConsoleSpiner spin = new ConsoleSpiner();
        Console.Write("Working....");
        while (true) 
        {
            spin.Turn();
        }
    }

public class ConsoleSpiner
{
    int counter;
    public ConsoleSpiner()
    {
        counter = 0;
    }
    public void Turn()
    {
        counter++;        
        switch (counter % 4)
        {
            case 0: Console.Write("/"); break;
            case 1: Console.Write("-"); break;
            case 2: Console.Write("\\"); break;
            case 3: Console.Write("|"); break;
        }
        Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
    }
}
Run Code Online (Sandbox Code Playgroud)


Thi*_*Guy 16

这是我的旋转器.它的目的是让一个程序做一些工作,同时微调器向用户显示实际发生的事情:

  public class Spinner : IDisposable
  {
     private const string Sequence = @"/-\|";
     private int counter = 0;
     private readonly int left;
     private readonly int top;
     private readonly int delay;
     private bool active;
     private readonly Thread thread;

     public Spinner(int left, int top, int delay = 100)
     {
        this.left = left;
        this.top = top;
        this.delay = delay;
        thread = new Thread(Spin);
     }

     public void Start()
     {
        active = true;
        if (!thread.IsAlive)
           thread.Start();
     }

     public void Stop()
     {
        active = false;
        Draw(' ');
     }

     private void Spin()
     {
        while (active)
        {
           Turn();
           Thread.Sleep(delay);
        }
     }

     private void Draw(char c)
     {
        Console.SetCursorPosition(left, top);
        Console.ForegroundColor = ConsoleColor.Green;
        Console.Write(c);
     }

     private void Turn()
     {
        Draw(Sequence[++counter % Sequence.Length]);
     }

     public void Dispose()
     {
        Stop();
     }
  }
Run Code Online (Sandbox Code Playgroud)

你使用这样的类:

     var spinner = new Spinner(10, 10);

     spinner.Start();

     // Do your work here instead of sleeping...
     Thread.Sleep(10000);

     spinner.Stop();
Run Code Online (Sandbox Code Playgroud)

  • @ThisGuy:您提供了最好的示例,它确实很容易理解,并包括了如何在多线程应用程序中使用它,这是旋转器的关键。谢谢。 (2认同)

Oze*_*esh 8

ConsoleSpinner 和序列实现的出色工作。感谢您的代码。我考虑过分享我的定制方法。

    public class ConsoleSpinner
    {
        static string[,] sequence = null;

        public int Delay { get; set; } = 200;

        int totalSequences = 0;
        int counter;

        public ConsoleSpinner()
        {
            counter = 0;
            sequence = new string[,] {
                { "/", "-", "\\", "|" },
                { ".", "o", "0", "o" },
                { "+", "x","+","x" },
                { "V", "<", "^", ">" },
                { ".   ", "..  ", "... ", "...." },
                { "=>   ", "==>  ", "===> ", "====>" },
               // ADD YOUR OWN CREATIVE SEQUENCE HERE IF YOU LIKE
            };

            totalSequences = sequence.GetLength(0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sequenceCode"> 0 | 1 | 2 |3 | 4 | 5 </param>
        public void Turn(string displayMsg = "", int sequenceCode = 0)
        {
            counter++;
            
            Thread.Sleep(Delay);

            sequenceCode = sequenceCode > totalSequences - 1 ? 0 : sequenceCode;

            int counterValue = counter % 4;

            string fullMessage = displayMsg + sequence[sequenceCode, counterValue];
            int msglength = fullMessage.Length;

            Console.Write(fullMessage);

            Console.SetCursorPosition(Console.CursorLeft - msglength, Console.CursorTop);
        }
    }
Run Code Online (Sandbox Code Playgroud)

执行:

    ConsoleSpinner spinner = new ConsoleSpinner();
    spinner.Delay = 300;
    while (true)
    {
        spinner.Turn(displayMsg: "Working ",sequenceCode:5);
    }
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明


Ree*_*sey 6

是的,有很多方法可以解决这个问题.

特别是,您可能希望查看以下Console方法:

  1. SetCursorPosition(你可以移动光标,并覆盖元素)
  2. MoveBufferArea(复制/粘贴区域顶部)
  3. ForegroundColorBackgroundColor(改变着色)


Chu*_*nch 6

刚刚看到这个和其他一些关于它的帖子,喜欢这种东西!我采用了 Tuukka 的一段不错的代码并对其进行了一些改进,以便该类可以轻松地设置为几乎任何旋转序列。我可能会添加一些访问器和一个重载的构造函数来完善它并将其放入旧工具箱中。好玩的东西!

class ConsoleSpinner
{
    int counter;
    string[] sequence;

    public ConsoleSpinner()
    {
        counter = 0;
        sequence = new string[] { "/", "-", "\\", "|" };
        sequence = new string[] { ".", "o", "0", "o"};
        sequence = new string[] { "+", "x" };
        sequence = new string[] { "V", "<", "^", ">" };
        sequence = new string[] { ".   ", "..  ", "... ", "...." };
    }

    public void Turn()
    {
        counter++;

        if (counter >= sequence.Length)
            counter = 0;

        Console.Write(sequence[counter]);
        Console.SetCursorPosition(Console.CursorLeft - sequence[counter].Length, Console.CursorTop);
    }
}
Run Code Online (Sandbox Code Playgroud)


Rub*_*ias 5

你需要使用Console.ForegroundColor,Console.BackgroundColorConsole.SetCursorPosition(int, int)

编辑:为了灵感,让我们跳舞吧

  • 我希望将Lets Dance动画的帧视为文本,因此我们可以将这样的动画实现为Console Spinner. (4认同)