Many RPGs implement a Level system. This is not to be confused with playable levels - i.e. different worlds or stages you visit in the game.
In RPGs, Levels instead refer to the Skill Level of the player. For example, your player may be a Level 1 Wizard, or a Level 13 Archer, and so on.
As you defeat enemies, you gain Experience Points - i.e. XP. Earn enough XP and you Level Up - where you increase a skill level. What this does depends on the game, but in general a higher Skill Level makes you stronger and better at fighting.
To explore today's example, either follow the instructions below or download the completed game to study it:
To get started we're going to enable the Satus Bar Extension.
Find the Advanced panel in the Toolbox on the left.
Find the Extensions panel at the bottom of the Toolbox and click it.
On the following screen, find the Extension called status-bar and click it. This adds a new panel called Status Bars to your Toolbox.
We're going to change our Enemy creation code to make it easier to test our Levelling system.
Put down an on game update every 500ms event. This is found in the Game panel.
In the Session 2 we made enemies spawn using a Repeat block. We're going to replace this and make it so enemies never run out.
Find the repeat 4 times: set enemySprite to... set of blocks we used in on start.
Remove the repeat and everything inside it from on start and place it down in empty space temporarily. Do not delete it!
Move everything inside the repeat block to the on game update every 500ms event we created in step 3.
Now we can delete the repeat.
What we have so far will infinitely create new Enemies, but there may be too many. We're going to need a variable to keep track of how many Enemies are created.
In the Variables panel, create a numberOfEnemies variable.
Now we can place the set numberOfEnemies to 0 block in on start.
Find in the Sprites panel the on created sprite of kind Player event.
This event triggers whenever a certain sprite is created. However first we need to change the kind to be Enemy instead.
In here we can change numberOfEnemies by 1. This block is found in the Variables panel.
Now we need to do exactly the same with on destroyed sprite of kind Player.
Now we need to check how many Enemies we've counted.
Inside on game update we're going to add an if statement. This is found in the Logic panel.
We need to make sure that the if surrounds all the blocks we already had.
Now we can compare the numberOfEnemies with a target number of our choosing.
For this we will need a 0 < 0 comparison block from the Logic panel.
This goes inside the if.
Then, numberOfEnemies goes in the left-hand slot of the 0 < 0 and the right-hand slot can contain any number you like.
Now we can add levels!
First, create a new Level variable and set it to 1 in on start.
To increase the level, the player must first gain Experience.
We're going to represent the player's progress to the next level with a Status Bar.
From the Status Bar panel, find set statusbar to create status bar sprite width 20 height 4 kind Health.
Place this in on start. Then change Health to Energy.
At the moment the Status Bar is in the middle and quite small.
To make it bigger, change the width and the height.
To move it elsewhere (like to the top of the screen) we can use the set mySprite position to x 0 y 0 block from the Sprites panel.
Change mySprite to be statusbar and adjust the x and y.
To give our player Experience Points (XP) we need to modify our combat code from Session 2.
Find the on sprite of kind Player overlaps otherSprite of kind Enemy event.
Here we need to update our code so that after the enemy is destroyed we increase the XP by a certain amount.
This is done with change statusbar value by 0 from the Status Bar panel.
Now we are increasing the XP value we should probably decide what happens when it becomes full.
We can do this with the on status bar kind Health <= 50 % status event found in Status Bars. Place this in empty space.
We need to change three things about this event:
Change Health to Energy.
Change <= to >=. (see image below)
Change 50% to 100%.
Inside this new event we're going to celebrate levelling up!
First things first though, we should reset the XP value down to 0 so the player can level up again.
set statusbar value to 0 is found in the Status Bars panel. Place it in the event.
Next we can finally increase the player's level.
In the Variables panel we can pull out a change _____ by 1 block. Here we can set the variable in question to be Level.
We can make levelling up more impressive by adding a splash " " block found in the Game panel. Here we could write something like "Level Up!". We could also add a sound effect.
To make your game more interesting and a little more challenging you could give enemies their own health bar. Then, rather than destroying them immediately after attacking them you could instead remove some health from their bar. Finally, only when their bar is empty does the sprite get destroyed and the XP earned.