我在我的遊戲中有這個boss戰,我正在爲一個班級工作,並且它不按預期工作,在老闆或玩家死後打印各種攻擊和死亡消息。C#雖然循環不按預期工作
我很新的代碼,今年剛開始爲一類
在此先感謝!
這裏的boss戰:
public static void redKeeperBattle()
{
if (boss.redKeeperHealth <= 0 && (player.playerHealth <= 0))
{
Console.Write("You have defeated ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The Red Keeper!");
Console.ResetColor();
Console.ReadLine();
Console.Write("But you have also ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Died!");
Console.ResetColor();
Console.ReadLine();
playerDeath();
}
if (player.playerHealth <= 0)
{
boss.redKeeperAlive = false;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("The Red Keeper ");
Console.ResetColor();
Console.WriteLine("has killed {0}, how sad...", player.playerName);
Console.ReadLine();
playerDeath();
}
if (boss.redKeeperHealth <= 0)
{
boss.redKeeperAlive = false;
Console.Write("You have defeated ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The Red Keeper!");
Console.ResetColor();
Console.ReadLine();
redKeeperRoom();
}
while (boss.redKeeperAlive == true)
{
//Player damage
Random pDmg = new Random();
int playerDmg = pDmg.Next(5, 16);
//Player dodge
Random pDodge = new Random();
int playerDodge = pDodge.Next(1, 11);
//Player crit
Random pCrit = new Random();
int playerCrit = pCrit.Next(10, 16);
//Red keeper damage
Random rDmg = new Random();
int redDmg = rDmg.Next(1, 21);
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("--------------------------------------------------------------------------------");
Console.WriteLine("");
Console.WriteLine("THE RED KEEPER: {0} HP", boss.redKeeperHealth);
Console.ResetColor();
Console.WriteLine("");
Console.Write("{0}: ", player.playerName);
if (player.playerHealth > 50)
{
Console.ForegroundColor = ConsoleColor.Green;
}
else if (player.playerHealth > 20)
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
else {
Console.ForegroundColor = ConsoleColor.Red;
}
Console.WriteLine("{0}", player.playerHealth);
Console.WriteLine("");
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("What would you like to do?");
Console.WriteLine("1) Attack");
Console.WriteLine("2) Dodge");
Console.WriteLine("");
Console.ResetColor();
string attackChoice = Console.ReadLine();
if (attackChoice == "1" && (playerCrit == 15))
{
playerDmg += playerCrit;
Console.WriteLine("");
Console.Write("You critically struck ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The Red Keeper ");
Console.ResetColor();
Console.WriteLine("for {0} HP!", playerDmg);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Press Enter to end your turn...");
boss.redKeeperHealth -= playerDmg;
Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("The Red Keeper ");
Console.ResetColor();
Console.WriteLine("hit you for {0}", redDmg);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Press Enter to end his turn...");
Console.ResetColor();
player.playerHealth -= redDmg;
Console.ReadLine();
redKeeperBattle();
}
if (attackChoice == "1")
{
Console.WriteLine("");
Console.WriteLine("You hit The Red Keeper for {0}", playerDmg);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Press Enter to end your turn...");
boss.redKeeperHealth -= playerDmg;
Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("The Red Keeper ");
Console.ResetColor();
Console.WriteLine("hit you for {0}", redDmg);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Press Enter to end his turn...");
Console.ResetColor();
player.playerHealth -= redDmg;
Console.ReadLine();
redKeeperBattle();
}
if (attackChoice == "2" && (playerDodge > 5))
{
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("You attempt to dodge!");
Console.ResetColor();
Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("You successfully dodged and gained 10 HP!");
Console.ReadLine();
player.playerHealth += 10;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("The Red Keeper ");
Console.ResetColor();
Console.WriteLine("would have hit you for {0} HP!", redDmg);
Console.ReadLine();
redKeeperBattle();
}
if (attackChoice == "2")
{
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("You attempt to dodge!");
Console.ResetColor();
Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Dodge failed!");
Console.ResetColor();
Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("The Red Keeper ");
Console.ResetColor();
Console.WriteLine("hit you for {0}", redDmg);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Press Enter to end his turn...");
Console.ResetColor();
player.playerHealth -= redDmg;
Console.ReadLine();
redKeeperBattle();
}
else {
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("An error occurred! Please try again!");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Press Enter to continue...");
Console.ResetColor();
Console.ReadLine();
redKeeperBattle();
}
}
}
public static void redKeeperRoom()
{
//Continue game
Console.WriteLine ("The boss has died!");
Console.ReadLine();
}
public static void playerDeath()
{
//End game
Console.WriteLine ("You have Died!");
Console.ReadLine();
}
所以會發生什麼是當人死後,它只是像修建垃圾郵件:
你打紅守護者12 按Enter鍵結束輪到你了.. 。
紅色守護打你14 按Enter鍵結束輪到他...
你已經打敗了紅色的守護者!
老闆死了!
發生錯誤!請再試一次! 按Enter繼續...
您已擊敗The Red Keeper!
老闆死了!
你打紅守護者29 按Enter鍵結束輪到你...
紅色守護打你19 按Enter鍵結束輪到他...
你已經打敗了的紅色的守護者!
但你也死了!
你已經死了!
你有沒有考慮時會發生什麼'redKeeperBattle()'因爲你是遞歸(這你可能不希望做調用它的回報... )。即'redKeeperBattle()'調用調用'redKeeperBattle()'的redKeeperBattle()'。現在,當第三個人回來時,控制權交還給第二個人。第二個電話是否正確?您可以使用靜態int來計算呼叫號碼並打印一些調試信息,以告訴您哪個呼叫正在輸出意外數據。 – John3136
:/代碼牆 –
對不起@ J.SMTBCJ15我真的不知道我在做什麼。 :/ – Josh