Bruce

Oct 9, 20214 min

Let's add a Difficulty Level setting to our game

To increase the longevity of your game, it's often a good idea to add different Difficulty Levels that the player can choose to play at, or maybe you want your game mechanics to become progressively harder the further the player gets.

Adding a difficulty level was something I wanted to achieve in my latest game, The Swarm is Coming... , here's how I achieved it...

1) Set up a variable

We're going to need a global variable that we can use to store the difficulty level, in this case, we'll use D for Difficulty

2) Create a MENU for selecting the Difficulty

In the INTRO MENU event, we will use MPAGDs MENU command to give the player a Difficulty Level selector:

So, in the code above, we initially store the Easiest difficulty value (5) in D. The player then selects their preference from a menu. Their selection will set a value in OPT (1st menu item is 0, then 1, 2 ,3), by subtracting OPT from D, we will then have a Difficulty value stored in D where:

5 = very easy

4 = easy

3 = hard

2 = very hard

3) Add a timer

You'll probably be familiar with the basic timer code that many MPAGD games will use in the MAIN LOOP; it usually looks something like:

ADD 1 to A

IF A = 2

LET A = 0

ENDIF

This will then store, at any given moment, either 1 or 0 in A. We can then use this to slow things down in the game, for example in an enemy sprite event we might do something like:

IF A = 0

{move the enemy}

ENDIF

In which case, the enemy would only move every other cycle (when A = 0), they'd be at half their maximum speed.

We can adapt this concept for our difficulty setting.

At the top of MAIN LOOP 1 we'll add:

So now, dependent on the difficulty level, A will store a value from 0 to Difficulty Value -1.

So, on the VERY EASY level above, the value of D is 5, in which case, A will cycle through the values 0,1,2,3,4

Whereas on the VERY HARD level,, the Value of D is 2, in which case, A will cycle through the value 0 and 1 only

Immediately, we can see how this could impact our Enemy speed...

4) Using the difficulty setting

a) Example: Game Speed

Think back to our earlier example:

IF A = 0

{move the enemy}

ENDIF

On our VERY EASY difficulty level, the enemy will only move once every 5 cycles, while on VERY HARD, the enemy will move every cycle.

b) Example: Damage

We can also use our difficulty level to determine things like Damage. For example, in your game your player might have a HEALTH value, which is stored in variable H with a maximum value of 100.

When the player collides with an enemy, maybe you:

SUBTRACT 10 FROM H

But we could , instead, use our difficulty setting to determine the amount of damage done. We could just right a big nested IF statement (IF D = 5 SUBTRACT 20 FROM H ELSE IF D = 4...) but that's a bit cumbersome, instead we'll do a bit of quick maths and 'borrow' the RND variable a temporary variable, we might do something like this in the enemy event:

So, on our Very hard level we subtract 2 from 10 to get 8 damage points that are then subtracted from the players health counter (H)

But if we are on the VERY EASY level - it only causes 10 - 5 = 5 damage points.

Obviously, you can build your own damage rules to inflict the required amount of pain!

c) Example: Frequencies

Another use case for your difficulty levels could be the frequency with which something happens, perhaps you have a Bonus sprite that appears every so often - you can use the D setting to make it appear more frequently on easier levels than on harder levels

d) Example: Time Limits

Adding a time limit to your game, either to complete the entire game or to complete a single screen, is an ideal opportunity to use your difficulty levels to make the game more (or less) challenging dependent on the level selected by the player. This is something I implemented in The Swarm is Coming... each mission has to be completed in a set amount of time, the easier the difficulty level the more time you get. The code behind this looks a bit like:

This will then give you a number of Minutes (M) and Seconds (S) that the player has to complete the game. Next, we need to add a countdown mechanism running in the MAIN LOOP that will decrement the timer:

To do this we need a timer that will give us a second, given that MPAGD runs at 25fps we could add a variable (T) in the initialisation (LET T = 0), then in the MAIN LOOP:

e) Example: Scores & Bonuses

You can also use your difficulty level to reward player achievements when playing on your harder settings. Perhaps by using MPAGDs SCORE & BONUS commands

For example, perhaps when an enemy is killed:

Again, we divide first from a given number as in our example the hardest difficulty level has the lowest D value, this then gives us a multiplier that we mutiply the points by (20) - then we score that amount.

Summary

Difficulty levels are really easy to achieve in MPAGD and with a bit of careful coding don't require a lot of memory, yet can significantly enhance the playability and longevity of your games. In my example, the value of D never changes - once it is set by the player, it remains constant throughout the game. If your mechanics require it's manipulation remember to immediately reset it back to its original value after you have manipulated it...else unexpected things may happen! Rather, I prefer to use a temporary variable like RND if I need to do some maths involving D.

    2590
    6