我正在用C#做一个非常简单的口袋妖怪对战游戏。主要思想是,第一个Pokemon
(Squirtle
)每转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!");
}
}
}
}
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;
}
}
我尝试做其他事情,例如更改函数的代码,但是所有尝试都具有相同的结果:damage
无法启动。
答案 0 :(得分:1)
MoreDamage()
方法仅修改powerup
参数。您需要改为编辑Pokemon
的{{1}}属性。
更改此:
damage
对此:
public void MoreDamage(powerup)
{
powerup= powerup+ 5;
}
然后,将您的呼叫更改为public void MoreDamage(int powerup)
{
damage += powerup;
}
,以传递您希望增加神奇宝贝的伤害值的值:
MoreDamage()