“Mike Dangers” HTML5 game engine made with Phaser and ARCADE physics – optimizing the code and making the game fair

In this new chapter of the Mike Dangers series we are going to optimize the code and make the game fair.

Making a game fair is the top priority when you randomly generate levels. Players have to have the feeling they lost the game because they weren’t skilled enough – and they will retry – and not because the level was in some way impossible to solve.

In previous step, spikes are randomly placed on the floor, and sometimes – quite often actually – they are placed right at the end of a ladder, making the player crashing into them with no way to avoid them.

To prevent this, each time I place a ladder I create some kind of “safe area”, which means each ladder has a “spike-free” radius. Sme thing goes for the spikes, each spike has its own “spike-free” radius because we do not want two spikes being too close to each other.

Another important change is I do not longer use arrays to store floors, ladders and stuff. I only use arrays for the object pooling while in-game objects are only organized in groups. This won’t affect performance because we only have a few objects on the canvas, but will make the code more understandable and easier to port in other languages.

Last but not least, floors below the player won’t fall down anymore. It just does not feel realistic.

Now they simple scroll down, fading away into some kind of fog.

Well, have a look at the game:

Just tap or click to make the player jump. Try to climb the ladders and collect diamonds while avoding spikes. If you have a mobile device, you can play directly at this link.

Now, the source code, not that big for a complete prototype:

Less than 350 lines. I’ll try to add score management and a splash screen keeping the code below 350 lines, then will start commenting it line by line. Meanwhile, download the source code.

  • kamal kishor

    Thanks for the code!