免责声明:非常新的HTML和javascript。还有,很抱歉格式化,我习惯了lua。
我正在尝试这样做,以便每次for循环重复并拉出提示符时,它都会主动更新和显示HTML文档上的统计信息。
统计数据指的是:
此外,我有一个问题,当龙或用户获胜时,所显示的生命值是上次攻击之前的健康状态。
任何帮助都是非常感谢的。提前谢谢。
var userDmg = document.body.querySelector(".userDamage")
var dragDmg = document.body.querySelector(".dragonDamage")
var userHthTotal = document.body.querySelector(".userHealth")
var dragHthTotal = document.body.querySelector(".dragonHealth")
var userOut = document.body.querySelector(".userOutcome")
var dragOut = document.body.querySelector(".dragonOutcome")
for (var userHealth = 50, dragHealth = 100; userHealth > 0 && dragHealth > 0;)
{
var userInput = Number(prompt("A dragon has appeared! How many hits do you want to deal?"))
userHthTotal.innerHTML = "Your health: " +userHealth
dragHthTotal.innerHTML = "Dragon's health: " +dragHealth
if(userInput > 5)
{userOut.innerHTML = "You exhausted yourself and died."
userHealth = 0}
else if(userInput <= 0)
{userOut.innerHTML = "The dragon was disappointed with your non-exstant attack. You died."
userHealth = 0}
else if(userInput === 5)
{userDmg.innerHTML = "Your attacks were weak. You deal 1 damage."
dragDmg.innerHTML = "The dragon is able to land a critical hit and deals 20 damage."
dragHealth = dragHealth - 1
userHealth = userHealth - 20}
else if(userInput === 1 || userInput === 2 || userInput === 3 || userInput === 4)
{var userAttackDmg = Number(userInput) * (Math.floor(Math.random() * 20))
userDmg.innerHTML = "You dealt " +userAttackDmg+ " damage!"
dragHealth = dragHealth - userAttackDmg
var dragAttackDmg = Math.floor(Math.random() * 20) + 10
dragDmg.innerHTML = "The dragon dealt " +dragAttackDmg+ " damage!"
userHealth = userHealth - dragAttackDmg}
else
{userOut.innerHTML = "You confused yourself. You died."
userHealth = 0}
if(userHealth < 0)
{userOut.innerHTML = "Tough battle, but the dragon kills you."
dragOut.innerHTML = "The dragon goes on to pay a visit to your village."}
if(dragHealth < 0)
{userOut.innerHTML = "You are victorious!"
dragOut.innerHTML = "However, watch out, the spirit of the dragon will probably haunt you for the rest of your life."}
}发布于 2021-03-07 07:56:43
您希望移动您的状态更新(这2行:)
userHthTotal.innerHTML = "Your health: " +userHealth
dragHthTotal.innerHTML = "Dragon's health: " +dragHealth在你的大如果块下面,甚至一直到你的for循环的底部。
另外,对于最后两个if-块,我怀疑您希望使用<=而不是<。
if (userHealth <= 0) ...
if (dragHealth <= 0) ...调整后的版本:
var userDmg = document.body.querySelector(".userDamage")
var dragDmg = document.body.querySelector(".dragonDamage")
var userHthTotal = document.body.querySelector(".userHealth")
var dragHthTotal = document.body.querySelector(".dragonHealth")
var userOut = document.body.querySelector(".userOutcome")
var dragOut = document.body.querySelector(".dragonOutcome")
for (var userHealth = 50, dragHealth = 100; userHealth > 0 && dragHealth > 0;)
{
var userInput = Number(prompt("A dragon has appeared! How many hits do you want to deal?"))
if (userInput > 5) {
userOut.innerHTML = "You exhausted yourself and died."
userHealth = 0
} else if (userInput <= 0) {
userOut.innerHTML = "The dragon was disappointed with your non-exstant attack. You died."
userHealth = 0
} else if (userInput === 5) {
userDmg.innerHTML = "Your attacks were weak. You deal 1 damage."
dragDmg.innerHTML = "The dragon is able to land a critical hit and deals 20 damage."
dragHealth = dragHealth - 1
userHealth = userHealth - 20
} else if (userInput === 1 || userInput === 2 || userInput === 3 || userInput === 4) {
var userAttackDmg = Number(userInput) * (Math.floor(Math.random() * 20))
userDmg.innerHTML = "You dealt " + userAttackDmg + " damage!"
dragHealth = dragHealth - userAttackDmg
var dragAttackDmg = Math.floor(Math.random() * 20) + 10
dragDmg.innerHTML = "The dragon dealt " + dragAttackDmg + " damage!"
userHealth = userHealth - dragAttackDmg
} else {
userOut.innerHTML = "You confused yourself. You died."
userHealth = 0
}
userHthTotal.innerHTML = "Your health: " + userHealth
dragHthTotal.innerHTML = "Dragon's health: " + dragHealth
if (userHealth <= 0) {
userOut.innerHTML = "Tough battle, but the dragon kills you."
dragOut.innerHTML = "The dragon goes on to pay a visit to your village."
}
if (dragHealth <= 0) {
userOut.innerHTML = "You are victorious!"
dragOut.innerHTML = "However, watch out, the spirit of the dragon will probably haunt you for the rest of your life."
}
}https://stackoverflow.com/questions/66514157
复制相似问题