High score entry

9-16-2018
Once we have determined that a high score has been achieved, we need a way to enter in the initials of the player so that they can be proudly be displayed next to the score. The following function allows you to accomplish this.

First of all, here’s our code.

void highScore()
{
letter = 65;//starting at the letter A
arrayCounter = 0;

PrintStr(highScoreMessage1, 0x090B);
PrintStr(highScoreMessage2, 0x030C);
while (1)
{
if( Input() & 0x04 )// A go down
{
-- letter;
}
if( Input() & 0x08 )// Q go up
{
++ letter;
}
if( Input() & 0x10) // Space (FIRE)
{
++ arrayCounter;
}
if (letter > 90) letter = 65;//if you go past Z, roll back to A
if (letter < 65) letter = 90;//if you go past A, roll back to Z

initials[arrayCounter] = letter;
sprintf(buffer, "%c", letter);

if (arrayCounter == 0)
{
PrintStr(buffer, 0x0D0E);//0x1901
}
if (arrayCounter == 1)
{
PrintStr(buffer, 0x0E0E);//0x1901
}
if (arrayCounter == 2)
{
PrintStr(buffer, 0x0F0E);//0x1901
}
if (arrayCounter == 3)
break;

FRAME;
Pause (10);
}

scores[5].hiInitals[0] = (initials[0]);
scores[5].hiInitals[1] = (initials[1]);
scores[5].hiInitals[2] = (initials[2]);
scores[screenCounter].hiScoreList = yourScore;

insertionSort(); //sort high score table

printHighScores();
}

Well, we are going to start out by setting up some variables. The variable letter sets up ascending and descending through the alphabet, whereas the arrayCounter will step through a small array.

letter = 65;//starting at the letter A
arrayCounter = 0;

In Z88dk and the spectrum, the letter A is found in position 65, while the letter Z is found in position 90.

Now, we are going to print up a congratulatory message to show that the player has achieved a new high score.

PrintStr(highScoreMessage1, 0x090B);
PrintStr(highScoreMessage2, 0x030C);

We set up an endless loop.

while (1)

This loop will run until all of the initials have been entered.

Our next three actions are in the form of IF statements, these look for 3 inputs, Up, moves upwards through the letters, Down, moves downwards through the letters and Fire, which will enter the letter.

if( Input() & 0x04 )// A go down
{
-- letter;
}
if( Input() & 0x08 )// Q go up
{
++ letter;
}
if( Input() & 0x10) // Space (FIRE)
{
++ arrayCounter;
}

Up and down, increase or decrease the letter variable, while fire will advance the arrayCounter variable.

Our next two IF statements

if (letter > 90) letter = 65;//if you go past Z, roll back to A
if (letter < 65) letter = 90;//if you go past A, roll back to Z

Make sure that we don’t go past the letters. You could increase the range if you wanted to include other characters or use lower case letters. In our case, I’m only interested in the all-caps version of this entry method.

initials[arrayCounter] = letter;
sprintf(buffer, "%c", letter);

Here we temporarily store the selected letter into the array and print that letter on the screen.

Sprintf is a built-in conversion what will take a number and convert it to a letter and send to the variable buffer.

The next 4 if statements check to see where the arrayCounter is and prints that letter in the correct position on the screen. When the arrayCounter makes it to the 4th statement, we know all of the letters are entered and will now break out of the loop.

The next 4 lines

scores[5].hiInitals[0] = (initials[0]);
scores[5].hiInitals[1] = (initials[1]);
scores[5].hiInitals[2] = (initials[2]);
scores[screenCounter].hiScoreList = yourScore;

Place all 3 initials into the 5th position of the struct along with the score the player has achieved.

Now we sort the results.

//sort high score table
insertionSort();

And finally, we print the results

printHighScores();

That should wrap up the end of the high score table.

As always, happy coding.

Advertisements

Author: andydansby

I'm a hobbyist coder working with the ZX Spectrum. Living in New York state near the Syracuse area. I grew up in Virgina. The first computer my parents bought for me was a Timex Sinclair 2068.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s