我在我的游戏中有这个老板的战斗,我正在为一个班级工作而且它没有按预期工作,在老板或玩家死后它打印出各种攻击和死亡信息。
我对代码很陌生,今年才开始上课
提前致谢!
这是老板的战斗:
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继续...
你击败了红色守护者!
老板已经死了!
你击中红色守护者29 按Enter键结束轮到你......
红色守护者打你19岁 按Enter键结束轮到他......
你击败了红色守护者!
但你也死了!
你死了!
答案 0 :(得分:0)
你使用递归循环和while循环,它太复杂了。很难理解你究竟是什么问题..你必须选择使用什么
1)循环
function P(){
while(boss.redKeeperAlive == true){
if(condition)
boss.redKeeperAlive = false;
}
}
2)递归
function P(){
if(boss.redKeeperAlive == true){
P()
}
}
但不是两个在同一时间。
答案 1 :(得分:0)
这是代码的修改版本。由于它是基于您的代码,它仍然需要更多的工作,但我注意到的一些事情是:
我建议你多读一些。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Boss
{
public int redKeeperHealth = 100;
}
public class Player
{
public int playerHealth = 100;
public string playerName = "Ace";
}
class Program
{
public static Boss boss = new Boss();
public static Player player = new Player();
static void Main(string[] args)
{
redKeeperBattle();
}
public static void redKeeperBattle()
{
//Player damage
Random pDmg = new Random();
int playerDmg;
//Player dodge
Random pDodge = new Random();
int playerDodge;
//Player crit
Random pCrit = new Random();
int playerCrit;
//Red keeper damage
Random rDmg = new Random();
int redDmg;
while (boss.redKeeperHealth > 0 && player.playerHealth > 0)
{
PrintToScreen();
int playerChoice = ReadPlayerChoice();
if (playerChoice == 1)
{
playerDmg = pDmg.Next(5, 16);
playerCrit = pCrit.Next(10, 16);
if (playerCrit == 15)
{
playerDmg += playerCrit;
Console.WriteLine("");
Console.Write("You critically struck ");
}
Console.WriteLine("");
Console.WriteLine("You hit The Red Keeper for {0}", playerDmg);
boss.redKeeperHealth -= playerDmg;
redDmg = rDmg.Next(1, 21);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("The Red Keeper ");
Console.ResetColor();
Console.WriteLine("hit you for {0}", redDmg);
Console.ForegroundColor = ConsoleColor.Gray;
redDmg = rDmg.Next(1, 21);
player.playerHealth -= redDmg;
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
Console.ResetColor();
}
else if (playerChoice == 2)
{
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("You attempt to dodge!");
Console.ResetColor();
Console.ReadLine();
playerDodge = pDodge.Next(1, 11);
if (playerDodge > 5)
{
redDmg = rDmg.Next(1, 21);
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();
}
else
{
redDmg = rDmg.Next(1, 21);
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();
}
}
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();
}
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.redKeeperHealth > 0)
{
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 && player.playerHealth > 0)
{
Console.Write("You have defeated ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The Red Keeper!");
Console.ResetColor();
Console.ReadLine();
redKeeperRoom();
}
}
}
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();
}
private static void PrintToScreen()
{
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();
}
private static int ReadPlayerChoice()
{
return int.Parse(Console.ReadLine());
}
}
}
-Enjoy