我如何修复我的伤害计算如此之高?
How do I fix my damage calculation being so high?
我已经为命令行 RPG 编写了一些 C# 代码。问题是:每当我击中敌人时,它受到的伤害太高了。
我为伤害创建了一个 int 变量,并且只调用了一次 DamageCalc()。问题仍然存在。
我假设这可能是我在第 154 行中从敌人的 HP 中减去伤害的方式。
这是重现问题的代码。对不起,它的长度,但我不知道问题发生的确切位置,所以我不能让它比我已经做的更短。
在这里你可以看到5 HP的野猪。我击中了野猪并造成了 4 点伤害。可悲的是,野猪受到 10 点伤害,它的生命值下降到 -5。
using System;
namespace ConsoleApp2
{
class Player
{
Archer archer = new Archer();
Knight knight = new Knight();
Assasin assasin = new Assasin();
CurrentWeapon currentWeapon = new CurrentWeapon();
public string Name { get; set; }
public string Klasse { get; set; }
public int HP { get; set; } = 20;
public int Level { get; set; } = 0;
}
class CurrentWeapon
{
public string WeaponName { get; set; }
public float DamageMultiplier { get; set; } = 1;
}
class Enemy
{
public string Type { get; set; }
public int Attack { get; set; }
public int HP { get; set; }
public int Experience { get; set; }
public bool Dead { get; set; } = false;
//Konstruktor
public Enemy(string _type, int _attack, int _hp, int _experience)
{
Type = _type;
Attack = _attack;
HP = _hp;
Experience = _experience;
}
}
class Archer
{
public string ClassName { get; set; } = "Archer";
public int BaseDamage { get; set; } = 6;
public string WeaponType { get; set; } = "Bow";
}
class Knight
{
public string ClassName { get; set; } = "Knight";
public int BaseDamage { get; set; } = 5;
public string WeaponType { get; set; } = "Greatsword";
}
class Assasin
{
public string ClassName { get; set; } = "Assasin";
public int BaseDamage { get; set; } = 4;
public string WeaponType { get; set; } = "Dagger";
}
class Program
{
static int GenerateNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
static void Main(string[] args)
{
Archer archer = new Archer();
Knight knight = new Knight();
Assasin assasin = new Assasin();
CurrentWeapon currentWeapon = new CurrentWeapon();
Player player = new Player();
int DamageCalc()
{
if (player.Klasse == "Archer")
{
float outputDamage = archer.BaseDamage * currentWeapon.DamageMultiplier;
return (int)Math.Round(outputDamage);
}
if (player.Klasse == "Knight")
{
float outputDamage = knight.BaseDamage * currentWeapon.DamageMultiplier;
return (int)Math.Round(outputDamage);
}
if (player.Klasse == "Assasin")
{
float outputDamage = assasin.BaseDamage * currentWeapon.DamageMultiplier;
return (int)Math.Round(outputDamage);
}
else { return 0; }
}
Console.WriteLine(" What shall you be called?");
player.Name = Console.ReadLine();
Console.ReadKey();
Console.WriteLine(player.Name);
Console.Clear();
Console.WriteLine("Good, " + player.Name + " choose your class.");
Console.WriteLine();
Console.WriteLine(archer.ClassName + " | DMG: " + archer.BaseDamage + " | Weapon Type: " + archer.WeaponType);
Console.WriteLine(knight.ClassName + " | DMG: " + knight.BaseDamage + " | Weapon Type: " + knight.WeaponType);
Console.WriteLine(assasin.ClassName + " | DMG: " + assasin.BaseDamage + " | WeaponType : " + assasin.WeaponType);
string UserClass = Console.ReadLine();
if (UserClass == "Archer")
{
player.Klasse = "Archer";
Console.WriteLine("You are now an Archer.");
currentWeapon.WeaponName = "Oak bow";
Console.WriteLine("You got a " + currentWeapon.WeaponName + ".");
}
if (UserClass == "Knight")
{
player.Klasse = "Knight";
Console.WriteLine("You are now a Knight.");
currentWeapon.WeaponName = "Wooden sword";
Console.WriteLine("You got a " + currentWeapon.WeaponName + ".");
}
if (UserClass == "Assasin")
{
player.Klasse = "Assasin";
Console.WriteLine("You are now an Assasin.");
currentWeapon.WeaponName = "Wooden Dagger";
Console.WriteLine("You got a " + currentWeapon.WeaponName + ".");
}
Console.ReadKey();
bool Play = true;
while (Play)
{
Console.Clear();
Console.WriteLine("Press any Key to walk.");
Console.ReadKey();
Console.Clear();
int randomNumber = Program.GenerateNumber(0, 101);
if (randomNumber <= 101 && randomNumber >= 0)
{
Enemy enemy1 = new Enemy("Wild Boar", Program.GenerateNumber(5, 10), Program.GenerateNumber(2, 4), Program.GenerateNumber(6, 9));
Console.WriteLine(" A " + enemy1.Type + " appears! | ATTK: " + enemy1.HP + " | HP: " + enemy1.Attack);
while (!enemy1.Dead)
{
Console.WriteLine("Use your " + currentWeapon.WeaponName + " and attack by pressing 'Q'.");
if (Console.ReadKey(true).Key == ConsoleKey.Q)
{
int hitDamage = DamageCalc();
enemy1.HP -= hitDamage;
Console.WriteLine("You hit the " + enemy1.Type + "! | HP: " + enemy1.HP + " (You did -" + hitDamage +")");
if (enemy1.HP <= 0)
{
Console.WriteLine("You killed the " + enemy1.Type + "! | +" + enemy1.Experience + " Experience!");
enemy1.Dead = true;
}
Console.ReadKey();
}
}
}
}
}
}
}
第一个错误:
Console.WriteLine(" A " + enemy1.Type + " appears! | ATTK: " + enemy1.HP + " | HP: " + enemy1.Attack);
你正在打印 HP on Attack,反之亦然。
第二个错误:
enemy1.HP -= DamageCalc();
Console.WriteLine("You hit the " + enemy1.Type + "! | HP: " + (enemy1.HP - DamageCalc()) + " (You did -" + DamageCalc() + ")");
enemy1.HP 已经减少了 (-=
) 的伤害,所以当你打印它时不要再减去 (HP: " + (enemy1.HP - DamageCalc())
)
您可以对输出使用字符串插值,因为它更易于阅读。
Console.WriteLine($" A {enemy1.Type} appears! ATTACK: {enemy1.Attack} HP {enemy1.HP}");
var damage = enemy1.HP -= DamageCalc();
Console.WriteLine($"You hit the {enemy1.Type} HP: {damage} You did {DamageCalc()} damage") ;
而且你可以考虑增加randomNumber,因为你的生命值在2到3之间,所以你可以一枪打死他们=D
我已经为命令行 RPG 编写了一些 C# 代码。问题是:每当我击中敌人时,它受到的伤害太高了。
我为伤害创建了一个 int 变量,并且只调用了一次 DamageCalc()。问题仍然存在。 我假设这可能是我在第 154 行中从敌人的 HP 中减去伤害的方式。
这是重现问题的代码。对不起,它的长度,但我不知道问题发生的确切位置,所以我不能让它比我已经做的更短。
在这里你可以看到5 HP的野猪。我击中了野猪并造成了 4 点伤害。可悲的是,野猪受到 10 点伤害,它的生命值下降到 -5。
using System;
namespace ConsoleApp2
{
class Player
{
Archer archer = new Archer();
Knight knight = new Knight();
Assasin assasin = new Assasin();
CurrentWeapon currentWeapon = new CurrentWeapon();
public string Name { get; set; }
public string Klasse { get; set; }
public int HP { get; set; } = 20;
public int Level { get; set; } = 0;
}
class CurrentWeapon
{
public string WeaponName { get; set; }
public float DamageMultiplier { get; set; } = 1;
}
class Enemy
{
public string Type { get; set; }
public int Attack { get; set; }
public int HP { get; set; }
public int Experience { get; set; }
public bool Dead { get; set; } = false;
//Konstruktor
public Enemy(string _type, int _attack, int _hp, int _experience)
{
Type = _type;
Attack = _attack;
HP = _hp;
Experience = _experience;
}
}
class Archer
{
public string ClassName { get; set; } = "Archer";
public int BaseDamage { get; set; } = 6;
public string WeaponType { get; set; } = "Bow";
}
class Knight
{
public string ClassName { get; set; } = "Knight";
public int BaseDamage { get; set; } = 5;
public string WeaponType { get; set; } = "Greatsword";
}
class Assasin
{
public string ClassName { get; set; } = "Assasin";
public int BaseDamage { get; set; } = 4;
public string WeaponType { get; set; } = "Dagger";
}
class Program
{
static int GenerateNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
static void Main(string[] args)
{
Archer archer = new Archer();
Knight knight = new Knight();
Assasin assasin = new Assasin();
CurrentWeapon currentWeapon = new CurrentWeapon();
Player player = new Player();
int DamageCalc()
{
if (player.Klasse == "Archer")
{
float outputDamage = archer.BaseDamage * currentWeapon.DamageMultiplier;
return (int)Math.Round(outputDamage);
}
if (player.Klasse == "Knight")
{
float outputDamage = knight.BaseDamage * currentWeapon.DamageMultiplier;
return (int)Math.Round(outputDamage);
}
if (player.Klasse == "Assasin")
{
float outputDamage = assasin.BaseDamage * currentWeapon.DamageMultiplier;
return (int)Math.Round(outputDamage);
}
else { return 0; }
}
Console.WriteLine(" What shall you be called?");
player.Name = Console.ReadLine();
Console.ReadKey();
Console.WriteLine(player.Name);
Console.Clear();
Console.WriteLine("Good, " + player.Name + " choose your class.");
Console.WriteLine();
Console.WriteLine(archer.ClassName + " | DMG: " + archer.BaseDamage + " | Weapon Type: " + archer.WeaponType);
Console.WriteLine(knight.ClassName + " | DMG: " + knight.BaseDamage + " | Weapon Type: " + knight.WeaponType);
Console.WriteLine(assasin.ClassName + " | DMG: " + assasin.BaseDamage + " | WeaponType : " + assasin.WeaponType);
string UserClass = Console.ReadLine();
if (UserClass == "Archer")
{
player.Klasse = "Archer";
Console.WriteLine("You are now an Archer.");
currentWeapon.WeaponName = "Oak bow";
Console.WriteLine("You got a " + currentWeapon.WeaponName + ".");
}
if (UserClass == "Knight")
{
player.Klasse = "Knight";
Console.WriteLine("You are now a Knight.");
currentWeapon.WeaponName = "Wooden sword";
Console.WriteLine("You got a " + currentWeapon.WeaponName + ".");
}
if (UserClass == "Assasin")
{
player.Klasse = "Assasin";
Console.WriteLine("You are now an Assasin.");
currentWeapon.WeaponName = "Wooden Dagger";
Console.WriteLine("You got a " + currentWeapon.WeaponName + ".");
}
Console.ReadKey();
bool Play = true;
while (Play)
{
Console.Clear();
Console.WriteLine("Press any Key to walk.");
Console.ReadKey();
Console.Clear();
int randomNumber = Program.GenerateNumber(0, 101);
if (randomNumber <= 101 && randomNumber >= 0)
{
Enemy enemy1 = new Enemy("Wild Boar", Program.GenerateNumber(5, 10), Program.GenerateNumber(2, 4), Program.GenerateNumber(6, 9));
Console.WriteLine(" A " + enemy1.Type + " appears! | ATTK: " + enemy1.HP + " | HP: " + enemy1.Attack);
while (!enemy1.Dead)
{
Console.WriteLine("Use your " + currentWeapon.WeaponName + " and attack by pressing 'Q'.");
if (Console.ReadKey(true).Key == ConsoleKey.Q)
{
int hitDamage = DamageCalc();
enemy1.HP -= hitDamage;
Console.WriteLine("You hit the " + enemy1.Type + "! | HP: " + enemy1.HP + " (You did -" + hitDamage +")");
if (enemy1.HP <= 0)
{
Console.WriteLine("You killed the " + enemy1.Type + "! | +" + enemy1.Experience + " Experience!");
enemy1.Dead = true;
}
Console.ReadKey();
}
}
}
}
}
}
}
第一个错误:
Console.WriteLine(" A " + enemy1.Type + " appears! | ATTK: " + enemy1.HP + " | HP: " + enemy1.Attack);
你正在打印 HP on Attack,反之亦然。
第二个错误:
enemy1.HP -= DamageCalc();
Console.WriteLine("You hit the " + enemy1.Type + "! | HP: " + (enemy1.HP - DamageCalc()) + " (You did -" + DamageCalc() + ")");
enemy1.HP 已经减少了 (-=
) 的伤害,所以当你打印它时不要再减去 (HP: " + (enemy1.HP - DamageCalc())
)
您可以对输出使用字符串插值,因为它更易于阅读。
Console.WriteLine($" A {enemy1.Type} appears! ATTACK: {enemy1.Attack} HP {enemy1.HP}");
var damage = enemy1.HP -= DamageCalc();
Console.WriteLine($"You hit the {enemy1.Type} HP: {damage} You did {DamageCalc()} damage") ;
而且你可以考虑增加randomNumber,因为你的生命值在2到3之间,所以你可以一枪打死他们=D