html5 mario style game with coins quintus

Adding Coins and Lives to the Mario-Style HTML5 Platformer

NOTE: This tutorial is not updated to the latest Quintus version and doesn’t run with it.

This tutorial is the third part in the Mario HTML5 platformer series, so get ready to dig in deeper into the awesome Quintus game framework. In this tutorial we’ll learn how to add coins, lives to our player and how to show all of this in a “player stats” area.

Are you a French speaker? If so, thanks to Alexandre Bianchi you can enjoy an adapted French version of this tutorial.

Tutorial goals

  1. Implementing “lives” for our player.
  2. How to collect coins.
  3. Loading the coins location from a JSON object.
  4. Displaying a “player stats” area and keeping it updated.
  5. Searching for an object in a scene.

Get Another HTML5 Gamedev Shot

If you are enjoying this tutorial and want a lot more of this stuff feel free to check out our HTML5 game development online courses that cover all the basics to get started:

HTML5 Mobile Game Development for Beginners

Requirements

The requirements for the dodgy Mario clone tutorial series are:

  • Familiarity with HTML, CSS, JavaScript and basic object oriented concepts.
  • Clone or download Quintus from it’s Github page.
  • Setup a local webserver. We need to run the code in this tutorial in a web server and not by just double clicking on the files. WAMP for Windows, MAMP for Mac. On linux just type sudo apt-get install apache2.
  • Download and install the Tiled game editor, available for Linux, Mac and Windows
  • Have your favorite IDE ready (Netbeans, Eclipse, Sublime2, Notepad++, VIM, or any tool you use for coding).
  • Have your knuckles cracked and ready for a new dive into HTML5 gamedev.

Tutorial Assets

Get the full tutorial source code and images here. All images used in this tutorial have a Public Domain license.

Heads up!
I’ve provided the Quintus files among the tutorial source code but keep in mind that the framework is under heavy development. It’s be best if you just grab them straight from the Github page. Also, keep an eye on the repository as you work on your games since new features are being added.

Player Stats

In these series we’ve been working all along with just one scene: the level that contains the actual game. Quintus allows us to keep on many scenes on the screen so that for instance we can be scrolling through the level in one, but the other ones stay on their same place and don’t get scrolled.

Being more clear on this point, imagine we just use one scene, the level one, and we add player stats to it such as the number of lives and the number of coins. It’s all gonna look good until you start working and the viewport camera moves away from your initial location. The player stats will stay behind as the rest of the level. This concept is explored in more depth in our HTML5 Educational Game tutorial.

Anyway, let’s create a second scene called “gameStats”:

What we are doing here is create a new scene that has a gray container which goes on the bottom of the page. This container has two Q.UI.Text objects, one for the lives and one for the coins. They both start with default values. In order to learn more about the Q.UI.Text and the containers check the file quintus_ui.js from the library and the HTML5 Educational Game tutorial.

Staging the game stats scene would be the last step. The asset loading should look like this (see that we are also adding a coin.png asset file, to be used next):

The second parameter when staging the scene is the index (0 by default). Scenes with higher index show on top of scenes with lower index.

Coins

Righto. All platformers need coins or something like them (diamonds, gold, fruits?). Their implementation using Quintus and the stuff we’ve learned so far is quite straightforward:

We’ll load the coins in our level initialisation using a JSON object in a similar manner to how we did it with the enemies:

Heads up!
It’s recommended you try to load all the entities in the game such as enemies, items, etc, using JSON objects. JSON is a data exchange protocol which is commonly used to receive and send information to and from the web. If your plan is to eventually load level data from the web or from the local storage it’s good that you keep JSON in mind and get used to working with this format.

Our level can be filled up with coins now but we still need to be able to grab them and keep a total of coins. Let’s update a bit our Player definition:

This is firstly, setting an initial value of 0 coins. We are checking the collision with coin objects. If we find a coin then destroy the coin and increase the number of coins in 1. We still need to update the GUI, which we’ll be doing next.

Searching for the GUI

We grabbed a coin, destroyed it and increased our coin count. The GUI needs to be updated, but how do we find it? we need to search for it. Quintus has it’s own way of searching for objects:

By doing

we are searching for all Q.UI.Text objects in the scene located at index 1. This returns an object, where the property “items” contains an Array with all the results. In this case the index 1 (second item in the Array) contains the text we are looking for. Index 0 of the array contains the lives text.

Once we find the text object we are looking for, we just update the label property (object.p.label, don’t forget the p which we always use to access an entity’s parameters in Quintus).

Player Lives

Our game so far is extremely tough. One strike and you are out! we’ll implement now player lives or energy. If the lives run out then we are getting the game over kick.

There are many ways in which lives / energy can be implemented. The way it’ll be in this example is:

  1. The player is hit by an enemy (same collision rules we have in place)
  2. The lives go minus one
  3. The player is “invincible” for 1 second, so that if you are hit again right away it won’t take a second life
  4. After the 1 second you can be hit again if you are not careful
  5. If the lives run out.. game over!

When the player is hit, ideally, the player sprite should be blinking for that second or have some sort of visual cue. Since this is just a tut not a real game we won’t show any visual cue.

Setup an initial number of 3 lives:

Modify the enemies so that now they don’t kill you, but damage() you instead.

The damage() method in the player will follow the steps mentioned before. It will make you invincible for 1 second and then you’ll be damageable again. We also need to update step() to keep track of this 1 second. The following goes inside Player:

This part:

is updating the GUI in a similar way to the coin counter, but using a first() method that the returned object comes with (remember that index 0 in the Array was the lives text).

This gives us a playable demo where you can collect coins, jump around, kill bad guys and if you are not careful they can take your lives away and kill you!

Congratulations

Congrats if you’ve got all the way here! you have now the basics of what can become a great game! Some ideas on what to build next:

  • Visual cue when the player is damaged
  • Multiple levels
  • Level boss
  • Grab extra lives
  • Enemies that take coins away instead of lives

What Else to Check Out

If you like my teaching style, at Zenva we have two high quality video courses on HTML5 game development where we build several examples of different types of games, such as a top-view shooting bad guys Zelda-style game, spaceship games, farming games, virtual pet games and many other examples!

HTML5 Mobile Game Development for Beginners

Published by

Pablo Farias Navarro

Pablo is an educator, developer and entrepreneur. Founder of Zenva, Pablo has published over 30 online courses that teach game, app and web development. Pablo has also created educational content for companies such as Amazon and Intel.

Share this article