我已经为命令行RPG编写了一些C#代码。问题是:每当我击中敌人时,它都会受到太高的伤害。
我对损害做了一个int变量,并且也只调用一次DamageCalc()。问题仍然出现。 我假设这可能是我在第154行中从敌人的生命力中减去伤害的方式。
这是重现此问题的代码。请原谅我的时间,但是我不知道问题出在哪里,所以我不能说它比我已经做的还要短。
在这里您可以看到5 HP的野猪。我击中野猪并造成4点伤害。可悲的是,野猪受到10点伤害,其HP降低至-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();
}
}
}
}
}
}
}
答案 0 :(得分:4)
您可以使用字符串插值进行输出,因为它更易于阅读。
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,因为您的HP数在2到3之间,所以您可以一次射击它们= D
答案 1 :(得分:2)
第一个错误:
Console.WriteLine(" A " + enemy1.Type + " appears! | ATTK: " + enemy1.HP + " | HP: " + enemy1.Attack);
您要在Attack上打印HP,反之亦然。
第二个错误:
enemy1.HP -= DamageCalc();
Console.WriteLine("You hit the " + enemy1.Type + "! | HP: " + (enemy1.HP - DamageCalc()) + " (You did -" + DamageCalc() + ")");
enemy1.HP已因损坏而减少(-=
),因此在打印时不要再减去(HP:“ +(enemy1.HP - DamageCalc())
)