【问题标题】:How to loop battle phase games c#如何循环战斗阶段游戏c#
【发布时间】:2021-05-04 07:25:27
【问题描述】:

我是 C# 新手,并不完全了解 for 循环。我正在开发 youtube 上 brackeys 推荐的基于文本的向导游戏。在游戏开始时,你会被要求选择你的法术,比如说“虫子(消耗 13 法力,造成 2 伤害)”,你有 3 个法术位和 10 生命值。到了攻击的时候,会进行计算等以减少敌人的(青蛙(10hp))hp。所以在青蛙攻击之后,我想重新开始战斗(循环),让我再次攻击,青蛙再次攻击。当我或敌人有0hp时,游戏将结束。我确定我的编码是正确的,但是我使用类和函数的方式太多了,我敢打赌有更简单的方法可以做到,但我只是不知道。

我不确定这个项目对于初学者来说是否太大,但是是的,哈哈。我正在考虑使用 for 循环,但我不完全理解它是如何工作的。 当我运行它时,它只会循环询问您要使用哪个咒语并减去您的法力和咒语槽的部分。它只会循环并且不会攻击另外,我对这个网站有点陌生,所以我不知道布置我的工作的正确方法。

class Wizard
    {
        //Wiz Profile
        public string name;
        public int  myHp = 10;
        public int myMana = 20;
        public string userAttack;
        public string favoriteSpell;
        private int spellSlots;
        private float xp;
        public static int Count;
        /////////////////////////

        //Spells Profile
        public int bugsDmg = 2;
        public int bugsMana = 13;
        public int rottingFodderDmg = 3;
        public int rottingFodderMana = 10;
        public int owlDmg = 5;
        public int owlMana = 10;
        //////////////////////////////////////////

        //Enemies Profile
        public int frogHp = 10;

       
             //Here is where I attempted to make the loop
        public void Gameover()
        {
            while(myHp > 0){
                Gameactive();
            }
        }       
                //I don't use this I just got this in brackets tutorial I will use it when
                  getting user name and spell not really sure of to use functions taking stuff 
                    in the brackets
         public Wizard(string _name, string _favoriteSpell)
        {
            name = _name;
            favoriteSpell =  _favoriteSpell;
            spellSlots = 3;
            xp = 0f;
            
            Count++;
        }
        
          //This is done after the spell calculation is done
        public void Battle()
        {
           userAttack = Console.ReadLine();
           
            if((userAttack == "Bugs") || (userAttack == "bugs"))
            {
                CastSpell();
                frogHp =frogHp - bugsDmg;
                Console.WriteLine("You casted " + userAttack + ". Dealth " + bugsDmg + " 
                 damage!");
                Thread.Sleep(5000);
                Console.WriteLine("----------------------------------------");
                Console.WriteLine("Frog hp: " + frogHp +"hp" +   " Your hp: " + myHp + "hp  Your 
                  mana: " + myMana + "mana");
                Console.WriteLine("Spell Slots: " + spellSlots + " slots left");
                Console.WriteLine("----------------------------------------");
                Thread.Sleep(3000);
            }
            if( (userAttack == " Rotting Fodder") || (userAttack == "rottingfodder") || 
               (userAttack == "rotting fodder"))
            {
                CastSpell();
                frogHp =frogHp - rottingFodderDmg;
                Console.WriteLine("You casted " + userAttack + ". Dealth " + rottingFodderDmg + 
                 " damage!");
                Thread.Sleep(5000);
                Console.WriteLine("----------------------------------------");
                Console.WriteLine("Frog hp: " + frogHp +"hp" +   " Your hp: " + myHp + "hp  Your
                mana: " + myMana + "mana");
                Console.WriteLine("Spell Slots: " + spellSlots + " slots left");
                Console.WriteLine("----------------------------------------");
                Thread.Sleep(3000);

            }

        }

           //This is called when an enemy(the frog) attacks
        public void Enemyattack()
        {
            myHp = myHp - rottingFodderDmg;
            Console.WriteLine("You got hit with rotting fodder");
            Thread.Sleep(2000);

            Console.WriteLine("Dealth" +  rottingFodderDmg + " damage!");
            Thread.Sleep(3000);
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("Frog hp: " + frogHp +"hp     Your hp: " + myHp + "hp     Your  
             mana: " + myMana + "mana");
            Console.WriteLine("Spell Slots: " + spellSlots + " slots left");
            Console.WriteLine("----------------------------------------");
        }

          //This is the calculation that happens when a player chooses a spell
        public void CastSpell(){
            if((userAttack == "Bugs") || (userAttack == "bugs"))
            {
                myMana = myMana - bugsMana;
                spellSlots--;
                Console.WriteLine("-15 mana,    -1 Spell Slot");
                Thread.Sleep(5000);
            }
              if( (userAttack == " Rotting Fodder") || (userAttack == "rottingfodder") ||   
                (userAttack == "rotting fodder"))
            {
                myMana = myMana - rottingFodderMana;
                spellSlots--;
                Console.WriteLine("-15 mana,    -1 Spell Slot");
                Thread.Sleep(5000);
                
            }
            Gameover();
        }

        public void Meditate(){
            Console.WriteLine(name + " meditates to regain spell slots.");
            spellSlots = 2;
        }
         
           ////////////////I assumed by making this into a func. I will be able to loop the part
            ///that takes user input then to attack again
        public void Gameactive()
            {
            Console.WriteLine("Select Your Spell to attack: \nBugs(costs 15 mana)   Rotting 

            Fodder(costs 4 mana)");

            
            Battle();

            Thread.Sleep(5000);
            Console.WriteLine("Standby Phase...");
            Thread.Sleep(2);
            Console.WriteLine("Frog is attacking...");
            Thread.Sleep(2000);

            Enemyattack();
            }
           
        
            
    }

   
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "WizardMadness";
            //Making a new wizard
            string userName;
            string userGender;
            string userFavoriteSpell;

            Wizard player1 = new Wizard(/*userName*/"Aaron", "Bugs" /*userFavoriteSpell*/);

          
            Console.WriteLine(Wizard.Count + " wizard created.");

          
            Console.WriteLine("Time to go into battle");
            Thread.Sleep(3000);
            Console.WriteLine("You ran into a Frog!");
            Thread.Sleep(2000);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine("Frog hp: 10hp    Your hp: 10hp   Your mana: 20mana\nSpell Slots: 
            3 slots available");
            Console.WriteLine("--------------------------------------------------");
            Thread.Sleep(5000);

            player1.Gameactive();
        

【问题讨论】:

  • 您使用while 循环,所以我假设您了解它们的工作原理:while 循环检查条件;如果为真,则进入循环体;如果到达正文的末尾或到达continue,则再次检查条件,依此类推。你会得到while 循环,对吗?
  • for 循环只是编写while 循环的一种更短的方式。 for(INIT; CONDITION; INC) BODY 只是写 { INIT; while (CONDITION) { BODY; INC } } 的简写方式,所以如果你理解 while 循环,for 循环非常简单。您只需将for 翻译成等效的while。请记住,如果您愿意,您可以始终for 循环编写为while 循环。 for 循环只是为了方便。
  • (有一点微妙之处,那就是forcontinue 转到INC。)

标签: c# function loops class


【解决方案1】:

欢迎编程!您可以做一些事情来完善您的代码并稍微清理一下。

  1. 在 if 语句中使用 .ToLower()
  2. 考虑为您的游戏状态使用 DO WHILE 循环
  3. 您可以嵌套您的 if 语句 如果 { 如果 { } 否则如果 { } 别的 { } }
  4. 使用库将清理您的代码,分离您的方法和函数可以真正帮助您排除故障和调试。让你的“主要”30 行而不是 300 行
  5. 了解 if 语句的实际作用。可以这样想,你的程序从上到下运行,所以一旦它通过该语句就完成了,除非有一个循环重新提示并让它再次通过它,但是一旦该语句被清除,它就不会再次看到(您可以将 if 语句嵌套在 do while 循环中) 希望这可以帮助!没有完整的答案,但编程是关于逻辑的,一旦你弄清楚它就会点击,祝你好运!

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2020-06-11
    • 2014-10-15
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 2015-03-22
    • 2014-01-21
    相关资源
    最近更新 更多