简体   繁体   English

我可以实时更新 C# 控制台输出而不需要不断写入吗?

[英]Can I update C# console output in realtime without constantly writing to it?

I am creating a simple fighting game console application and I have a health property on my Player objects, that is updated in the Fight method.我正在创建一个简单的格斗游戏控制台应用程序,并且我的Player对象上有一个health属性,该属性在Fight方法中更新。

I wonder if it is possible to show the health variable, in real-time in the console without calling a method that updates the console more than once.我想知道是否可以在控制台中实时显示健康变量,而无需调用多次更新控制台的方法。

Here is my main method which starts the fight, and displays the health output of each player.这是我开始战斗并显示每个玩家的健康输出的主要方法。

 public static void StartFight(Player p1, Player p2) {
   Console.Clear();
   Title(" BATTLE ");
   Console.WriteLine($"{p1.name} Health: {p1.health} ");
   Console.WriteLine($"{p2.name} Health: {p2.health} ");

   // Loop giving each player a chance to attack
   // and block each turn until 1 dies
   while (true) {
     if (Fight(p1, p2)) {
       Title(" GAME OVER! ");
       PlayAgain();
       break;
     }

     if (Fight(p2, p1)) {
       Title(" GAME OVER! ");
       PlayAgain();
       break;
     }
   }
 }

Is it possible to write to the console in real-time so that I don't have to keep doing Console.WriteLine($"{p1.name} Health: {p1.health} ");是否可以实时写入控制台,以便我不必继续执行Console.WriteLine($"{p1.name} Health: {p1.health} "); ? ?

No - if you are displaying to console, there is no way to have a realtime updating of the UI without calling a method more than once to update the console.否 - 如果您正在向控制台显示,则无法在不多次调用方法来更新控制台的情况下实时更新 UI。

Other UIs have ways to do this using events, bindings etc.其他 UI 有办法使用事件、绑定等来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# - 实时控制台输出重定向 - C# - Realtime console output redirection 如何将tsharks实时输出到c#程序 - How can I pipe tsharks output in realtime to c# program 在C#控制台应用程序中不断更新Datetime变量 - Constantly update Datetime variable in C# console application 将Console的输出写入C#中的文件? - Writing Output of Console to a file in C#? 如何在不中断用户输入的情况下在C#Windows控制台应用程序中更新行? - How can I update a line in a C# Windows Console App while without disrupting user input? 对代码完全陌生,有没有办法让循环不断运行,而其余代码在ac#控制台应用程序中继续运行呢? - Completely new to code, is there a way I can make a loop constantly run whilst the rest of the code continues in a c# console application? 使用批处理文件执行的输出实时更新C#表单RichTextBox内容,而无需等待批处理命令执行完成 - Update C# form RichTextBox content (in realtime) with the output of the batch file execution without waiting for the batch command execution to finish 无需将输出写入控制台的过程 - Process without writing output to console ANSI colors 并直接写入控制台 output C# - ANSI colors and writing directly to console output C# DataGrid实时更新C# - Realtime update of DataGrid C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM