gpt4 book ai didi

c# - 让 while 循环在 C# 中为 HP 游戏工作

转载 作者:行者123 更新时间:2023-12-05 04:41:21 24 4
gpt4 key购买 nike

我不知道我尝试这样做的方式是否可行,但我正在制作一个游戏,其中玩家和计算机不断相互攻击,直到有人的 HP 为 0。我正在尝试使用一段时间循环不断要求玩家攻击告诉某人死亡。我无法要求玩家进行另一次攻击。它一直说请输入 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. 如果任一玩家的 HP 为 0,则退出 while 循环

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

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

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

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

祝你好运!

附录

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

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

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

关于c# - 让 while 循环在 C# 中为 HP 游戏工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70102999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com