在C#中进行简单的神奇宝贝战斗的麻烦

时间:2019-03-20 19:58:22

标签: c# function

我正在用C#做一个非常简单的口袋妖怪对战游戏。主要思想是,第一个PokemonSquirtle)每转5圈会用一个函数加重伤害。 但是当我编译它时,Squirtle的损害是相同的。知道发生了什么吗?

代码如下:

using System;

namespace FirstProgram
{
    class MainClass
    {
        static void Main(string[]args)
        {
            int turn = 1;

            int potion1 = 0;

            //Creando y declarando Pikachu
            Pokemon Pikachu = new Pokemon();

            Pikachu.health = 190;

            Pikachu.damage = 40;
            //Creando y declarando Squirtle

            Pokemon Squirtle = new Pokemon();

            Squirtle.health = 190;

            Squirtle.damage = 40;

            while (Pikachu.health > 0 && Squirtle.health > 0)
            {
                if(turn == 1)
                {
                    Squirtle.health = Squirtle.health - Pikachu.damage;

                    turn = 0;

                    potion1++;

                    Console.WriteLine("Pikachu atacks!");
                    Console.WriteLine("Squirtle has only " + Squirtle.health + " PV!");

                    if(potion1 == 5)
                    {
                        potion1 = 0;
                        Squirtle.MoreDamage(Squirtle.damage);
                        Console.WriteLine("Squirtle damage now it's  " + Squirtle.damage);
                    }                 
                }
                else
                {
                    Pikachu.health = Pikachu.health - Squirtle.damage;
                    turn = 1;
                    Console.WriteLine("Squirtle atacks!");
                    Console.WriteLine("Pikachu has only " + Pikachu.health + " PV!");
                }
            }

            if(Pikachu.health > 0)
            {
                Console.WriteLine("Pikachu wins!");
            }
            else if(Squirtle.health > 0)
            {
                Console.WriteLine("Squirtle wins!");
            }        
        }
    }
}

Pokemon.cs

class Pokemon
{
    //Declarando la vida y el ataque
    public int health;
    public int damage;

    //Creando metodo para aumentar el ataque
    public  void MoreDamage(powerup)
    {
        powerup= powerup+ 5;
    }
}

这是输出: output

我尝试做其他事情,例如更改函数的代码,但是所有尝试都具有相同的结果:damage无法启动。

1 个答案:

答案 0 :(得分:1)

MoreDamage()方法仅修改powerup参数。您需要改为编辑Pokemon的{​​{1}}属性。

更改此:

damage

对此:

public  void MoreDamage(powerup)
{
    powerup= powerup+ 5;
}

然后,将您的呼叫更改为public void MoreDamage(int powerup) { damage += powerup; } ,以传递您希望增加神奇宝贝的伤害值的值:

MoreDamage()