gpt4 book ai didi

javascript - 如何在初始功能之前将一个变量重置为原始值?

转载 作者:行者123 更新时间:2023-12-03 08:26:09 25 4
gpt4 key购买 nike

我正在编写一个“选择你自己的冒险”脚本,其中玩家与妖精战斗,直到他们的生命值(var hp)之一达到 0 或更少。如果一轮攻击后(玩家攻击一次,妖精攻击一次)双方的生命值都没有达到 0 或更少,则脚本调用一个函数来重复原始一轮攻击(但这次妖精和玩家都减少了hp 和 def 值)。当然,该函数会继续循环,直到玩家或妖精的生命值达到 0 或更低。我的问题出现在我如何计算玩家或妖精每轮受到的伤害上。两者都有一个 atk 变量,它从对手的 def 变量中减去,如果 def 变量小于 0,那么 def 变量的绝对值就是从妖精或玩家的 HP 中减去多少(取决于谁攻击了谁) )。当函数循环时,没有任何变量重置。这对除 def 之外的每个变量都有好处,我需要每轮重置它。

我的问题是:在玩家在装甲开关中做出选择后(如下),如何将每轮的 def 变量重置为正确的值?我需要它发生在 FightLoop() 函数之前,但不是在 vararmor 提示之前。

这是代码:

   function getRandomAtk(min,max) {
return Math.random() *(max-min) + min;
}

//player stats
var player= {
atk: 1,
def: 1,
hp: 10,
mp: 0,
block: 1,
ltn: 0,
fire: 0,
magic: 0,
ltnDef: 0,
fireDef:0,
magDef:0
}
//goblin stats
var goblin= {
atk: Math.floor(getRandomAtk(3,6)),
def: 1,
hp: 5,
mp: 0,
block: 1,
magDef: 0,
ltnDef: 0,
fireDef:0
}
//variable increments
f=0;
g=0;
h=0;
i=0;
j=0;
ff=0;
gg=0;



while(i<=0){
var begin= prompt("Pick your weapon: sword, staff, or shield").toLowerCase();
switch(begin) {
case "sword":
console.log("You chose the sword. Your Attack is increased by 5. Block is increased by 2");
player.atk += 5;
player.block += 2;
i++;
break;

case "staff":
console.log("You chose the staff. Your Magic is increased by 5. Your MP is increased by 10. You obtained Spell Attack!");
player.magic += 5;
player.mp+= 10;
i++;
//Spell Attack acquired
break;


case "shield":
console.log("You chose the shield. Your Attack is increased by 2. Your Block is increased by 5");
player.atk += 2;
player.block += 5;
i++;
break;

case "stats":
console.log("Your Stats");
console.log("Attack=" + " " + player.atk + " " + "Defense=" + "1"+ " "+ "HP=" + " " + player.hp+ " " + "MP=" + " " + player.mp+ " " +"Block=" + " " + player.block + " " +"Lightning Atk=" + " " + player.ltn + " " + "Fire Atk="+ " " + player.fire + " "+ "Magic Atk=" + " " + player.magic + " " + "Lightning Def="+ " " + player.ltnDef + " "+ "Fire Def=" + " " + player.fireDef + " " + "Magic Def=" + " " + player.magDef);
continue;

default:
console.log("You must make a choice");
continue;




}
}

if(begin == "sword") {
while (f<=0) {
var sacrifice= prompt("Which weapon do you choose to sacrifice?").toLowerCase();
switch(sacrifice) {
case "sword":
console.log("You cannot do that.");
break;



case "staff":
console.log("You give up the staff. Your Block is increased by 2");
player.block += 2;
f++;
break;

case "shield":
console.log("You give up the shield. Your Magic Attack is increased by 2. Your MP is increased by 2");
player.magic += 2;
player.mp+= 3;
f++;
break;

case "stats":
console.log("Your Stats");
console.log("Attack=" + " " + player.atk + " " + "Defense=" + " " + player.def + " "+ "HP=" + " " + player.hp+ " " + "MP=" + " " + player.mp+ " " +"Block=" + " " + player.block + " " +"Lightning Atk=" + " " + player.ltn + " " + "Fire Atk="+ " " + player.fire + " "+ "Magic Atk=" + " " + player.magic + " " + "Lightning Def="+ " " + player.ltnDef + " "+ "Fire Def=" + " " + player.fireDef + " " + "Magic Def=" + " " + player.magDef);
continue;

default:
console.log("You must make a choice");
continue;

}
}
}

else if(begin=="shield") {
while(g<=0) {
var sacrifice2= prompt("Which weapon do you choose to sacrifice?").toLowerCase();
switch(sacrifice2) {
case "sword":
console.log("You give up the sword. Your Magic Attack is increased by 2. Your MP is increased by 2");
player.magic +=2;
player.mp+= 3;
g++;
break;

case "staff":
console.log("You give up the staff. Your Attack is increased by 2. Your Block is increased by 1");
player.atk += 2;
player.block +=1;
g++;
break;

case "shield":
console.log("You cannot do that");
continue;

case "stats":
console.log("Your Stats");
console.log("Attack=" + " " + player.atk + " " + "Defense="+ " " + "1" +" "+ "HP=" + " " + player.hp+ " " + "MP=" + " " + player.mp+ " " +"Block=" + " " + player.block + " " +"Lightning Atk=" + " " + player.ltn + " " + "Fire Atk="+ " " + player.fire + " "+ "Magic Atk=" + " " + player.magic + " " + "Lightning Def="+ " " + player.ltnDef + " "+ "Fire Def=" + " " + player.fireDef + " " + "Magic Def=" + " " + player.magDef);
continue;

default:
console.log("You must make a choice");
continue;

}
}
}

else if(begin == "staff") {
while(h<=0) {
var sacrifice3= prompt("Which weapon do you choose to sacrifice?").toLowerCase();
switch(sacrifice3) {
case "sword":
console.log("You give up the sword. Your Block is increased by 2");
player.block +=2;
h++;
break;

case "staff":
console.log("You cannot do that.");
continue;


case "shield":
console.log("You give up the shield. Your Attack is increased by 2. Your Block is increased by 1");
player.atk +=2;
player.block +=1;
h++;
break;

case "stats":
console.log("Your Stats");
console.log("Attack=" + " " + player.atk + " " + "Defense=" + " " + "1" + " "+ "HP=" + " " + player.hp+ " " + "MP=" + " " + player.mp+ " " +"Block=" + " " + player.block + " " +"Lightning Atk=" + " " + player.ltn + " " + "Fire Atk="+ " " + player.fire + " "+ "Magic Atk=" + " " + player.magic + " " + "Lightning Def="+ " " + player.ltnDef + " "+ "Fire Def=" + " " + player.fireDef + " " + "Magic Def=" + " " + player.magDef);
continue;

default:
console.log("You must make a choice");
continue;

}
}
}

while(j<=0){

var armor= prompt("Which armor set do you choose? Magic Robe('robe') or Leather Plate? ('plate')").toLowerCase();
switch(armor) {
case "robe":
console.log("You choose the Magic Robe. Your Magic Defense goes up by 3.");
player.magDef+=3;
j++;
break;

case "plate":
console.log("You choose the Leather Plate. Your Defense goes up by 3.");
player.def+=3;
j++;
break;

case "stats":
console.log("Your stats");
console.log("Attack=" + " " + player.atk + " " + "Defense=" + " " + player.def + " "+ "HP=" + " " + player.hp+ " " + "MP=" + " " + player.mp+ " " +"Block=" + " " + player.block + " " +"Lightning Atk=" + " " + player.ltn + " " + "Fire Atk="+ " " + player.fire + " "+ "Magic Atk=" + " " + player.magic + " " + "Lightning Def="+ " " + player.ltnDef + " "+ "Fire Def=" + " " + player.fireDef + " " + "Magic Def=" + " " + player.magDef);
continue;

default:
console.log("You must make a choice.");
continue;
}
}





var fight= alert("A goblin attacks you!");

fightLoop();

function fightLoop() {
while(ff<=0) {
var fight2= prompt("What do you do? 'Attack' or 'Block'? or 'Spell Attack(spell)'?").toLowerCase();
switch(fight2) {
case "attack":
console.log("You Attack the goblin!");
goblin.def-=player.atk;

if(goblin.def<0) {
goblin.hp-=Math.abs(goblin.def);
console.log("You dealt"+ " " + Math.abs(goblin.def) + " "+ "damage!");
if(goblin.hp<=0) {
console.log("The goblin is slain!");
ff++;
gg++;
}

else if(goblin.hp>=0) {
ff++;
}
}

else {
console.log("You did 0 damage!");
ff++;
}
break;

case "spell":
if(begin=="staff"){
console.log("You cast a Spell Attack!");
goblin.magDef-=player.magic;

if(goblin.magDef<0) {
goblin.hp-= Math.abs(goblin.magDef);
console.log("You dealt" + " " + Math.abs(goblin.magDef) + " " + "damage!");
if(goblin.hp<=0) {
console.log("The goblin is slain!");
ff++;
gg++;
}

else {
ff++;
}
}

else {
console.log("You did 0 damage!");
f++;
}
}

else {
console.log("You cannot use magic.")
}
continue;

case "block":
console.log("You choose to Block the goblin's next Attack!");
ff++;
break;

case "stats":
console.log("Your Stats");
console.log("Attack=" + " " + player.atk + " " + "Defense=" + " " + player.def + " "+ "HP=" + " " + player.hp+ " " + "MP=" + " " + player.mp+ " " +"Block=" + " " + player.block + " " +"Lightning Atk=" + " " + player.ltn + " " + "Fire Atk="+ " " + player.fire + " "+ "Magic Atk=" + " " + player.magic + " " + "Lightning Def="+ " " + player.ltnDef + " "+ "Fire Def=" + " " + player.fireDef + " " + "Magic Def=" + " " + player.magDef);
continue;

case "goblin stats":
console.log("Goblin Stats");
console.log("Attack=" + " " + goblin.atk+ " " + "Defense="+ " "+ goblin.def+ " "+ "HP="+ " "+ goblin.hp+ " "+ "MP="+ " "+ goblin.mp+ " " + "Block"+ " "+ goblin.block+ " "+ "Magic Defense"+ " "+ goblin.magDef)
continue;

default:
console.log("You must make a choice.");
continue;
}
}

if (gg>0) {
console.log("You won the game! Congratulations!")
}

else {
var retaliate= alert("The Goblin Attacks you!");

if (fight2== "block") {
player.def+=player.block;
player.def-= goblin.atk;
if(player.def<0) {
console.log("You blocked the Attack!");
player.hp-=Math.abs(player.def);
console.log("The Goblin did"+ " "+ Math.abs(player.def)+ " "+ "Damage!");

if(player.hp<=0) {
console.log("The Goblin defeated you! You died.");
console.log("Game Over.");


}


}


else {
console.log("The goblin did 0 damage!");
}
}

else {
player.def-=goblin.atk;
if(player.def<0) {
player.hp-=Math.abs(player.def);
console.log("The Goblin did"+ " "+ Math.abs(player.def)+ " "+ "Damage!");
if(player.hp<=0) {
console.log("The Goblin defeated you! You died");
console.log("Game Over.");

}


}
else {
console.log("The Goblin did 0 damage!");
}
}
}
}

if (goblin.hp>=0) {
ff-=1;
fightLoop();

}

最佳答案

向玩家“类别”添加变量以表示每个统计数据的最大值:

var player= {
def: 1,
max_def: 10,
hp: 10,
max_hp: 10,
...
}

如果您需要在遭遇中重置某些内容,请将当前值设置回最大值:

player.def = player.max_def;

关于javascript - 如何在初始功能之前将一个变量重置为原始值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33541370/

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