Creating a game save

From TRCCompSci - AQA Computer Science
Revision as of 14:49, 21 January 2018 by Admin (talk | contribs) (RPG / Platform Game)
Jump to: navigation, search

After trying to save the game in mid play, i have essentially discovered it is difficult if not impossible to save the game mid play.

Tower / Racing Game

For a Tower Defence game, or racing game you will only need to record the data about the game such as score, lives, credits, level etc.

You could create a struct for a GameSave:

public struct GameSave
{
    public string SaveName;
    public int Score;
    public int Lives;
    public int Credits;
    public int Level;
}

We can create a variable based on this struct:

public GameSave gameData;

This method will use serialization so we need to add the following into the using section at the top of your Game1.cs:

using System.IO;
using System.Xml.Serialization;

RPG / Platform Game

For an RPG or Platformer we can focus on saving the player position in the game, and recording the game events in order to reprocess to a similar state. This will require you to think carefully about what your game will need to be able to save its current position. For example a list of the items collected, a list of the enemies killed, a list of completed challenges, and also the standard information such as player position, score etc.

This example is the constructor of the GameSave class i created.

        public GameSave(Vector2 pos)
        {
            data.name = "test";
            
            data.x = (int)pos.X;
            data.y = (int)pos.Y;
            Stream streamwrite = File.Create("mygame.bin");
            BinaryFormatter binarywrite = new BinaryFormatter();
            binarywrite.Serialize(streamwrite, data);
            streamwrite.Close();
        }