An XNA Top Scores program

Today we are going to write the code to maintain a top ten list. We will have to:

  1. Read the current top ten names and scores from a file.
  2. Add a new name and score to the list.
  3. Sort the list.
  4. Write the sorted list back out to the file.

All of the things on the above list are done once.

  1. Reading the current top ten needs to be done once before everything else.
  2. Adding a new name needs to be done once after the game has been played (although in this program there is no game to be played, so we'll just do it right away.).
  3. Sorting needs to be done once after the new name and score have been added to the listat the end of the game.
  4. Writing the sorted list back to the file needs to be done onceafter the data has been sorted.

Create a new XNA project called Day19TopScores. A game program would need to keep track of the top scores. Assume that we already have a Top Scores file out there and we are going to keep track of the top 10 scores. Create a file in NotePad. Name the file Top10.txt. Each name is padded with enough blanks to make it 10 characters long.

Nobody 10

Nobody 20

Nobody 30

Nobody 40

Nobody 50

Nobody 60

Nobody 70

Nobody 80

Nobody 90

Nobody 100

Save it in your bin/x86/debug folder of your Day19TopScores project.

Our program will have the following states:

  1. Splash (read the file and display the top 10)
  2. GetName
  3. GetScore
  4. Playing
  5. GameOver
  1. Add current name and score to the list (once)
  2. Sort the list (once)
  3. Write the list back out to the file (once)

We need to:

  1. Read in the top scores file in Initialize.
  2. When in Splash, display the top scores in Draw every time.
  3. When user presses Start, switch to the GetName state.
  4. When in the GetName state get the player's name.
  5. When the player's name has been read, switch to the GetScore state.
  6. When in the GetScore state, get the player's score.
  7. When the player's score has beenread, switch to the Playing state.
  8. When the user presses the Start button on the gamepad, switch to the GameOver state.
  9. When in the GameOver state, sort the new data.
  10. When done sorting the data, write it out to the data file.
  11. When in the GameOver state, display the new top 10 on the screen on every tick.
  12. When the user presses the Back button, end the program.

I used the following variables:

IAsyncResult keyboardAsyncResult; //*********************************

bool nameRequested, scoreRequested; //*********************************

bool listSorted, fileWritten;

GamePadState gamePad, prevGamePad;

enumGameStates

{

Splash,

GetName,

GetScore,

Playing,

GameOver

}

GameStates gameState;

structPlayerStruct

{

publicstring PlayerName;

publicint PlayerScore;

}

constint maxPlayers = 11;

PlayerStruct[] player;

StreamReader inFile;

StreamWriter outFile;

SpriteFont font;

Note that if we are going to read a file and write a file, we need to add the following:

using System.IO;

The Initialize method

player = newPlayerStruct[maxPlayers];

nameRequested = false;

scoreRequested = false;

listSorted = false;

fileWritten = false;

ReadFile(player);

gameState = GameStates.Splash;

A procedure to read the top scores

To read in the top scores file. We have an array with room for 11 (0 through 10) scores even though we only keep track of the top 10. This will allow us to add the current player's score to the list (in the last position, index=10).

Note:

  • The use of Substring, Trim, and Convert.ToInt32.

void ReadFile(PlayerStruct[] player)

{

inFile = newStreamReader("Top10.txt");

for (int i= 0; i < maxPlayers - 1; i++)

{

string line = inFile.ReadLine();

player[i].PlayerName = line.Substring(0, 10).Trim();

player[i].PlayerScore = Convert.ToInt32(line.Substring(10));

}

player[maxPlayers - 1].PlayerName = "JUNK ";

player[maxPlayers - 1].PlayerScore = 0;

inFile.Close();

}

Before we write the methods to get input from the player, we will write some other methods (that are easier to write).

Add code to call the following from within the Draw method and test your program.

A procedure to display the top 10:

NOTE:

  • Output is padded for alignment. Note that this only works with a monospaced font like Courier New.

void DisplayList(PlayerStruct[] player)

{

Vector2 nameVector = newVector2(0, 0);

float lineHeight = font.MeasureString("X").Y;

for (int i = 0; i < maxPlayers; i++)

{

nameVector.Y += lineHeight;

spriteBatch.DrawString(font, player[i].PlayerName.PadRight(15) +

player[i].PlayerScore.ToString(), nameVector, Color.White);

}

}

10/10/2018Day19--An XNA Top Scores programFULL.docxPage 1 of 3