首页 > 解决方案 > Console.Write() without delay / lag

问题描述

Is there a better way to write in the console than doing Console.Write ?

I'm doing an ASCII game using the windows console (without any graphic library), and when I move my character around, I call my function writeBackground(); from my Camera.cs class :

public void printBackground(int posX, int posY)
    {
        for (int x = 2; x < 81; x++)
        {
            int xPrint = posX - 40;
            int yPrint = posY - 13;
            for (int y = 1; y < 28; y++)
            {
                Console.SetCursorPosition(x, y);
                Console.Write(background.Colors[(xPrint + x - 2), (yPrint + y - 1)] + background.Characters[(xPrint + x - 2), (yPrint + y - 1)]);
            }
        }
    }

The graphics look like this : https://www.youtube.com/watch?v=aUc-UkNinx4

It's a bit teary and I was wondering if I could turn this writeBackground() a lot smoother :3

Like if there is a way to have more processing power or directly ask the GPU to Console.Write ^^

===== EDIT

I found a way to make my square tires way more smooth using StringBuilder ^^ !

public void printBackground(int posX, int posY)
{
    List<StringBuilder> stringBuilders = new List<StringBuilder>();


        for (int y = 1; y < 28; y++)
        {

            int xPrint = posX - 40;
            int yPrint = posY - 13;
            string line = "";
            for (int x = 2; x < 81; x++)
            {
                line += background.Colors[(xPrint + x - 2), (yPrint + y - 1)] + background.Characters[(xPrint + x - 2), (yPrint + y - 1)];
            }
            stringBuilders.Add(new StringBuilder(line));
        }
        for (int i = 1; i < stringBuilders.Count + 1; i++)
        {
            Console.SetCursorPosition(2, i);
            Console.Write(stringBuilders[i - 1]);

        }         
}

I basically make a list of string builder then print each of them instead of printing each character separately :D

标签: c#windowsconsoleconsole-applicationgame-development

解决方案


推荐阅读