Part 8 – Scoring

In this article we’ll add scoring and a hi-score. A video is available also here:

https://www.youtube.com/endscreen?feature=vm&v=bHiYkNIQ824

Here’s the full code to paste into your IDE, click to expand.

Let’s go through it in detail. Instantly at line 3 we have this extra line

#include <EEPROM.h>

In every processor used in the Arduino range of boards there is some memory that is referred to as EEPROM (Electrically Erasable Programmable Read Only Memory).  This is memory that we can access to store information during program runtime that will not be lost when the processor is powered down. It will remain there for all intents and purposes for ever. You may wonder why not have all dynamic memory (storage for program data, variables etc.) in EEPROM memory. Well, generally speaking it is slower to access than normal volatile memory so would reduce performance for general programs.  So to access this facility on our Arduino’s we use this library written for that purpose. Why do we need to use this special memory? To store the Hi-Score, we want this to be kept even when the unit is switched off.

Player Score

We’ve added a extra property to the player structure, their score.  Putting in the players structure would allow for an easier expansion to a two player option in the future if we decided to implement one. Also note we’ve added the Lives property to the player but for now we will do nothing more with it. The hi-score is defined at line 213:

unsigned int HiScore;

Checking the Hi-Score on startup
When we power up our unit it needs to retrieve the hi-score, these lines accomplish that task.

The variable HiScore is a integer variable and takes up 2 bytes  to store up to a maximum value of 65535, which for this implementation will be adequate. Note that with a brand new Arduino all the EEPROM bytes are set to “1”s. Two bytes that are all “1”s is 65535. So on initialisation if the value read using line EEPROM.get(0,HiScore) is this value then we clear it to 0. Later we will add in a button to reset the Hi-Score manually should you wish to. Let’s look closer at that line

EEPROM.get(0,HiScore);

This calls a routine called get that will start retrieving bytes from the location passed (0, which is the start of EEPROM storage). It will read the number of bytes that the type HiScore needs, in this case as its an INT then it will read two bytes (0 and 1) and store them in HiScore. If you wanted to store another value in EEPROM then it must start at location 2 and not 1 as 1 is already being used to store part of the hi-score. It is very important to know how much storage space certain variable types require and to keep track of your EEPROM storage usage otherwise your code will not work correctly.

So if we are setting the Hi-Score to 0 then the line EEPROM.put(0,HiScore); will achieve this. It puts the values of the two bytes for the HiScore variable into the EEPROM starting at location 0 (so will use location 0 and 1 because we need two bytes).

Different Aliens – Different Scores
Different Aliens score a differing amount, the higher up the more points are awarded. The following routine returns the score for a particular Invader on a particular row.

All that is needed is to pass in the row number of the Alien that has just been destroyed and it will return the appropriate score. 0 is the top row and 2 is the bottom row. We call this routine in the function MissileAndAlienCollisions, listed below;

Line 410 passes in the current row of the destroyed Invader and the players score is updated accordingly.

We also update the players score when the Mystery-ship is destroyed, here’s the collision checking routine from the main code.

Here you can see that line 383 updates the players score with whatever bonus has been awarded.

And finally we clear the players score back to 0 when the game starts:

What if the player beats the hi-score?
Well, at present nothing happens, that bit of code to handle checking and storing new hi-scores will come in a later article when we will display a congratulations screen as well but for now it will wait until we’ve more structure to the game.

Displaying the score

Looking in the display routine we find the previous lines for Mystery-Ship bonus and now we’ve added some more for score and lives.  We can see that we only display them if there is no bonus being displayed. This is because if the bonus is printing over the score then having both together on screen at once makes the bonus illegible.

See you next time.