这将是一个很奇怪的问题,但请耐心等待。我的PHP阵列问题
我正在编写一个基于浏览器的游戏,每个玩家都有一定数量的卫兵,每人有100个卫生。每次射击时,卫兵失去健康。如果所有的守卫都死了,玩家就会改变健康。
射门伤害总是重叠,所以如果球员有3名后卫,而顶级后卫有60名生命,则100次射门会杀死后卫3并留下60后卫2。
我使用一个php数组来排序这个,它起作用,除非它涉及到玩家的健康。它不能正确计算,例如所有的球员守卫都已经死亡,并且球员剩下60个生命值。他被射中了100,而不是死于健康循环,所以他有一些其他数字健康而不是-40。
$p_bg = array(); // players guard count
$p_bg[0] = $rs[bgs_hp2]; // only the top guard hp is saved (bgs_hp2)
$p_hp = $rs[hp2]; // players health
$dmg = 80 // shot damage
$x = 0;
while($x < $rs[BGs2]) { $p_bg[$x] = 100; $x++; }
// As long as there's still damage to take and bgs to take it:
while($dmg && !empty($p_bg)) {
$soak = min($p_bg[0], $dmg); // not more than the first bg can take
$p_bg[0] -= $soak; // remove hps from the first bg
$dmg -= $soak; // deduct from the amount of damage to tage
if ($p_bg[0] == 0) {
// bodyguard dead, remove him from the array
array_shift($p_bg);
}
}
// If there's any damage left over, it goes to hp
$p_hp = $p_hp - $dmg;
我现在不在PHP框中,所以不能为你做,但这是'xdebug'的错误。安装它来遍历你的代码,它会告诉你变量在哪里被改变。 – 2015-03-31 20:39:57