我正在创建一个游戏,用户可以在这里取得一些进展以进入新的水平。这是代码:
<?php
$progress=150; // example
$percentage=0;
$level=0;
if ($progress<300) { $level = 1; $percentage=; }
if ($progress<600) { $level = 2 $percentage=; }
if ($progress<900) { $level = 3 $percentage=; }
echo $percentage;
?>
因此,如果进度为150分,那么该用户的等级为1,他移动到新等级2的百分比为50%(已完成)。
例如,如果进度为750分,则进度应显示50%(已完成)以移至新的3级。
我在这里选择简单值150和750,但我不知道如何计算进度是610,625等...
如果您有任何问题,请与我们联系。
感谢。
答案 0 :(得分:0)
这很有效。可以重构:
if ($progress < 300) { $level = 1; $percentage = (($progress/300) * 100); }
elseif ($progress < 600) { $level = 2; $percentage = (($progress/600) * 100); }
elseif ($progress < 900) { $level = 3; $percentage = (($progress/900) * 100); }
答案 1 :(得分:0)
您应该将进度存储为数组,并循环遍历它们:
$progress = 750;
$levels = array(0, 300, 600, 900);
foreach ($levels as $key => $threshold) {
if ($progress >= $threshold) {
$level = $key;
if (isset($levels[$key + 1])) {
$percentage = 100 * ($progress - $threshold) /
($levels[$key + 1] - $threshold);
} else {
// user has passed the final level
$percentage = null;
}
}
}
答案 2 :(得分:0)
通用答案:
$limits = array( 0, 300, 600, 900);
$found = false;
for ($level = 1; ($level < count($limits)) && !$found; $level++) {
if ($progress < $limits[$level]) {
$found = true;
break;
}
}
echo "Level: $level\n";
if ($found) {
// percentage to next level
$lower = $limits[$level-1];
$percentage = ($progress-$lower)/($limits[$level]-$lower);
echo $percentage;
} else {
echo "You're a fewking superhero!!";
}
答案 3 :(得分:0)
使用以下功能。
<?php
$progress=700;
$_multiples_of=300;
echo "Progress to next level is ".getProgressPercentage($progress,$_multiples_of);
function getProgressPercentage($progress,$multiples_of){
$level=floor($progress/$multiples_of);//Get the level of the user based on the interval of 300
$nextLevelValue=$multiples_of * ($level +1);
$percentageToNextLevel=intval(($progress/$nextLevelValue) *100);
return $percentageToNextLevel;
}
?>
答案 4 :(得分:0)
尝试此动态级别系统,您不需要设置xp需要升级静态但动态。您可以定义必须有多少级别以及每个级别之间的xp增长因子。
<?php
$userXp = 1344; // change this to test
$maxLevel = 12;
$grow = 0.88;
$maxFisrtLv = 530;
$ranks = array();
for($a = 0; $a <= $maxLevel; $a++){
if($a == 0){
$xp = 0;
}
elseif($a == 1){
$xp = $maxFisrtLv;
}
else{
$prev = $a - 1;
$xp = $maxFisrtLv + ($grow * $ranks[$prev]);
}
if($userXp >= $xp){
$userlevel = $a;
}
$ranks[$a] = $xp;
}
$xpBlank = $userXp - $ranks[$userlevel];
$betweenXp = $ranks[$userlevel + 1] - $ranks[$userlevel];
$progres = number_format($xpBlank / $betweenXp * 100, 2, '.', '');
$progresall = number_format($userXp / end($ranks) * 100, 2, '.', '');
$rank = print_r($ranks, true);
echo "
userlevel: $userlevel<br>
progress overall: $progresall%<br>
progress: $progres%<br>
xp: $userXp<br>
<br>
<br>
<pre>
$rank
</pre>
";
?>