php game, formula to calculate a level based on exp

前端 未结 7 2424
心在旅途
心在旅途 2020-12-23 10:34

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.

What i need is a formula to translate

7条回答
  •  再見小時候
    2020-12-23 11:19

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

    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

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

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

    Provided a player starts at lvl1 with no 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:

    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

    $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.

    $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

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

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

提交回复
热议问题