[英]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.