让一段时间循环在C#中为HP游戏工作



我不知道我尝试的方式是否有效,但我正在制作一个游戏,玩家和电脑不断攻击对方,直到有人打出0点生命值。我试着用一个while循环来不断要求玩家攻击,告诉某人死亡。我不能要求球员再次进攻。它一直说请输入1-6的数字,所以它只是将空白文本框作为输入。

public int userPickForWeapon(int userPick)
{ //this takes what number the user pick for a weapon and returns it
return userPick;
}
private void btnPlayerOneAttack_Click(object sender, EventArgs e)
{
do 
{
int userPickTwo = 0;
if (int.TryParse(txtPlayerWeapons.Text, out userPick))
{
userPickTwo = userPickForWeapon(userPick);

if (userPickTwo == 1)
{
bowAndArrow = rand.Next(8, 16); //picks a random number of damage
computerHP -= bowAndArrow; // takes away the players hp
lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
lblComputerResultsShow.Text = "You used Bow and Arrow n and did " + bowAndArrow + " damage";
txtPlayerWeapons.Text = ""; //clears the text box
}
else if (userPickTwo == 2)
{
sword = rand.Next(1, 10); //picks a random number of damage
computerHP -= sword; // takes away the players hp
lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
lblComputerResultsShow.Text = "You used Sword n and did " + sword + " damage";
txtPlayerWeapons.Text = ""; //clears the text box
}
else if (userPickTwo == 3)
{
fire = rand.Next(7, 15); //picks a random number of damage
computerHP -= fire; // takes away the players hp
lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
lblComputerResultsShow.Text = "You used Fire n and did " + fire + " damage";
txtPlayerWeapons.Text = ""; //clears the text box
}
else if (userPickTwo == 4)
{
ice = rand.Next(8, 15); //picks a random number of damage
computerHP -= ice; // takes away the players hp
lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
lblComputerResultsShow.Text = "You used Ice n and did " + ice + " damage";
txtPlayerWeapons.Text = ""; //clears the text box
}
else if (userPickTwo == 5)
{
wind = rand.Next(9, 15); //picks a random number of damage
computerHP -= wind; // takes away the players hp
lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
lblComputerResultsShow.Text = "You used Wind n and did " + wind + " damage";
txtPlayerWeapons.Text = ""; //clears the text box
}
else if (userPickTwo == 6)
{
water = rand.Next(1, 6); //picks a random number of damage
computerHP -= water; // takes away the players hp
lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
lblComputerResultsShow.Text = "You used water n and did " + water + " damage";
txtPlayerWeapons.Text = ""; //clears the text box
}
else
{
// Display an error message for if the user puts in something other than a number between 1 -6
MessageBox.Show("Please enter a number between 1 - 6");
txtPlayerWeapons.Focus();
}
}

else
{
// Display an error message for if the user puts in something other than a number
//inside the while loop
MessageBox.Show("Please enter a number between 1 - 6");
}
}while(playerOneHP >= 0 || computerHP >= 0);
}

这是一个操作顺序问题。通常,在这些场景中,您想要做的是:

  1. 输入while循环
  2. 选择现役战斗人员
  3. 如果现役战斗人员是玩家,则提示用户选择武器
  4. 计算所选武器的伤害
  5. 对对方球员造成伤害
  6. 如果任一玩家的生命值达到0,则退出while循环

相反,您的算法的工作方式如下:

  1. 确定活动玩家
  2. 提示用户使用主动武器
  3. 进入while循环
  4. 计算所选武器的伤害
  5. 对对方玩家造成伤害
  6. 如果任一玩家达到HP 0,则退出while循环

如果你的算法是这样构造的,它就无法工作,因为循环将无限旋转,除非有巨大的伤害峰值(可能是由杂散宇宙射线引起的(杀死对方玩家。

考虑重新构造算法,使其遵循第一种模式。

祝你好运!

附录

您试图在按钮单击事件中创建的循环已由Windows应用程序提供,称为事件循环。当应用程序启动时,它将进入轮询状态,等待用户单击用户界面中的项目、按键或单击鼠标按钮。这些操作引发事件,这些事件由您在应用程序中处理。完成此操作后,应用程序将重新进入轮询状态。该轮询状态相当于while循环(但并不完全如此(。

在我上面为您概述的步骤中,第一个和最后一个步骤指的是Windows事件循环(我的错误是没有更清楚(。

从按钮单击事件处理程序中删除while循环,并让操作系统在用户单击按钮时通知您。将逻辑保持在循环中,只需移除do {并关闭}即可。

最新更新