How To Write ZX Spectrum Games – Chapter 10

Scores and High Scores

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

More Scoring Routines

Up until now we have gotten away with an unsophisticated scoring routine. Our score is held as a 16-bit number stored in a register pair, and to display it we have made use of the Sinclair ROM’s line number print/display routine. There are two main drawbacks to this method, firstly we are limited to numbers 0-9999, and secondly it looks awful.

This method works well, though we’re still limited to a five-digit score of no more than 65535. For a more professional-looking affair complete with any number of leading zeroes we need to hold the score as a string of ASCII digits.

I have used the same scoring technique for something like 15 years now, it isn’t terribly sophisticated but it’s good enough to do what we need. This method uses one ASCII character per digit, which makes it easy to display. Incidentally, this routine is taken from the shoot-em-up More Tea, Vicar?

Simple, but it does the job. Pedants would no doubt point out that this could be done using BCD, and that all the opcodes for this are in the Z80 instruction set.

High Score Tables

High Score routines are not especially easy to write for a beginner, but once you have written one it can be re-used again and again. The basic principle is that we start at the bottom of the table and work our way up until we find a score that is greater than, or equal to, the player’s score. We then shift all the data in the table below that point down by one position, and copy our player’s name and score into the table at that point.

We can set the hl or ix register pair to the first digit of the bottom score in the table and work our way comparing each digit to the corresponding one in the player’s score. If the digit in the player’s score is higher we move up a position, if it is lower we stop there and copy the player’s score into the table one place below. If both digits are the same we move to the next digit and repeat the check until the digits are different or we have checked all the digits in the score. If the scores are identical we place the player’s entry in the table one place below. This is repeated until a score in the table is higher than the player’s score, or we reach the top of the table.

When first initialising a high score table it may be tempting to place your own name at the top with a score that is very difficult to beat. Try to resist this temptation. High score tables are for the player to judge his own performance, and there is no point in frustrating the player by making it difficult to reach the top position.