c# - 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
解决方案
推荐阅读
- android - Ble:从 Android 向 iOS 发送广告数据
- asp.net-core - Web API EF Core 异常被吞下?
- ubuntu - 我在使用 mkvirtualen 时遇到问题,我该如何继续?
- azure - Azure VM 在未通过远程桌面连接时暂停正在运行的软件
- vue.js - 在 Vue 应用程序中嵌入 GrapesJS MJML
- swift - 我们如何在 swift 中发送 BOOL 来代替 httpBody 中的(JSON 或数据),简而言之,我想发送 true 来代替 {value : true}?
- ios - Godot ios模板构建-运行时错误EXC_BAD_ACCESS
- html - 页面底部很难用,当我滚动时,底部有一个空白
- javascript - htmlWebPack 插件 publicPath 未按预期工作
- javascript - 如何在 vuejs 中更新页面