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

