php数学循环查询

php数学循环查询,php,math,loops,Php,Math,Loops,这很难解释我想要什么,所以我会尽力的 我有一个游戏,游戏中的玩家有一个健康计数器(hp),还有保镖。每个保镖都有100点生命,一旦所有a玩家的保镖都消失了,他们的生命就会受到伤害 这些是我的价值观: $dmg // the damage $bgs // the amount of bodyguards the player has $bg_hp // the bodyguards health. Starts at 100, drains with $dmg fired at $hp // th

这很难解释我想要什么,所以我会尽力的

我有一个游戏,游戏中的玩家有一个健康计数器(hp),还有保镖。每个保镖都有100点生命,一旦所有a玩家的保镖都消失了,他们的生命就会受到伤害

这些是我的价值观:

$dmg // the damage
$bgs // the amount of bodyguards the player has
$bg_hp // the bodyguards health. Starts at 100, drains with $dmg fired at
$hp // the players health
当他们被射杀时,它需要检查他们是否有$BG。这是一个基本的例子

if ($bgs > 0) {
  $bgs_hp = $bgs_hp - $dmg;
     if ($bg_hp <= 0) { $bg = $bg - 1; $bg_hp = 100; }
  } else {
  $hp = $hp - $dmg;
  }
if($bgs>0){
$bgs\U hp=$bgs\U hp-$dmg;

如果($bg_hp,我只需保留一个包含每个保镖的hp的阵列,如下所示:

$bgs = array(100); // one bg with 100 hp

如果你想知道有多少保镖,一个简单的
计数($bgs)
告诉你

这将使您在将来更容易扩展游戏,也使您更容易解决这些类型的计算。您现在可以编写:

$hp = 100;
$bgs = array(100, 100);
$damage = 101; // for example

// As long as there's still damage to take and bgs to take it:
while($damage && !empty($bgs)) {
    $soak = min($bgs[0], $damage); // not more than the first bg can take
    $bgs[0] -= $soak; // remove hps from the first bg
    $damage -= $soak; // deduct from the amount of damage to tage
    if ($bgs[0] == 0) {
        // bodyguard dead, remove him from the array
        array_shift($bgs);
    }
}

// If there's any damage left over, it goes to hp
$hp -= $damage;

更新:以下是适用于当前方案的代码。它并不简单,而且对您可以执行的操作限制更大:

$max_bg_health = 100;
$hp = 100;
$bgs = 2;
$bg_hp = $max_bg_health; // so that makes 2 "full health" bgs
$damage = 101; // for example
if($bgs) {
    $bg_capacity = ($bgs - 1) * $max_bg_health + $bg_hp;
    $soak = min($bg_capacity, $damage);
    $bg_capacity -= $soak;
    $damage -= $soak;

    // update bodyguard status; this is extra tricky because $bgs is not
    // "the amount of full hp bgs" (which would make it a lot simpler)
    // but "the amount of bgs including the one which may not have full hp"
    $bgs = $bg_capacity ? intval($bg_capacity - 1 / $max_bg_health) + 1 : 0;
    $bg_hp = $bg_capacity % $max_bg_health;
}

// If there's any damage left over, it goes to hp
$hp -= $damage;

我只需要保留一个包含每个保镖hps的阵列,如下所示:

$bgs = array(100); // one bg with 100 hp

如果你想知道有多少保镖,一个简单的
计数($bgs)
告诉你

这将使您在将来更容易扩展游戏,也使您更容易解决这些类型的计算。您现在可以编写:

$hp = 100;
$bgs = array(100, 100);
$damage = 101; // for example

// As long as there's still damage to take and bgs to take it:
while($damage && !empty($bgs)) {
    $soak = min($bgs[0], $damage); // not more than the first bg can take
    $bgs[0] -= $soak; // remove hps from the first bg
    $damage -= $soak; // deduct from the amount of damage to tage
    if ($bgs[0] == 0) {
        // bodyguard dead, remove him from the array
        array_shift($bgs);
    }
}

// If there's any damage left over, it goes to hp
$hp -= $damage;

更新:以下是适用于当前方案的代码。它并不简单,而且对您可以执行的操作限制更大:

$max_bg_health = 100;
$hp = 100;
$bgs = 2;
$bg_hp = $max_bg_health; // so that makes 2 "full health" bgs
$damage = 101; // for example
if($bgs) {
    $bg_capacity = ($bgs - 1) * $max_bg_health + $bg_hp;
    $soak = min($bg_capacity, $damage);
    $bg_capacity -= $soak;
    $damage -= $soak;

    // update bodyguard status; this is extra tricky because $bgs is not
    // "the amount of full hp bgs" (which would make it a lot simpler)
    // but "the amount of bgs including the one which may not have full hp"
    $bgs = $bg_capacity ? intval($bg_capacity - 1 / $max_bg_health) + 1 : 0;
    $bg_hp = $bg_capacity % $max_bg_health;
}

// If there's any damage left over, it goes to hp
$hp -= $damage;

一种可能是,只存储总健康点(即您的健康点和保镖的健康点)。然后您可以在每次需要时计算您的健康点和保镖的数量(因此您不需要保存它们)

然后,当你受到一些伤害时,只需从
$totalhp
中减去:

$totalhp = $totalhp - $dmg;

一种可能是,只存储总健康点(即您的健康点和保镖的健康点)。然后您可以在每次需要时计算您的健康点和保镖的数量(因此您不需要保存它们)

然后,当你受到一些伤害时,只需从
$totalhp
中减去:

$totalhp = $totalhp - $dmg;

这将产生

你还有100个生命值和1个保镖,生命值为77


这将产生

你还有100个生命值和1个保镖,生命值为77


如果你有两个保镖怎么办?他们能在你的健康下降之前拿走200 dmg吗?$bg_hp是包含所有保镖的健康还是只包含目前正在接受子弹的保镖的健康?只包含承受伤害的保镖。其余的保镖都是100 dmg,在“最上面”的保镖死亡之前不会被触摸。所以如果你有两个保镖,拿走199 d伤害,它杀死1名保镖,但另一名保镖没有受伤,但如果你受到201点伤害,它会杀死2名保镖,而玩家得到1点伤害?不,如果你有2名保镖,受到199点伤害,它会杀死第一名(100),然后再减去99,剩下1点生命。@Juhana,那是201而不是199…如果你有两个保镖怎么办?他们能在你的健康下降之前拿走200 dmg吗?$bg_生命值是包含所有保镖的健康值还是只包含目前正在接受子弹的保镖的健康值?只包含受到伤害的保镖。其余的是100和100在“最上面的”一个死亡之前不会被触摸。因此,如果你有两个保镖并受到199点伤害,它会杀死一个保镖,但另一个不会受伤,但是如果你受到201点伤害,它会杀死两个保镖,玩家会受到1点伤害?不,如果你有两个保镖并受到199点伤害,它会杀死第一个(100),然后下一个减去99,剩下1点生命。@Juhana那是201而不是199…谢谢,我会努力弄明白的(我还在学习)。如果我使用这个,我需要根据我以前使用的$bgs、$bgs_hp和$hp的方法来构建阵列,因为这是目前正在使用的。从未真正使用过阵列,所以我必须研究它们是如何工作的。@user961882:我更新了答案,以涵盖您当前的情况,但您应该真正使用阵列。谢谢,我会尝试理解的(我还在学习)。如果我使用这个,我需要根据我以前使用的$bgs、$bgs_hp和$hp的方法来构建阵列,因为这是目前正在使用的。从未真正使用过阵列,所以我必须研究它们是如何工作的。@user961882:我更新了答案以涵盖您当前的情况,但您真的应该使用阵列。如果您将伤害设置为100,则程序将继续m会告诉你剩下两名保镖(即使你一开始只有两名保镖)。如果你将伤害设置为100,程序会告诉你剩下两名保镖(即使你一开始只有两名保镖)。