每次重新调用我的GameStart
函数时,ToastersHealth
都会返回500,因为它会重置值(正如您在程序注释中看到的)任何建议?我希望这个值在第一次只有500并且在玩家射击时不断减少
public static void Main(string[] args) //Main Function
{
GameStart(args);
}
public static void GameStart(string[] args) //GameStart Function
{
Console.WriteLine("Choose a gun to shoot at Toaster... ");
Console.Write("rocket/sniper/: ");
string playersGunChoice = Console.ReadLine();
Random randomweapondmg = new Random();
int weapondmg = 0;
switch (playersGunChoice.ToLower())
{
case "rocket":
Console.WriteLine("You chose a rocket.");
weapondmg = randomweapondmg.Next(75, 200);
Console.WriteLine("Your rocket does " + weapondmg + " to Toaster.");
break;
case "sniper":
Console.WriteLine("You chose a sniper.");
weapondmg = randomweapondmg.Next(50, 150);
Console.WriteLine("Your sniper does " + weapondmg + " to Toaster.");
break;
default:
Console.WriteLine("You didn't choose a gun.");
break;
}
int ToasterHealth = 500; // I don't want ToasterHealth to reset to 500 everytime the GameStart function gets called
int ToastersLeftHp = ToasterHealth - weapondmg;
Console.WriteLine("Toaster has " + ToastersLeftHp + " healthpoints left.");
if (ToastersLeftHp != 0)
do
{
GameStart(args);
} while (ToastersLeftHp < 0);
if (ToastersLeftHp == 0)
Console.WriteLine("You killed Toaster!");
else if (ToastersLeftHp < 100)
Console.WriteLine("Toaster is almost dead! He has " + ToastersLeftHp + " healthpoints left.");
else if (ToastersLeftHp < 0)
Console.WriteLine("You killed Toaster!");
}
答案 0 :(得分:1)
我认为你的问题是你从错误的财产中减去。尝试像这样做你的武器伤害减法。
player.TakeDamage(weapondmg);
...只需从您的方法中删除toastersLeftHp并替换为player.CurrentHealth。将你的代码的伤害处理部分移动到玩家也可以让你添加像盔甲一样的伤害修饰符。
class Player
{
public CurrentHealth { get; set;}
public Player()
{
CurrentHealth = 500;
}
public TakeDamage(int WeaponDamage)
{
CurrentHealth -= WeaponDamage;
}
}
答案 1 :(得分:1)
问题是你的循环很奇怪:
if (ToastersLeftHp != 0)
do
{
GameStart(args);
} while (ToastersLeftHp < 0);
如果ToastersLeftHp
变为负数,那就是无限循环。绝对不是你想要的。尝试围绕整个游戏逻辑循环:
public static void GameStart(string[] args) //GameStart Function
{
int ToasterHealth = 500; // happens just once, before the loop starts
do {
Console.WriteLine("Choose a gun to shoot at Toaster... ");
Console.Write("rocket/sniper/: ");
// all the stuff to make the gun hurt Toaster here
} while (ToasterHealth > 0);
DisplayMessage("You killed Toaster!");
Console.ReadLine();
}
答案 2 :(得分:0)
将ToasterHealth = 500声明为类变量,而不是GameStart函数中的变量。
答案 3 :(得分:-1)
为了在每次调用GameStart时都不重置ToasterHealth,需要在GameStart之外定义它。
static int ToasterHealth = 500;
public static void GameStart(string[] args) //GameStart Function
{
int ToastersLeftHp = ToasterHealth - weapondmg;
}
如果你想监视ToasterHealth以查看它被重置的位置,请使用它并将断点放在_ToasterHealth =值上;线。
static int _ToasterHealth = 500;
private int ToasterHealth
{
get { return _ToasterHealth; }
set
{
_ToasterHealth = value;
}
}
请确保不直接在任何地方设置_ToasterHealth,只能通过ToasterHealth属性。我想你会发现ToasterHealth不会失败的原因是你永远不会从中减去任何东西。
答案 4 :(得分:-1)
编辑: 根据OP解决方案要求修改了提供的示例。还包括武器类对象作为延长游戏未来的选项,允许武器类型,弹药类型和伤害潜力的灵活性。考虑一下作业。 (提示:几乎与我使用敌人对象的方式完全相同。)
class Program
{
static void Main(string[] args)
{
GameStart(args);
}
/// <summary>
/// This method starts the game.
/// Note: You can change the method GameStart signature to accept no parameters instead and still
/// get the exact same results. ( eg. Change GameStart signature from GameStart(string[] args) to GameStart(). )
/// </summary>
/// <param name="args">Irrelevant array or parameters. They are not presently used within the method.</param>
static void GameStart(string[] args)
{
// Create a new player
Player player = new Player();
// Create a new toaster enemy with a life of 500. (To demonstrate how to use the Enemy class to create different
// enemy types, comment or uncomment the enemy initialization lines below.)
Enemy enemy = new Enemy("Toaster", 500);
//Enemy enemy = new Enemy("King Toaster", 1000);
//Enemy enemy = new Enemy("Gargantuan Toaster", 10000);
// Until the enemy is dead, we will choose a gun or ammo type, and attack the enemy.
while (enemy.Health > 0)
{
// Ask the player to choose a gun in order to kill the enemy.
DisplayMessage(string.Format("Choose a gun to shoot at {0}...", enemy.EnemyType));
DisplayMessage("rocket/sniper: ");
// Get the gun selection from the player.
player.PlayerGunChoice = Console.ReadLine();
// Add a new line between the text to make it more easily readable.
DisplayMessage(string.Empty);
// Based on the player's gun selection, set the players weapon damage.
switch (player.PlayerGunChoice)
{
case "rocket":
DisplayMessage("You chose a rocket.");
player.WeaponDamage = GetRandomWeaponDamage(75, 200);
DisplayMessage(string.Format("Your rocket does {0} to {1}.", player.WeaponDamage, enemy.EnemyType));
break;
case "sniper":
DisplayMessage("You chose a sniper.");
player.WeaponDamage = GetRandomWeaponDamage(50, 150);
DisplayMessage(string.Format("Your sniper does {0} to {1}.", player.WeaponDamage, enemy.EnemyType));
break;
default:
DisplayMessage("You didn't choose a gun.");
break;
}
// Subtract the enemy's health by the player's current weapon damage.
enemy.Health -= player.WeaponDamage;
// Is the enemy health less than 0? If so, set it to 0. If an enemy is already 0, then it's dead, it can't lose anymore health.
// Otherwise, is the enemy health less than 100? If so, let the player know that the enemy is almost dead.
if (enemy.Health < 0)
enemy.Health = 0;
else if (enemy.Health < 100)
DisplayMessage(string.Format("{0} is almost dead!", enemy.EnemyType, enemy.Health));
// Tell the player what the current health is of the enemy.
DisplayMessage(string.Format("{0} has {1} healthpoints left.", enemy.EnemyType, enemy.Health));
// Wait for the player to read the message displayed before clearing the screen. We do this to keep the message window clutter free and readable.
Console.ReadLine();
// The damage done and health of the enemy has been read by the player. So lets clear the screen for the next attack.
ClearDisplay();
}
// Success! The health of the enemy has reached 0, so lets let the player know that the enemy is dead.
DisplayMessage(string.Format("You killed {0}!\n", enemy.EnemyType));
// End the program when the player has hit a key.
Console.ReadLine();
}
/// <summary>
/// Method used to get a random weapon damage based on the minimum and maximum damage potential of the weapon.
/// </summary>
/// <param name="min">Minimum possible damage.</param>
/// <param name="max">Maximum possible damage</param>
/// <returns></returns>
static int GetRandomWeaponDamage(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
/// <summary>
/// Simply a wrapper method that calls Console.WriteLine to display a message to the console window. This is for the purpose of understanding program flow.
/// </summary>
/// <param name="text">The method to display on the console window.</param>
static void DisplayMessage(string text)
{
Console.WriteLine(text);
}
/// <summary>
/// Simply a wrapper method that calls Console.Clear to clear the message window. This is for the purpose of understanding program flow.
/// </summary>
static void ClearDisplay()
{
Console.Clear();
}
}
/// <summary>
/// A player class object which encompasses the player gun choice and weapon damage.
/// </summary>
class Player
{
public int WeaponDamage { get; set; }
public string PlayerGunChoice { get; set; }
public Player()
{
}
}
/// <summary>
/// The enemy class object which encompasses the enemy of a defined type and designated health.
/// This object provides the option to create different types of enemies with varying differences in health.
/// (eg. King Toaster with 1000 maximum health points or Gargantuan Toaster with 10000 maximum health points.)
/// </summary>
class Enemy
{
public string EnemyType { get; set; }
public int Health { get; set; }
public Enemy()
{
}
public Enemy(string enemyType, int health)
{
EnemyType = enemyType;
Health = health;
}
}
/// <summary>
/// The weapon class object which encompasses the weapon type, ammo type, minimum and maximum damage potential of the weapon.
/// </summary>
class Weapon
{
public string WeaponType { get; set; } // (eg. Rocket Launcher, Sniper Rifle, Shotgun, Crossbow)
public string AmmoType { get; set; } // (eg. Explosive Rounds, buckshots, Armor piercing)
public int MinimumDamage { get; set; }
public int MaximumDamage { get; set; }
public Weapon()
{
}
public Weapon(string weaponType, string ammoType, int minimumDamage, int maximumDamage)
{
WeaponType = weaponType;
AmmoType = ammoType;
MinimumDamage = minimumDamage;
MaximumDamage = maximumDamage;
}
}