初学者C#。在Console中造成随机伤害

时间:2014-09-24 19:52:19

标签: console-application

我和我的一个朋友正在制作一个在命令提示符中打开的小巧有趣的游戏。我们无法让每回合的随机伤害发挥作用。每回合伤害不会改变。我们你喜欢在每次攻击时都拥有它的伤害是不同的,因为此时为伤害生成的第一个数字保持不变直到游戏结束。如果你看到其他任何东西,可以更好地随意添加你的选择!感谢您抽出宝贵时间阅读本文。

class Program
{
    static void Main(string[] args)
    {
        Random rand = new Random();

        int playerHP = 30;
        int playerDMG = rand.Next(1, 8);
        string yourName;

        int monsterHP = 30;
        int monsterDMG = rand.Next(1, 10);

        string[] monsterName = { "John Crawford", "Gaurav the Chicken" , "Satan" , "Temmo"};
        string[] phrase = { " ' Let the blood drip from your body!'" , " Evil comes from the soul!" , " I will eat your soul!"};
        int phrasewords = rand.Next(0, phrase.Length);
        int mNames = rand.Next(0, monsterName.Length);

        //Story

        Console.WriteLine("Name your Hero: ");
        yourName = Console.ReadLine();


        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("As you walk through the forest... " + monsterName[mNames] + " appears from the shadows \nand says," + phrase[phrasewords] + "\n");
        Console.WriteLine("Do you wish to <attack> " + monsterName[mNames] + ", <defend> yourself, or <run>?\n");
        Console.ResetColor();

        while (playerHP > 0 && monsterHP > 0)
        {
            bool defend = false;
            string input = Console.ReadLine();

            //Inputs

            switch (input)
            {
                case "attack":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(yourName + " attacks " + monsterName[mNames] + " with his fist, which deals " + playerDMG + " damage\n");
                    monsterHP -= playerDMG;
                    System.Threading.Thread.Sleep(500);
                    Console.ResetColor();
                    break;

                case "run":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(monsterName[mNames] + " says, 'LOL NOPE' and you must remain on the battlefield.\n");
                    Console.ResetColor();
                    break;

                case "defend":
                    defend = true;
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    Console.WriteLine(yourName + " blocks " + monsterName[mNames] + "'s attack with his arms\n");
                    Console.ResetColor();
                    break;

            }

            //Damage & Health

            if (!defend)
            {
                Console.WriteLine(monsterName[mNames] + " attacks " + yourName + " for " + monsterDMG + " damage because " + yourName + " doesn't know how to \ndefend himself.\n");
                playerHP -= monsterDMG;
                System.Threading.Thread.Sleep(500);
            }
            else
            {
                Console.WriteLine(monsterName[mNames] + " attacks " + yourName + " for " + (monsterDMG / 2) + " damage because " + yourName + " doesn't know how to \nfight like a man.\n");
                playerHP -= monsterDMG / 2;
                System.Threading.Thread.Sleep(500);
            }

            Console.WriteLine(yourName + " has " + playerHP + " health.\n");
            Console.WriteLine(monsterName[mNames] + " has " + monsterHP + " health.\n");

        }

        //Win, Lose, Tie

        if (playerHP <= 0)
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine(yourName + " died! GG NO RE\n");
            Console.ResetColor();
        }
        if (monsterHP <= 0 && (playerHP > 0))
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Wow, you win. Congratulations.\n");
            Console.ResetColor();

        }
        else
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("It was a tie!");
            Console.ResetColor();
        }
        Console.ReadLine();
    }
}

2 个答案:

答案 0 :(得分:0)

你的MonsterDMG和playeyDMG只计算一次。 把它移到你的循环中:))

while (playerHP > 0 && monsterHP > 0)
    {
        bool defend = false;
        string input = Console.ReadLine();
        monsterDMG = rand.Next(1, 10); //for example here
        playerDMG = rand.Next(1, 8); // : )

PS。未来提示:如果您必须在短时间内生成更多的random,请使用此随机生成器:)

Random random = new Random(Guid.NewGuid().GetHashCode());

答案 1 :(得分:0)

您需要在while循环结束时计算新的MonsterDMG和PlayerDMG

int playerHP = 30;
        int playerDMG = rand.Next(1, 8);
        string yourName;

        int monsterHP = 30;
        int monsterDMG = rand.Next(1, 10);

        string[] monsterName = { "John Crawford", "Gaurav the Chicken" , "Satan" , "Temmo"};
        string[] phrase = { " ' Let the blood drip from your body!'" , " Evil comes from the soul!" , " I will eat your soul!"};
        int phrasewords = rand.Next(0, phrase.Length);
        int mNames = rand.Next(0, monsterName.Length);

        //Story

        Console.WriteLine("Name your Hero: ");
        yourName = Console.ReadLine();


        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("As you walk through the forest... " + monsterName[mNames] + " appears from the shadows \nand says," + phrase[phrasewords] + "\n");
        Console.WriteLine("Do you wish to <attack> " + monsterName[mNames] + ", <defend> yourself, or <run>?\n");
        Console.ResetColor();

        while (playerHP > 0 && monsterHP > 0)
        {
            bool defend = false;
            string input = Console.ReadLine();

            //Inputs

            switch (input)
            {
                case "attack":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(yourName + " attacks " + monsterName[mNames] + " with his fist, which deals " + playerDMG + " damage\n");
                    monsterHP -= playerDMG;
                    System.Threading.Thread.Sleep(500);
                    Console.ResetColor();
                    break;

                case "run":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(monsterName[mNames] + " says, 'LOL NOPE' and you must remain on the battlefield.\n");
                    Console.ResetColor();
                    break;

                case "defend":
                    defend = true;
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    Console.WriteLine(yourName + " blocks " + monsterName[mNames] + "'s attack with his arms\n");
                    Console.ResetColor();
                    break;

            }

            //Damage & Health

            if (!defend)
            {
                Console.WriteLine(monsterName[mNames] + " attacks " + yourName + " for " + monsterDMG + " damage because " + yourName + " doesn't know how to \ndefend himself.\n");
                playerHP -= monsterDMG;
                System.Threading.Thread.Sleep(500);
            }
            else
            {
                Console.WriteLine(monsterName[mNames] + " attacks " + yourName + " for " + (monsterDMG / 2) + " damage because " + yourName + " doesn't know how to \nfight like a man.\n");
                playerHP -= monsterDMG / 2;
                System.Threading.Thread.Sleep(500);
            }

            Console.WriteLine(yourName + " has " + playerHP + " health.\n");
            Console.WriteLine(monsterName[mNames] + " has " + monsterHP + " health.\n");

            **playerDMG = rand.Next(1, 8);
            monsterDMG = rand.Next(1, 10);**
        }

        //Win, Lose, Tie

        if (playerHP <= 0)
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine(yourName + " died! GG NO RE\n");
            Console.ResetColor();
        }
        if (monsterHP <= 0 && (playerHP > 0))
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Wow, you win. Congratulations.\n");
            Console.ResetColor();

        }
        else
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("It was a tie!");
            Console.ResetColor();
        }
        Console.ReadLine();

    }
}