Jump to content

Recommended Posts

When you battle a user in my game you get experience, and once you get a certain amount (level*level*150) you go up a level.

 

Sometimes a player can defea somebody who can give so much experience that they would gain several levels...How would I loop it so I don't have to write it out 100 times?

 

I need it to:

1. Level up

2. Upon Level up, re-add what exp you need until the next level

3. work out how much exp you have left after using some to level

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/67815-solved-need-help-with-a-while/
Share on other sites

i don't really understand what you're saying, but this is the while format lol. also consider using a do-while loop or for loop

-------------------

while(condition) {

 

execute code

 

};

-------------

do{

 

execute code

 

} while(condition);

--------------

for (condition){

execute code

};

 

or something like that

Ummmm.... I'm not sure where you're getting this data from, but it would look something like this:

 

$user_level = $new_level = 1;
$user_exp = 0;
$exp_gain = 500;
$exp_tree = array(
     1 => 0,
     2 => 50,
     3 => 100,
     4 => 200,
     5 => 400,
     6 => 800,
);

while($exp_gain > $exp_tree[$user_level+1]) {
     $new_level++;
     $exp_gain -= $exp_tree[$new_level];
}

$level_exp = ($player_level + 1)_exp_value; // experience needed for next level

$exp_missing = $level_exp - $player_exp; // experience player still needs to advance level

while ( $total_exp_gained > $exp_missing ) // if experience needed is less than experience gained
{

$player_exp += $exp_missing; // increase player experience until next level
$player_level +=1; // increase player level
$total_exp_gained -= $exp_missing; // subtract experience just given to player from experience gained total

$level_exp = ($player_level + 1)_exp_value; // experience needed for next level

$exp_missing = $level_exp - $player_exp; // experience player still needs to advance level
}

 

I think i'd use something like this.

Ahhh... I came up with this:

 

function NewLevel($level, $exp, $gain) {
$next_exp = ($level)*($level)*150;
$sum = $gain + $exp;
while($sum >= $next_exp) {
	$level++;
	$sum -= $next_exp;
	$next_exp = $level*$level*150;
}
return array($level, $sum);
}

 

It of course doesn't need to be in function form, but it seemed easier to wrap my head around that way.  To not use it as a function, you could just set level exp and gain before it....

 

Anyway, this will take $level, $exp, and $gain and return the new level, the remaining exp, and how much exp is needed for the next level....

 

For example, if a player was level 1 with 40 exp and they gained 2000 exp, it would work like this:

 

player level 1

exp 40

gained 2000

 

sum = 2040

 

exp to level 2 would be level (1) squared times 150 1*1*150 = 150

sum > 150 so the players level would be incremented to 2, and their exp would be cut down 150 points (sum = sum - 150 = 1890)

 

exp to next level (level 3) would be current level squared times 150 (2*2*150 = 600)

1890 >= 600 so level is now 3 and sum is 1890 - 600 = 1290

 

exp to next level (level 4) would be current level squared times 150 (3*3*150) = 1350

 

1290 is not >= 1350, so the level would stay the same, and the player now has 1350 exp.

 

print_r(NewLevel(1, 40, 2000));

 

Will return:

 

Array

(

    [0] => 3

    [1] => 1290

    [2] => 60

)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.