结果是这样的:
你死了:-11马力敌人赢了!
Enemy hp是:-8你赢了!你获得50 exp!
两者都有100马力,正如你在第2行所看到的那样,当敌人击中8点伤害并且剩下86马力时,有一个“循环”丢失。
当其中一个死亡时,如何停止循环,以便两个同时死亡?因此,如果一个人死了,那么循环就会停止并确定胜利者。
有没有更好的方法来确定从随机开始?
<?php
$p_hp = 100;
$p_atk = 20;
$p_defence = 5;
$m_hp = 100;
$m_atk = 20;
$m_defence = 5;
if ($p_hp > 0) {
$turn = rand(0,1);
$hitnr = 0;
while (($p_hp >= 0) && ($m_hp >= 0)) {
$turn--;
$hitnr++;
$enemydef = $p_atk - $m_defence / 2;
$my_damage = rand(1, $enemydef);
$playerdef = $m_atk - $p_defence / 2;
$enemy_damage = rand(1, $playerdef);
$p_hp = $p_hp - $enemy_damage;
$m_hp = $m_hp - $my_damage;
// Player turn
if ($turn % 2 == 0) {
echo "<b>".$hitnr.".</b> Enemy hit you for <font color='red'><b>".$enemy_damage . "</b></font> damage you have <b>".$p_hp." hp</b> left<br>";
// Monster turn
} else {
echo "<b>".$hitnr.".</b> You hit enemy for <font color='green'><b>".$my_damage . "</b></font> damage enemy has <b>".$m_hp." hp</b> left<br>";
}
if ($p_hp <= 0) {
echo "<br>You died: ".$p_hp." hp<br><font color='red'><b>Enemy wins!</b></font>";
}
if ($m_hp <= 0) {
echo "<br>Enemy hp is: ".$m_hp."<br><font color='green'><b>You win! You gain 50 exp!</b></font>";
}
}
} else {
echo "You are dead. <a href='hospital.php'>Go to the hospital!</a>";
}
?>
答案 0 :(得分:0)
除非怪物(或玩家)能够反击,否则只有目标公司的HP会减少:
$p_hp = 100;
$p_atk = 20;
$p_defence = 5;
$m_hp = 100;
$m_atk = 20;
$m_defence = 5;
if ($p_hp >= 0) {
$turn = rand(0,1);
$hitnr = 0;
while (($p_hp >= 0) && ($m_hp >= 0)) {
$turn--;
$hitnr++;
$enemydef = $p_atk - $m_defence / 2;
$my_damage = rand(1, $enemydef);
$playerdef = $m_atk - $p_defence / 2;
$enemy_damage = rand(1, $playerdef);
// Monster turn
if ($turn % 2 == 0) {
$p_hp = $p_hp - $enemy_damage;
echo "<b>".$hitnr.".</b> Enemy hit you for <font color='red'><b>".$enemy_damage . "</b></font> damage you have <b>".$p_hp." hp</b> left<br>";
// Player turn
} else {
$m_hp = $m_hp - $my_damage;
echo "<b>".$hitnr.".</b> You hit enemy for <font color='green'><b>".$my_damage . "</b></font> damage enemy has <b>".$m_hp." hp</b> left<br>";
}
if ($p_hp <= 0) {
echo "<br>You died: ".$p_hp." hp<br><font color='red'><b>Enemy wins!</b></font>";
}
if ($m_hp <= 0) {
echo "<br>Enemy hp is: ".$m_hp."<br><font color='green'><b>You win! You gain 50 exp!</b></font>";
}
}
} else {
echo "You are dead. <a href='hospital.php'>Go to the hospital!</a>";
}
?>