Lesson 5.2
          < Previous Page
Next Page >

Scoring and Lives

Game Maker has a built-in scoring system with a High Score Table that can be displayed at the end or beginning of a game. It can also display the number of lives available and this can be tested to see if the player has lost all his team members lives (which in this game is 5) and died.

The first thing to do is to set the player's score to 0 and the number of lives to 5 at the start of the game. This is known in programming as initialisation. At the same time the game can be set to display the player's score and number of lives left in each room's title bar.

Start by creating a new object called object_init. Add a Create event and drag the following actions into the Actions panel:

Now that object_init has been completed it can be placed in the game. It shouldn't go on the Splash Screen because this will display the score and lives in the title bar there before the game has started. Instead, put it in room_level-1 so that initialisation begins as soon as this room is entered.

You can put it on any background _sand image - it doesn't matter where. In the example below I have put near the top right hand corner of the game screen. Because it has no sprite (and doesn't need one), it shows up as a blue dot with a red question mark in it as explained earlier. If you put it on another game object by mistake, it will may delete the object underneath and you don't want this to happen. If it does, press Ctrl+Z to undo the action and reposition it somewhere else.

Save the game and run it to check that the score and lives are displayed in room_level-1's title bar after the caption like this:

Losing Lives

Part of the skill of this game is not to lose any lives accidentally as you progress though the game. Some parts of the game require you to 'sacrifice' a life to get the keys you need. But if you lose all five of your team's lives, you'll die and the game is over. You may still register a High Score, but you won't have found the Golden Scarab and gained the gift of eternal life (which actually means 1000 bonus points!).

To make this work you need to change the score and the number of lives when you bump into a mummy, collect a key or find the scarab. To do this you need to add a Set Score action to the collision events of object_mummy1, object_mummy2, object_key and object_scarab with object_player as follows:

object Set Score Value Relative
mummy1
-5
Ticked
mummy2
-5
Ticked
key
50
Ticked
scarab
1000
Ticked

Follow these steps to do this for object_scarab:

  1. Open the object_scarab Object Properties form
  2. Click on the collision event with object_player
  3. Drag the Set Score action from the score tab to the Actions panel
  4. Enter 1000 in the new score field and click on the Relative checkbox to make it increase the previous score
  5. Drag the Set Score action inbetween the Destroy the instance and Go to next room actions

It should now look like this:

Here the Set Score action has been inserted in between the Destroy the instance and Go to next room actions. Repeat these steps for all the other objects shown in the table above.

The only objects that cause your player to lose lives are the two mummies and so you now need to edit object_mummy1 and object_mummy2 to reflect this. Open object_mummy1 and click the collision event with object_player. Drag the Set Lives action from the score tab to the Actions panel and enter -1 into the lives field and then click the Relative checkbox to put a tick in it which should then look like this:

Click OK to finish. Then repeat this procedure for object_mummy2.

Finally you need to check if all the lives have been lost and if so they have, end the game. To do this you're going to create code block with a test to check the number of lives left.

Open object_mummy1 and click on the collision event with object_player. Drag the Start Block action from the control tab to the Actions panel. Then drag the Test Lives action from the score tab to the Actions panel. Enter 0 into the value field if necessary and set the operation field to equal to.

Click OK to finish. Then drag the Different Room action from the main1 tab to the Actions panel.

Select room_gameover in the new room field and Blend as the room transition. Click OK to finish.

Finally drag the End Block action from the control tab to the Actions panel. The object_mummy1's collision event with object_player should now look like this:

Now select all these new actions in the code block by dragging over them with the mouse, copy them with CTRL+C, open object_mummy2 and paste them into the collision event with object_player in a similar place.

You are now ready to test your lives. Save the game and then run it. Click through the Splash Screen and play the first room, noting that the number of lives should be displayed as 5 and the score should only increase in here as there are no mummies to bump into. In the second room there are mummies, and so each time you bump in to any of them your score should decrease by 5 and your number of lives by 1.

If your number of lives ever gets down to 0 the game should end and the Game Over screen displayed. You should deliberately lose to check this is working correctly! If you restart the game by clicking the RESTART button, the Splash Screen will be displayed again and when you enter the first room your score should be 0 and your lives should be 5 again. It's a miracle - you've been reborn! Who says you never get a second chance at life?

Adding a High Score Table

This is a quick and easy procedure because Game Maker does most of it automatically and it rounds off the game nicely. You just have to say where and when you want the High Score Table to be displayed.

Start by creating a new object called object_highscore and add a Create event in the Object Properties form. Drag the Show Highscore action from the score tab to the Actions panel and the Show Highscore form will pop up like the one shown on the right.

If you wish you can change some of the settings for the how the High Score Table is displayed in the pop up from, but I'll leave that up to you. I've set mine to use the Papyrus font size 14 and bold and I've used the background_sand object for the background image.

It looks like this when finished:

The finished Object Properties form should lool like this:

To display the Hgh Score Table, open room_gameover and add object_highscore to it anywhere on the background image, being careful not to put it on any other objects in the room. As it doesn't have a sprite it will appear as a blue dot with red question mark in it in the room. The High Score Table always pops up in the centre of the screen, no matter where you put object_highscore on the page.

Well that's it! You've now added a fully fledged scoring system with lives and a High Score Table to your game. If everything is working correctly, click Next Page > to continue.