Hi there, I'm currently studying games dev and doing Sturctured programming instead of objective oriented programming.
I'.m using XNA4.0 & Trying to do a game with a highscore table and trying to get the user to enter their name then store it the display their score once the games done. But having problem storing whats been wrote on screen
This should get you moving in the right direction. Although this will just save the list to a file named 'Highscores.txt', implementing the text entry is dependent on your drawing code/text entry handling. To use this, you just need to create a list of HighScoreEntrys and pass it in with the entry.
using System.IO;
class HighScoreEntry
{
public string Name;
public int Score;
}
class AddHighScore
{
public AddHighScore(HighScoreEntry Entry, List<HighScoreEntry> Scores)
{
Scores.Add(Entry);
}
}
class SaveScores
{
public SaveScores(List<HighScoreEntry> Scores)
{
string Filename = "Highscores.txt";
if (File.Exists(Filename)) { File.Delete(Filename); } // If the file already exists, delete it so we can create a new one
StreamWriter Writer = File.CreateText(Filename);
foreach (HighScoreEntry thisEntry in Scores)
{
Writer.WriteLine(Scores);
}
Writer.Close();
}
}
"May the mercy of His Divine Shadow fall upon you." - Stanley H. Tweedle, Security Guard class IV, The League of 20,000 planets
Lol, you were editing the post as I was replying.. thanks for the extra info. I have to run out and pick up dinner, but I'll be back in about 30 minutes or so, and I'll make another post, showing how to read the file back into the list<HighScoreEntry>… it's basically the reverse of SaveScores, changing the StreamWriter to a StreamReader.
"May the mercy of His Divine Shadow fall upon you." - Stanley H. Tweedle, Security Guard class IV, The League of 20,000 planets
PiscesMike is definitely on the right track with his answer, though I might combine the AddHighScore and SaveScore classes into one HighScoreManager class with the two methods.
It sounds like you're trying to avoid object-oriented programming (which is actually counter to the C# language itself—you should use object-oriented programming if it makes sense, and in this case it probably does) but if you wanted to avoid OOP, you could simply add these as methods to your main game class (Game1 by default).
To actually get the input, you would need to listen for key presses in the Update method, which I talk about here: http://rbwhitaker.wikidot.com/basic-keyboard-input
RB is right, after a little tinkering with the code, I redid things into a separate class which will load and save the highscores:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace highscorestest
{
class HighScoreEntry
{
public string Name;
public int Score;
public HighScoreEntry(string inName, int inScore)
{
Name = inName;
Score = inScore;
}
}
class Highscores
{
public void AddHighScore(HighScoreEntry Entry, List<HighScoreEntry> Scores)
{
Scores.Add(Entry);
}
public void SaveScores(List<HighScoreEntry> Scores)
{
string Filename = "Highscores.txt";
if (File.Exists(Filename)) { File.Delete(Filename); } // If the file already exists, delete it so we can create a new one
StreamWriter Writer = File.CreateText(Filename);
foreach (HighScoreEntry thisEntry in Scores)
{
Writer.WriteLine(thisEntry.Name);
Writer.WriteLine(thisEntry.Score);
}
Writer.Close();
}
public void LoadScores(List<HighScoreEntry> Scores)
{
string Filename = "Highscores.txt";
if (!File.Exists(Filename)) { return; } // If the file does not exist, return without doing anything
StreamReader Reader = File.OpenText(Filename);
while(Reader.EndOfStream==false)
{
HighScoreEntry thisEntry = new HighScoreEntry(null, 0);
thisEntry.Name=Reader.ReadLine();
thisEntry.Score=Convert.ToInt32(Reader.ReadLine());
Scores.Add(thisEntry);
}
Reader.Close();
}
}
}
You'll want to instantiate an instance of the highscorestest in the game1.cs, like this:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace highscorestest
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Highscores Highscores = new Highscores();
List<HighScoreEntry> Entries = new List<HighScoreEntry>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// You should be using a list<> to track high scores. Arrays could also be used, but that's so inefficient...
// We've created a list of Object.Name and Object.Score properties wrapped together in a class called HighScoreEntry
// We also have the 'Highscores' Highscores = new Highscores(); class - see the clas implementation
/*Highscores.AddHighScore(new HighScoreEntry("ABC", 10000), Entries);
Highscores.AddHighScore(new HighScoreEntry("ABC", 9000), Entries);
Highscores.AddHighScore(new HighScoreEntry("ABC", 8000), Entries);
Highscores.AddHighScore(new HighScoreEntry("ABC", 7000), Entries);
Highscores.AddHighScore(new HighScoreEntry("ABC", 6000), Entries);
Highscores.AddHighScore(new HighScoreEntry("ABC", 5000), Entries);
Highscores.AddHighScore(new HighScoreEntry("ABC", 4000), Entries);
Highscores.AddHighScore(new HighScoreEntry("ABC", 3000), Entries);
Highscores.AddHighScore(new HighScoreEntry("ABC", 2000), Entries);
Highscores.AddHighScore(new HighScoreEntry("ABC", 1000), Entries);
Highscores.SaveScores(Entries);*/
Highscores.LoadScores(Entries);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
You'll see some stuff commented out about, that shows how to add entries!
"May the mercy of His Divine Shadow fall upon you." - Stanley H. Tweedle, Security Guard class IV, The League of 20,000 planets