php php游戏,根据exp计算关卡的公式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6954874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:40:45  来源:igfitidea点击:

php game, formula to calculate a level based on exp

phpgame-development

提问by user880356

Im making a browser based PHP game and in my database for the players it has a record of that players total EXP or experience.

我正在制作一个基于浏览器的 PHP 游戏,并且在我的玩家数据库中,它记录了该玩家的总 EXP 或经验。

What i need is a formula to translate that exp into a level or rank, out of 100.

我需要的是一个公式,可以将 exp 转换为 100 分中的级别或等级。

So they start off at level 1, and when they hit say, 50 exp, go to level 2, then when they hit maybe 125/150, level 2.

所以他们从 1 级开始,当他们达到 50 经验时,进入 2 级,然后当他们达到 125/150 时,达到 2 级。

Basically a formula that steadily makes each level longer (more exp)

基本上是一个稳定地使每个级别更长的公式(更多经验)

Can anyone help? I'm not very good at maths :P

任何人都可以帮忙吗?我数学不太好:P

回答by Vilx-

Many formulas may suit your needs, depending on how fast you want the required exp to go up.

许多公式可能适合您的需求,具体取决于您希望所需 exp 上升的速度。

In fact, you really should make this configurable (or at least easily changed in one central location), so that you can balance the game later. In most games these (and other) formulas are determined only after playtestingand trying out several options.

事实上,您确实应该将其设置为可配置(或者至少可以在一个中心位置轻松更改),以便您以后可以平衡游戏。在大多数游戏中,这些(和其他)公式只有在playtesting尝试了几个选项之后才能确定。

Here's one formula: First level-up happens at 50 exp; second at 150exp; third at 300 exp; fourth at 500 exp; etc. In other words, first you have to gather 50 exp, then 100 exp, then 150exp, etc. It's an Arithmetic Progression.

这是一个公式:第一次升级发生在 50 exp;第二个是 150exp;第三名 300 exp;第四名 500 exp;等等。换句话说,首先你必须收集 50 exp,然后是 100 exp,然后是 150exp,等等。这是一个算术级数。

For levelup Xthen you need 25*X*(1+X)exp.

为了升级,X那么你需要25*X*(1+X)exp。

Added:To get it the other way round you just use basic math. Like this:

补充:反过来,你只需使用基本的数学。像这样:

y=25*X*(1+X)
0=25*X*X+25*X-y

That's a standard Quadratic equation, and you can solve for X with:

这是一个标准的Quadratic equation,你可以用以下方法求解 X:

X = (-25±sqrt(625+100y))/50

Now, since we want both X and Y to be greater than 0, we can drop one of the answers and are left with:

现在,由于我们希望 X 和 Y 都大于 0,我们可以去掉其中一个答案,剩下:

X = (sqrt(625+100y)-25)/50

So, for example, if we have 300 exp, we see that:

因此,例如,如果我们有 300 exp,我们会看到:

(sqrt(625+100*300)-25)/50 = (sqrt(30625)-25)/50 = (175-25)/50 = 150/50 = 3

Now, this is the 3rd levelup, so that means level 4.

现在,这是第3次的LevelUp,所以这意味着4级。

回答by Mike

If you wanted the following:

如果您想要以下内容:

  • Level 1 @ 0 points
  • Level 2 @ 50 points
  • Level 3 @ 150 points
  • Level 4 @ 300 points
  • Level 5 @ 500 points etc.
  • 等级 1 @ 0 分
  • 等级 2 @ 50 分
  • 等级 3 @ 150 分
  • 4 级 @ 300 点
  • 等级 5 @ 500 点等

An equation relating experience (X) with level (L) is:

将经验 (X) 与级别 (L) 联系起来的等式是:

X = 25 * L * L - 25 * L

To calculate the level for a given experience use the quadratic equation to get:

要计算给定体验的级别,请使用二次方程得到:

L = (25 + sqrt(25 * 25 - 4 * 25 * (-X) ))/ (2 * 25)

This simplifies to:

这简化为:

L = (25 + sqrt(625 + 100 * X)) / 50

Then round down using the floor function to get your final formula:

然后使用 floor 函数向下舍入以获得最终公式:

L = floor(25 + sqrt(625 + 100 * X)) / 50

Where L is the level, and X is the experience points

其中L是等级,X是经验值

回答by Sleeperson

It really depends on how you want the exp to scale for each level. Let's say

这实际上取决于您希望 exp 如何为每个级别进行缩放。让我们说

LvL1 : 50 Xp
Lvl2: LvL1*2=100Xp
LvL3: LvL2*2=200Xp
Lvl4: LvL3*2=400Xp

This means you have a geometric progression The Xp required to complete level n would be

这意味着你有一个几何级数 完成第 n 级所需的 Xp 将是

`XPn=base*Q^(n-1)`

In my example base is the inital 50 xp and Q is 2 (ratio).

在我的示例中,初始值为 50 xp,Q 为 2(比率)。

Provided a player starts at lvl1 with no xp:

如果玩家从 lvl1 开始且没有 xp:

when he dings lvl2 he would have 50 total Xp
at  lvl3 150xp
at  lvl4 350xp

and so forth The total xp a player has when he gets a new level up would be:

以此类推 玩家升到新级别时拥有的总经验值为:

 base*(Q^n-1)/(Q-1)

In your case you already know how much xp the player has. For a ratio of 2 the formula gets simpler:

在您的情况下,您已经知道玩家拥有多少经验。对于 2 的比率,公式变得更简单:

base * (2^n-1)=total xp at level n

to find out the level for a given xp amount all you need to do is apply a simple formula

要找出给定 xp 数量的级别,您需要做的就是应用一个简单的公式

$playerLevel=floor(log($playerXp/50+1,2));

But with a geometric progression it will get harder and harder and harder for players to level.

但是随着几何级数的增加,玩家升级会变得越来越难。

To display the XP required for next level you can just calculate total XP for next level.

要显示下一级别所需的 XP,您只需计算下一级别的总 XP。

$totalXpNextLevel=50*(pow(2,$playerLevel+1)-1);
$reqXp=$totalXpNextLevel - $playerXp;

Check start of the post: to get from lvl1 -> lvl2 you need 50 xp lvl2 ->lvl3 100xp

检查帖子的开头:要从 lvl1 -> lvl2 你需要 50 xp lvl2 ->lvl3 100xp

to get from lvl x to lvl(x+1) you would need

要从 lvl x 到 lvl(x+1) 你需要

$totalXprequired=50*pow(2,$playerLevel-1);

回答by DirkZz

Google gave me this:

谷歌给了我这个:

function experience($L) {
 $a=0;
  for($x=1; $x<$L; $x++) {
    $a += floor($x+300*pow(2, ($x/7)));
  }
 return floor($a/4);
}

for($L=1;$L<100;$L++) {
 echo 'Level '.$L.': '.experience($L).'<br />';
}

It is supposed the be the formula that RuneScape uses, you might me able to modify it to your needs. Example output:

应该是 RuneScape 使用的公式,您可以根据需要修改它。示例输出:

Level 1: 0
Level 2: 55
Level 3: 116
Level 4: 184
Level 5: 259
Level 6: 343
Level 7: 435
Level 8: 536
Level 9: 649
Level 10: 773

回答by NappingRabbit

Here is a fast solution I used for a similar problem. You will likely wanna change the math of course, but it will give you the level from a summed xp.

这是我用于类似问题的快速解决方案。你当然可能想要改变数学,但它会给你一个总经验值的水平。

    $n = -1;
    $L = 0;

    while($n < $xp){
        $n += pow(($L+1),3)+30*pow(($L+1),2)+30*($L+1)-50;
        $L++;
    }

    echo("Current XP: " .$xp);
    echo("Current Level: ".$L);
    echo("Next Level: " .$n);

回答by Old Man Walter

The original was based upon a base of 50, thus the 25 scattered across the equation.

原来的基数是 50,因此 25 分散在等式中。

This is the answer as a real equation. Just supply your multiplier (base) and your in business.

这是一个真正的方程的答案。只需提供您的乘数(基数)和您的业务。

$_level = floor( floor( ($_multipliter/2)
                      + sqrt( ($_multipliter^2) + ( ($_multipliter*2) * $_score) ) 
                      ) 
                      / $_multipliter
               ) ;

回答by John

I take it what you're looking for is the amount of experience to decide what level they are on? Such as: Level 1: 50exp Level 2: 100exp Level 3: 150exp ?

我认为您正在寻找的是决定他们处于哪个级别的经验量?如: 1 级:50exp 2 级:100exp 3 级:150exp ?

if that's the case you could use a loop something like:

如果是这种情况,您可以使用如下循环:

$currentExp = x;
$currentLevel;
$i; // initialLevel

for($i=1; $i < 100; $i *= 3)
{
     if( ($i*50 > $currentExp) && ($i < ($i+1)*$currentExp)){
         $currentLevel = $i/3;
         break;
     }
}

This is as simple as I can make an algorithm for levels, I haven't tested it so there could be errors.

这很简单,因为我可以为级别制定算法,我还没有对其进行测试,因此可能会出现错误。

Let me know if you do use this, cool to think an algorithm I wrote could be in a game!

如果您确实使用它,请告诉我,想到我编写的算法可以在游戏中使用,这很酷!