gpt4 book ai didi

javascript - 为什么我的敌人死后还要攻击?(Javascript)

转载 作者:行者123 更新时间:2023-11-30 21:19:06 25 4
gpt4 key购买 nike

     function battle(){
//User turn
alert("It's your turn!");
var user_move = prompt("Type 1 to use fireball, 2 to use lightning strike, or 3 to do earthquake");
if(user_move == 1){
var r = Math.round(Math.random()*(dmg.length-1));
alert("You did "+dmg[r]+" damage to the enemy!");
enemy_HP = enemy_HP - dmg[r];
}
else if(user_move == 2){
var r = Math.round(Math.random()*(dmg.length-1));
alert("You did "+dmg[r]+" damage to the enemy!");
enemy_HP = enemy_HP - dmg[r];
}
else if(user_move == 3){
var r = Math.round(Math.random()*(dmg.length-1));
alert("You did "+dmg[r]+" damage to the enemy!");
enemy_HP = enemy_HP - dmg[r];
}
alert("Your enemy has "+enemy_HP+"HP");
//Enemy turn
alert("Now its your enemys turn!");
var a = Math.round(Math.random(enemy_attack.length-1));
var r = Math.round(Math.random()*(dmg.length-1));
alert("You enemy used "+enemy_attack[a]+"\nand did "+enemy_dmg[r]+" damage to you!");
HP = HP - enemy_dmg[r];
alert("You now have "+HP+"HP!");
//If still living keep battling
if(HP > 0 && enemy_HP > 0){
battle();
}
else if(HP <= 0){
alert("You lost D:");
}
else if(enemy_HP <= 0){
alert("You win :D!");
}
}
battle();

我正在尝试制作一个有趣的小型 js 战斗游戏,但有时当你杀死敌人时,它会有最后一次攻击,即使它已经死了也可以杀死你。我该如何解决?

最佳答案

好吧,根据您的代码判断,您首先对敌人造成伤害,然后才检查他是否有足够的生命。在检查敌人是否死亡的 if 语句中包括来自敌人的伤害处理。

关于javascript - 为什么我的敌人死后还要攻击?(Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45364679/

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