C#控制台 – 绘制黑白像素

huangapple go评论60阅读模式
英文:

C# Console - Drawing Black & White pixels

问题

<sup>我之前已经问过同样的问题</sup><br>

我想要并且需要一种在C#控制台应用程序中绘制像素的方法,但不是大多数人所认为的方式。

1. 通过<code>x</code><code>y</code>绘制白色像素。
2. 每当我要求时,保存位于<code>x</code><code>y</code>位置的像素。我可以随意添加更多像素。
3. 显示白板,但内存中保存的所有像素将显示为黑色。并在最后清除内存中的每个像素。

<hr>

我已经尝试了实现我想要/需要的相同代码<b>4</b>次。这是最稳定的代码。如果你不明白我需要什么,请尝试这个库(程序),我个人愿意称其为**PxDrawEasy** - 用于在控制台应用程序中绘制像素。

```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using static PxDrawEasy;

public class PxDrawEasy
{
    // NewPixel(x, y) - 在X和Y处添加像素。
    // InitializeGrid(x, y) - 设置网格大小为X和Y。
    // Clear() - 清除内存中的所有像素,网格,网格初始化,网格大小等。
    // RemovePixels() - 清除内存中保存的所有像素。
    // ClearDisplay() - 清除显示。
    // RefreshDisplay() - 刷新网格,并显示内存中的所有像素。
    // DrawRectangle(x, y) - 绘制宽度为X,长度为Y的矩形。

    static List<int> XValues = new List<int>();
    static List<int> YValues = new List<int>();
    static int VerticalGridSize = 0; // Y
    static int HorizontalGridSize = 0; // X
    static bool GridInitialized = false;

    public static void InitializeGrid(int x, int y)
    {
        if (GridInitialized)
        {
            Console.ResetColor(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("PxDrawEasy错误:网格已初始化。"); Console.ResetColor(); return;
        } else GridInitialized = true;

        VerticalGridSize = y;
        HorizontalGridSize = x;
        DrawRectangle(x, y);
    }

    public static void DrawRectangle(int x, int y)
    {
        Console.ResetColor();
        for (int i = 0; i < y; i++)
        {
            for (int b = 0; b < x; b++)
            {
                Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.White;
                Console.Write("Ty");
            }
            Console.WriteLine();
        }
    }

    public static void NewPixel(int x, int y)
    {
        if (VerticalGridSize == 0 || HorizontalGridSize == 0 || !GridInitialized)
        {
            Console.ResetColor(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("PxDrawEasy错误:网格尚未初始化。"); Console.ResetColor(); return;
        }

        if (VerticalGridSize < x || HorizontalGridSize < y)
        {
            Console.ResetColor(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"PxDrawEasy错误:像素{x}x{y}超出生成网格的范围(网格大小:{HorizontalGridSize}x{VerticalGridSize})。"); Console.ResetColor(); return;
        }

        XValues.Add(x);
        YValues.Add(y);
    }

    public static void Clear()
    {
        XValues.Clear();
        YValues.Clear();
        GridInitialized = false;
        HorizontalGridSize = 0;
        VerticalGridSize = 0;
        Console.ResetColor(); Console.Clear();
    }

    public static void ClearDisplay()
    {
        Console.ResetColor(); Console.Clear();
    }

    public static void RemovePixels()
    {
        XValues.Clear();
        YValues.Clear();
    }

    public static void RefreshDisplay()
    {
        Console.ResetColor(); Console.Clear();

        for (int y = 1; y < VerticalGridSize +1; y++)
        {
            for (int x = 1; x < HorizontalGridSize +1; x++)
            {
                bool DRAWNOW = false;
                foreach (var item in YValues.ToList())
                {
                    foreach (var Item in XValues.ToList())
                    {
                        if (y == Convert.ToInt32(item) && x == Convert.ToInt32(Item))
                        {
                            XValues.Remove(x);
                            YValues.Remove(y);
                            DRAWNOW = true;
                        }
                    }
                }

                if (DRAWNOW)
                {
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Write("ll");
                    Console.ResetColor();
                } else {
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("ll");
                    Console.ResetColor();
                }
                DRAWNOW = false;
            }
            Console.WriteLine();
        }
    }
}

public class Program
{
    public static void Main()
    {
        InitializeGrid(8, 8);
        NewPixel(1, 1);
        NewPixel(1, 8);
        NewPixel(8, 1);
        NewPixel(8, 8);
        RefreshDisplay();
    }
}

希望能对你有所帮助。

英文:

<sup>I have already asked the same question before</sup><br>

I want and need a way to draw pixels in C# Console Application, but not the way most people would think it is.

  1. Draw <code>x</code> by <code>y</code> white pixels.
  2. Whenever I ask it to, save pixel located at <code>x</code>,<code>y</code>. I can add as more pixels as I want.
  3. Display white board, but all pixels saved in memory will be displayed as black. And clear every pixel in memory afterall.

<hr>

I've tried to implement the same code that I want/need over <b>4</b> times already. Here's the most stable code. Please try this library (program) if you don't understand what I need, I'd personally like to call it PxDrawEasy - for drawing pixels in a Console Application.

using System;
using System.Collections.Generic;
using System.Linq;
using static PxDrawEasy;
public class PxDrawEasy
{
// NewPixel(x, y) - Add pixel at X and Y.
// InitializeGrid(x, y) - Set grid size to be X and Y.
// Clear() - Clear all pixels in memory, grid, grid initialization, grid size, and so on.
// RemovePixels() - Clear all pixels saved in memory.
// ClearDisplay() - Clear the display.
// RefreshDisplay() - Refreshes the grid, and shows all pixels in the memory.
// DrawRectangle(x, y) - Draws a rectangle X wide and Y long.
static List&lt;int&gt; XValues = new List&lt;int&gt;();
static List&lt;int&gt; YValues = new List&lt;int&gt;();
static int VerticalGridSize = 0; // Y
static int HorizontalGridSize = 0; // X
static bool GridInitialized = false;
public static void InitializeGrid(int x, int y)
{
if (GridInitialized)
{
Console.ResetColor(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(&quot;PxDrawEasy error: Grid already initialized.&quot;); Console.ResetColor(); return;
} else GridInitialized = true;
VerticalGridSize = y;
HorizontalGridSize = x;
DrawRectangle(x, y);
}
public static void DrawRectangle(int x, int y)
{
Console.ResetColor();
for (int i = 0; i &lt; y; i++)
{
for (int b = 0; b &lt; x; b++)
{
Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.White;
Console.Write(&quot;Ty&quot;);
}
Console.WriteLine();
}
}
public static void NewPixel(int x, int y)
{
if (VerticalGridSize == 0 || HorizontalGridSize == 0 || !GridInitialized)
{
Console.ResetColor(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(&quot;PxDrawEasy error: Grid is not yet initialized.&quot;); Console.ResetColor(); return;
}
if (VerticalGridSize &lt; x || HorizontalGridSize &lt; y)
{
Console.ResetColor(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($&quot;PxDrawEasy error: Pixel {x}x{y} is outside of the bounds of a generated grid (grid size: {HorizontalGridSize}x{VerticalGridSize}).&quot;); Console.ResetColor(); return;
}
XValues.Add(x);
YValues.Add(y);
}
public static void Clear()
{
XValues.Clear();
YValues.Clear();
GridInitialized = false;
HorizontalGridSize = 0;
VerticalGridSize = 0;
Console.ResetColor(); Console.Clear();
}
public static void ClearDisplay()
{
Console.ResetColor(); Console.Clear();
}
public static void RemovePixels()
{
XValues.Clear();
YValues.Clear();
}
public static void RefreshDisplay()
{
Console.ResetColor(); Console.Clear();
for (int y = 1; y &lt; VerticalGridSize +1; y++)
{
for (int x = 1; x &lt; HorizontalGridSize +1; x++)
{
bool DRAWNOW = false;
foreach (var item in YValues.ToList())
{
foreach (var Item in XValues.ToList())
{
if (y == Convert.ToInt32(item) &amp;&amp; x == Convert.ToInt32(Item))
{
XValues.Remove(x);
YValues.Remove(y);
DRAWNOW = true;
}
}
}
if (DRAWNOW)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(&quot;ll&quot;);
Console.ResetColor();
} else {
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.White;
Console.Write(&quot;ll&quot;);
Console.ResetColor();
}
DRAWNOW = false;
}
Console.WriteLine();
}
}
}
public class Program
{
public static void Main()
{
InitializeGrid(8, 8);
NewPixel(1, 1);
NewPixel(1, 8);
NewPixel(8, 1);
NewPixel(8, 8);
RefreshDisplay();
}
}

It works as intended, except that I cannot draw multiple pixels in one same line.

I can also appreciate it if someone lets me know if I can use any library that does the same (if there is one), but it must have open source code. <i>No, I am not asking for downloading libraries (because I don't think that's allowed on StackOverflow), but if that's the only possible choice, then sure.</i><br><hr>

Can anyone help? I would appreciate it.

答案1

得分: 1

我假设你的'Pixel'意味着一个具有特定颜色的字符。

嗯,我不确定你的代码实际上做了什么,但这很可能不是一个好的方法。我建议你保留一个你想要的颜色缓冲区,这样你就可以画出完整的线条:

var frameBuffer = new ConsoleColor[10, 10]; // 默认为黑色

frameBuffer[1, 6] = ConsoleColor.White;
frameBuffer[6, 1] = ConsoleColor.White;

for (int x = 0; x < frameBuffer.GetLength(0); x++)
{
    for (int y = 0; y < frameBuffer.GetLength(1); y++)
    {
        Console.BackgroundColor = frameBuffer[x, y];
        Console.Write('_');
    }
    Console.WriteLine();
}

但请注意,这是一个非常糟糕的绘图方法。我知道ASCII艺术是一种存在的形式,有些人甚至可以通过ASCII渲染器获得足够好的性能来玩游戏。但我不确定在Windows控制台中是否可能实现这种效果,如果可能的话,你可能需要使用比这更先进的东西。如果你真的想做任何与图形相关的事情,我建议使用实际的图形API。

英文:

I assume your 'Pixel' means a character with a specific color.

Well, I'm not sure what your code actually does, but it is most likely not a good way to do it. I would instead recommend that you keep a buffer of your intended colors, so you can draw complete lines:

var frameBuffer = new ConsoleColor[10, 10]; // defaults to black
frameBuffer[1, 6] = ConsoleColor.White;
frameBuffer[6, 1] = ConsoleColor.White;
for (int x = 0; x &lt; frameBuffer.GetLength(0); x++)
{
for (int y = 0; y &lt; frameBuffer.GetLength(1); y++)
{
Console.BackgroundColor = frameBuffer[x, y];
Console.Write(&#39;_&#39;);
}
Console.WriteLine();
}

But note that this is a terrible way to draw anything. I know ASCII art is a thing, and some even get good enough performance to play games using an ascii renderer. But I'm not sure that is even possible in the windows console, and if it is, you likely need to use something more advanced than this. I would advice using the actual graphics APIs if you actually want to do anything graphics related.

huangapple
  • 本文由 发表于 2023年3月9日 23:22:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定