You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#控制台黑白像素绘制工具问题:同一行无法绘制多像素

C#控制台黑白像素绘制功能实现问题

需求说明

  • 绘制x×y尺寸的白色像素网格
  • 可保存任意数量指定坐标(x,y)的像素
  • 显示网格时,已保存的像素以黑色显示,显示完成后清空内存中的像素

现有代码问题

当前使用的PxDrawEasy工具存在同一行无法绘制多个像素的问题,核心原因:

  1. 用两个独立的List<int>存储X、Y坐标,容易出现坐标配对错误
  2. RefreshDisplay方法中嵌套遍历X/Y列表,且遍历过程中直接修改列表,导致匹配逻辑混乱
  3. 坐标匹配逻辑错误,无法正确识别同一行的多个像素

修复后的实现代码

using System;
using System.Collections.Generic;

public class PxDrawEasy
{
    // 存储待绘制的像素坐标,使用值类型元组保证坐标配对正确
    private static List<(int X, int Y)> _pixels = new List<(int X, int Y)>();
    private static int _gridWidth = 0; // X方向尺寸
    private static int _gridHeight = 0; // Y方向尺寸
    private static bool _isGridInitialized = false;

    /// <summary>初始化指定尺寸的白色网格</summary>
    public static void InitializeGrid(int width, int height)
    {
        if (_isGridInitialized)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("PxDrawEasy 错误:网格已初始化");
            Console.ResetColor();
            return;
        }

        _gridWidth = width;
        _gridHeight = height;
        _isGridInitialized = true;
        DrawWhiteGrid();
    }

    /// <summary>添加指定坐标的像素</summary>
    public static void AddPixel(int x, int y)
    {
        if (!_isGridInitialized || _gridWidth == 0 || _gridHeight == 0)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("PxDrawEasy 错误:网格未初始化");
            Console.ResetColor();
            return;
        }

        // 校验坐标是否在网格范围内(坐标从1开始计数)
        if (x < 1 || x > _gridWidth || y < 1 || y > _gridHeight)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"PxDrawEasy 错误:像素({x},{y})超出网格范围(网格尺寸:{_gridWidth}×{_gridHeight})");
            Console.ResetColor();
            return;
        }

        _pixels.Add((x, y));
    }

    /// <summary>刷新显示:绘制网格+黑色像素,完成后清空内存像素</summary>
    public static void RefreshDisplay()
    {
        if (!_isGridInitialized)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("PxDrawEasy 错误:网格未初始化");
            Console.ResetColor();
            return;
        }

        Console.Clear();
        Console.ResetColor();

        // 逐行绘制网格
        for (int y = 1; y <= _gridHeight; y++)
        {
            for (int x = 1; x <= _gridWidth; x++)
            {
                // 检查当前坐标是否为待绘制的黑色像素
                bool isBlackPixel = _pixels.Contains((x, y));
                
                if (isBlackPixel)
                {
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.ForegroundColor = ConsoleColor.Black;
                }
                else
                {
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.ForegroundColor = ConsoleColor.White;
                }
                // 用两个字符模拟像素(适配控制台字符宽高比)
                Console.Write("██");
                Console.ResetColor();
            }
            Console.WriteLine();
        }

        // 显示完成后清空内存中的像素
        _pixels.Clear();
    }

    /// <summary>绘制纯白色网格</summary>
    private static void DrawWhiteGrid()
    {
        Console.Clear();
        Console.ResetColor();
        for (int y = 0; y < _gridHeight; y++)
        {
            for (int x = 0; x < _gridWidth; x++)
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("██");
                Console.ResetColor();
            }
            Console.WriteLine();
        }
    }

    /// <summary>清空所有状态(网格+像素+初始化标记)</summary>
    public static void ClearAll()
    {
        _pixels.Clear();
        _gridWidth = 0;
        _gridHeight = 0;
        _isGridInitialized = false;
        Console.Clear();
        Console.ResetColor();
    }

    /// <summary>仅清空内存中的像素</summary>
    public static void ClearPixels()
    {
        _pixels.Clear();
    }
}

public class Program
{
    public static void Main()
    {
        PxDrawEasy.InitializeGrid(8, 8);
        // 添加同一行的多个像素测试
        PxDrawEasy.AddPixel(1, 1);
        PxDrawEasy.AddPixel(2, 1);
        PxDrawEasy.AddPixel(8, 1);
        PxDrawEasy.AddPixel(8, 8);
        PxDrawEasy.RefreshDisplay();
        
        // 按任意键退出
        Console.ReadKey();
    }
}

修复要点说明

  1. 使用List<(int X, int Y)>存储像素坐标,确保每个X和Y配对正确,避免独立列表的索引混乱
  2. 简化RefreshDisplay匹配逻辑,直接用Contains方法判断当前坐标是否需要绘制黑色像素
  3. 显示完成后立即清空像素列表,严格符合需求要求
  4. 优化坐标范围校验逻辑,修复原代码的边界判断错误
  5. 用██替代原字符,模拟更接近真实像素的显示效果

开源同类库推荐(优先自行实现,必要时可选)

  • ConsoleGUI:轻量级控制台UI库,包含基础绘图功能,开源可定制
  • Terminal.Gui:功能丰富的终端UI框架,支持像素级绘图、窗口管理等,开源且活跃维护
  • ConsoleCanvas:专注于控制台绘图的小型库,提供简单的像素绘制API,代码简洁易集成

内容的提问来源于stack exchange,提问作者winscripter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 17:08:13