Adding Experience to our Combat System(PHP)

Building Browsergames reader Mehmet Simsek recently e-mailed me to point out something that I can’t believe I missed: our combat system doesn’t have any experience or levelling built into it!

Right now, when a player kills a monster in the forest, they get gold and an item – but they don’t get any experience. Thankfully, adding experience to our combat system is a fairly simple task: to begin with, we’ll add a few more stats to the database:

INSERT INTO stats(display_name, short_name) VALUES
    ('Experience','exp'),
    ('Experience Remaining', 'exp_rem');

These two stats will let us keep track of the player’s current experience, and the experience that a monster gives after it has been killed. We will be using both to determine when the player has levelled up – although what happens after they level up is up to you.

We will also need to insert some starter values for our monsters:

INSERT INTO entity_stats(stat_id,entity_id,value,entity_type) VALUES ((SELECT id FROM stats WHERE short_name='exp'),(SELECT id FROM monsters WHERE name='Crazy Eric'),5,'Monster');
INSERT INTO entity_stats(stat_id,entity_id,value,entity_type) VALUES ((SELECT id FROM stats WHERE short_name='exp'),(SELECT id FROM monsters WHERE name='Lazy Russell'),10,'Monster');
INSERT INTO entity_stats(stat_id,entity_id,value,entity_type) VALUES ((SELECT id FROM stats WHERE short_name='exp'),(SELECT id FROM monsters WHERE name='Hard Hitting Louis'),15,'Monster');

As you can see, we have inserted rows into the entity_stats table that will make it so that the Crazy Eric monster gives 5 experience, Lazy Russell gives 10, and Hard Hitting Louis gives 15 experience.

With the neccessary database changes made, it’s just a matter of opening up forest.php and making some changes:

105
106
107
108
109
110
111
112
113
114
115
116
$monster_exp = getMonsterStat('exp',$monsterID);
$smarty->assign('exp',$monster_exp);
$exp_rem = getStat('exp_rem',$userID);
$exp_rem -= $monster_exp;
$level_up = 0;
if($exp_rem <= 0) {
    // level up!
    $exp_rem = 100;
    $level_up = 1;
}
$smarty->assign('level_up',$level_up);
setStat('exp_rem',$userID,$exp_rem);

As you can see, we are also setting some new variables in our Smarty template – so we will need to modify that as well:

19
20
21
22
23
{if $won eq 1}
	<p>You killed <strong>{$smarty.post.monster}</strong>! You gained <strong>{$gold}</strong> gold, and {$exp} experience.</p>
	{if $level_up eq 1}
	    <p><strong>You gained a level!</strong></p>
	{/if}

With those changes made, test out your game – you should notice that the message “You gained a level!” will appear occasionally while you fight monsters in the forest.

Extra Credit

  • Right now, a player will level up on their first fight, and then start levelling normally. Make it so that the first level up does not happen.
  • Modify the code given so that when a player levels up, the value of their magic, defence, or attack stat gets raised by one – randomly.
  • Modify the code given so that when a player levels up, they are allowed to choose which attribute to increase.
  • Modify the code given so that when the player levels up, a special ‘level’ stat is modified – which is then displayed on the main page.

Wish there was more?

I'm considering writing an ebook - click here.

.

Luke is the primary editor of Building Browsergames, and has written a large portion of the articles that you read here. He generally has no idea what to say when asked to write about himself in the third person.

Tags: , ,

Monday, January 5th, 2009 buildingbrowsergames, medieval
  • Hi luke, I really love this tutorial, I followed it, and I can say that I learned very very much...
    I even got some improvements :D

    Improvement for level of the player:

    Add level stat:
    INSERT INTO stats(display_name, short_name) VALUES ('Level','lvl');

    Replace the change you made to forest.php with:

    $monster_exp = getMonsterStat('exp',$monsterID);
    $smarty->assign('exp',$monster_exp);
    $exp_rem = getStat('exp_rem',$userID);
    $exp_rem -= $monster_exp;
    $level = getStat('lvl',$userID);
    $level2 = 0;
    $exp = getStat('exp',$userID);
    if($exp_rem<=0) {
    $exp_rem = 100+$exp_rem;
    $level +=1;
    $level2 = 1;
    setStat('lvl',$userID,$level);
    }
    $smarty->assign('level_up',$level2);
    $smarty->assign('nlevel',$level);
    setStat('exp',$userID,getStat('exp',$userID) + $monster_exp);
    setStat('exp_rem',$userID,$exp_rem);

    In register.php add this to the block of starting values of the player:
    setStat('lvl',$userID,'1');

    In index.php add those to the block of smarty assignments:
    $smarty->assign('level',getStat('lvl',$userID));
    $smarty->assign('experience',getStat('exp',$userID));
    $smarty->assign('exp_remaining',getStat('exp_rem',$userID));

    In index.tpl add those to the block of user stats:
    <li>Current level: {$level}</li>
    <li>Experience: {$experience}</li>
    <li>Experience needed until level {$level+1}: {$exp_remaining}</li>

    This is it :D

blog comments powered by Disqus

About

Building Browsergames is a blog about browsergames(also known as PBBG's). It's geared towards the beginner to intermediate developer who has an interest in building their own browsergame.

Sponsors

Got Something to Say?

Send an e-mail to luke@buildingbrowsergames.com, or get in touch through Twitter at http://twitter.com/bbrowsergames