在我的 C# 游戏 TextQuest 中,战斗系统正在进行中,我遇到了一个问题。如果玩家错过了怪物也错过了(总是同时)。这是使用的代码:
private void button1_Click(object sender, EventArgs e)
{
Monster_Class mc = new Monster_Class();
Account_Class ac = new Account_Class();
if (hitOrMiss())
{
int hit = -1;
hit = mc.hitAmount(ac.getAtk(Properties.Settings.Default.CurrentUser), mc.getDef(monster));
mhealth -= hit;
if (hit == -1)
{
setText("You missed.");
}
else
{
setText("You hit " + hit + ".");
}
monsterHit();
update();
}
else
{
setText("You missed.");
monsterHit();
}
}
private void monsterHit()
{
Monster_Class mc = new Monster_Class();
Account_Class ac = new Account_Class();
if (hitOrMiss())
{
int hit = -1;
hit = mc.hitAmount(mc.getAtk(monster), ac.getDef(Properties.Settings.Default.CurrentUser));
phealth -= hit;
if (hit == -1)
{
addText(monster + " missed.");
}
else
{
addText(monster + " hit " + hit + ".");
}
}
else
{
addText(monster + " missed.");
}
}
private bool hitOrMiss()
{
bool hit = true;
Random rand = new Random();
if (rand.Next(101) < 15)
{
hit = false;
}
return hit;
}
public int hitAmount(int Atk, int Def)
{
int hit = -1;
Random rand = new Random();
int deturm = rand.Next(6);
try
{
hit = ((Atk + deturm * 3) / Def + 1) / 2;
if (hit == 0)
{
hit = 1;
}
}
catch { }
return hit;
}
另外,如果您对造成的伤害量有更好的想法,请告诉我。因为我只是把数字和符号放在一起