不断显示文字

时间:2014-08-01 03:56:00

标签: c# text

我是C#编程的新手,我的主要目标是制作简单的基于文本的游戏。我目前遇到了第一个问题,即显示玩家的健康和攻击以及怪物的健康状况和攻击值。

我希望这些值显示在控制台的顶部,并在每次提升或降低玩家或怪物的健康状况时进行更新。我遇到的问题是我目前仅限于将此显示作为与其余功能不同的功能,我只能在运行该功能时显示它。

就像我说的那样,我对C#很陌生,所以我的代码可能非常笨重和业余。

   while (monsterHealth > 0 && playerHealthUpgraded > 0) 
        {
            DisplayStats ();

            Console.WriteLine ("You can either 'attack' or 'defend' yourself from the monster.");
            Console.WriteLine ("Attacking will decrease the monster's health by " + playerAttackUpgraded + " and defending yourself from the monster will cut the monster's attack value in half.");

            string input = Console.ReadLine ();

            if (input == "defend" || input == "attack" || input == "stimpak") 
            {

                switch (input) 
                {
                case "attack":
                    Console.Clear ();

                    DisplayStats ();

                    monsterHealth -= playerAttackUpgraded;
                    Console.WriteLine ("You raise your " + playerWeapon + " and fire several shots at the monster");
                    Console.WriteLine ("The monster's HP was lowered to " + monsterHealth + " by your attack.");

                    playerDamage += monsterAttack;

很明显" if" "而"而"语句在函数结束时关闭。

现在,我希望在控制台顶部不断显示的东西是" DisplayStats()",它看起来像这样:

   public static void DisplayStats()
    {
        Console.WriteLine ("Player Health: " + playerHealthUpgraded + "     Monster Health: " + monsterHealth);
        Console.WriteLine ("Player Attack: " + playerAttackUpgraded + "     Monster Attack: " + monsterAttack);
        Console.WriteLine (" ");
    }

任何帮助都会非常感激。谢谢,

-Liam

2 个答案:

答案 0 :(得分:1)

你不能让线条向后流动。但是,您可以使用Console.Clear()清除控制台。所以也许你可以尝试重绘游戏循环中的所有内容,就像在其他类型的游戏中一样。

答案 1 :(得分:0)

我建议亲自制作一个基于表单的游戏,因为你可以做更多的事情。

话虽如此,我认为你走的正确。

正如约翰尼所说,你可以在每次更新时重新绘制文本。 如果需要,可以将所有文本存储在字符串中,并在重绘文本时刷新控制台。

基本上,添加一些方法可以更容易地编写/清除框架:

private static string frame;

public static void writeLine(string s) {
    frame += s + Environment.NewLine; //I believe "\n" works too
}
public static void write(string s) {
    frame += s;
}
public static void clearFrame() { frame = ""; }
public static void drawFrame() {
    Console.Clear();
    DisplayStats();
    Console.WriteLine(frame);
}
public static void DisplayStats() {
    Console.WriteLine("Player Health: " + playerHealthUpgraded + "\tMonster Health: " + monsterHealth);
    Console.WriteLine("Player Attack: " + playerAttackUpgraded + "\tMonster Attack: " + monsterAttack);
    Console.WriteLine("");
}

然后,您的开关变为:

switch (input) 
{
    case "attack":
        monsterHealth -= playerAttackUpgraded;
        writeLine("You raise your " + playerWeapon + " and fire several shots at the monster");
        writeLine("The monster's HP was lowered to " + monsterHealth + " by your attack.");

        playerDamage += monsterAttack;
        drawFrame();

这样,如果你愿意,你可以保留你已有的文字,附加它或你想做的任何事情。