我在控制台应用程序"游戏"中遇到的问题



所以我在一个 c# 控制台应用程序项目中制作了一个"游戏",它基本上是一个必须杀死怪物的英雄,英雄和怪物都有 HP 和随机数量的伤害(应该是这样(。 我有几个小问题对我来说没有意义。我在游戏中添加了再生药水,它显然会为玩家增加 10-30 之间的随机数量的生命值。

//Regenerating or not
if (isRegen == false) //if it doesn't
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You've failed to regenerate!");
Console.WriteLine("Enemy: " + monster.name);
Console.WriteLine("Enemy's hp: " + monster.hp);
GetDamage();
}
else //if it does
{
if (myHP + regenAmount > 100 || myHP == 100)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You can't regenerate above 100 hp.");
Game();
}
else
{
if (potionCounter == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You are out of health potions. Cannot regenerate!");
Game();
}
potionCounter--;
myHP += regenAmount;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("You consumed a Health Potion!");
Console.WriteLine("Your hp was regenerated successfully! HP is raised by " + regenAmount);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Enemy: " + monster.name);
Game();
}
}

现在,正如您在上面的代码中看到的那样,我做了一个 if 语句来检查 myHP 和再生量的总和是否高于 100,显示一条消息,显示"您无法再生超过 100 hp"。问题是,当我尝试这样做时,有时它确实会显示消息 如这里所述, 但最终它决定在这里显示"你未能再生!",游戏继续进行,玩家被击中。(这显然不应该发生(。 药水计数器甚至还有另一个类似的问题。如代码所示:

if (potionCounter == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You are out of health potions. Cannot regenerate!");
Game();
}

每当药水计数器达到 0 时,它应该显示一条消息,指出用户药水用完。它发生并有效,但与前面的问题类似,它有时会忽略 if 语句,并允许用户要么治愈并继续减少 potionCounter 变量,要么无法再生并被怪物击中。

我知道其中一些代码有点糟糕,那是因为我对编程有点陌生,但我尽我所能发展自己并尽可能多地学习,这就是我决定在这里分享它的原因。

感谢您阅读它,希望您找到解决方案:)

编辑:Game()的代码:

static void Game()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(monster.name + "'s hp: " + monster.hp);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Your hp: " + myHP + "n");
Console.ForegroundColor = ConsoleColor.White;
while(monster.hp > 0 && myHP > 0) //Running until the enemy or the player dies.
{
Console.Write("A - ATTACK (Give damage but always get hit)nD - DEFEND (50% chance of not getting hit)nR - REGENERATE (Regenerates HP, but if it fails you get hit) - ");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(potionCounter + " POTIONS LEFTn");
string input = Console.ReadLine();
//ATTACK
if (input == "a" || input == "A")
{
Console.Clear();
Attack();
}
else if (input == "d" || input == "D")
{
Console.Clear();
Defend();
}
else if (input == "r" || input == "R")
{
Console.Clear();
Regenerate();
}
else
{
Console.Clear();
Game();
}
}
}

这就是我构建这样一个简单的游戏的方式。

你似乎已经有一个"怪物"类 - 很好!- 但为了简单起见,它从这个例子中省略了。同样,您可能会将Player封装为一个类,以单独跟踪每个玩家的药水和HP。

无论哪种方式,这个想法都是有

  • 由"游戏结束"标志控制的无限循环
  • 一个运行单回合的功能——检查游戏是否结束、打印游戏状态、等待输入、处理它、运行 AI 动作(鉴于我们的怪物是同类的简单食人魔,它只是猛烈抨击你的头(
  • 游戏中发生的不同事件的单独函数
  • 。还有一个辅助函数,可让您在一次调用中更改控制台颜色并打印一行文本;-(

我希望这有帮助!我知道,这不完全是一个激光聚焦的精确答案......

class Game
{
private int playerHp = 100;
private int monsterHp = 100;
private int nPotions = 3;
private readonly Random random = new Random();
private bool gameOver = false;
static void ColorPrint(string message, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
}
public void Play()
{
while (!gameOver)
{
RunTurn();
}
}
void RunTurn()
{
CheckGameOver();
if (gameOver) {
return;
}
ColorPrint(String.Format("Monster HP: {0}", monsterHp), ConsoleColor.Red);
ColorPrint(String.Format("   Your HP: {0}", playerHp), ConsoleColor.Green);
ColorPrint(String.Format("   Potions: {0}", nPotions), ConsoleColor.Yellow);
ColorPrint("(A)ttack / (P)otion / anything else to skip your turn?", ConsoleColor.White);
var command = Console.ReadLine().Trim().ToLower();
switch (command)
{
case "a":
DoPlayerAttack();
break;
case "p":
DoPlayerPotion();
break;
default:
ColorPrint("You decide to loiter around.", ConsoleColor.White);
break;
}
DoEnemyAttack();
}
void CheckGameOver() {
if (monsterHp <= 0)
{
gameOver = true;
ColorPrint("The monster is slain!", ConsoleColor.White);
}
if (playerHp <= 0)
{
gameOver = true;
ColorPrint("You are dead. :(", ConsoleColor.White);
}
}
void DoPlayerAttack()
{
var damage = random.Next(1, 10);
ColorPrint(String.Format("You strike the monster for {0} damage.", damage), ConsoleColor.White);
monsterHp -= damage;
}
void DoPlayerPotion()
{
if (nPotions > 0)
{
var heal = random.NextDouble() < 0.7 ? random.Next(5, 15) : 0;
if (heal > 0)
{
ColorPrint(String.Format("You heal for {0} HP.", heal), ConsoleColor.Gray);
}
else
{
ColorPrint("That potion was a dud.", ConsoleColor.Gray);
}
playerHp = Math.Min(100, playerHp + heal);
nPotions--;
}
else
{
ColorPrint("You rummage through your belongings to find no potions.", ConsoleColor.Gray);
}
}
void DoEnemyAttack()
{
var damage = random.Next(1, 10);
ColorPrint(String.Format("The monster nibbles on you for {0} damage.", damage), ConsoleColor.White);
playerHp -= damage;
}
}

只是专注于你的这段代码:

if (potionCounter == 0)
{
// ...
}
potionCounter--;
myHP += regenAmount;

想想当药水计数器为零时会发生什么:
- 然后它会将药水计数器设置为 -1
- 它会给你 HP

您可能打算将此代码放在else语句中。

此外,检查if (potionCounter <= 0)将比精确测试零更可靠。

相关内容

最新更新