[原创]Alex
游戏过程中,通常需要存储游戏状态。在下次启动游戏时,重新载入游戏,继续游戏。
实现过程非常简单,首先建立一个结构体定义需要保存的游戏参数,然后通过XmlSerializer类将其保存为一个XML文档。
1 interface GameData
2 {
3 int GameID;
4 string PlayName;
5 DateTime Time;
6 }
2 {
3 int GameID;
4 string PlayName;
5 DateTime Time;
6 }
XNA游戏可运行在XBOX和PC,做到两者的兼容,需要使用StorageDevice将数据保存到磁盘。
首先 一步创建一个StorageDevice,这个过程需要打开Guide并终止程序,直到用户关闭Guide。使用Guide必须在Game类的构造函数中进行注册。
1 public AlexGame()
2 {
3 graphics = new GraphicsDeviceManager(this);
4 Content.RootDirectory = "Content";
5 Components.Add(new GamerServicesComponent(this));
6 }
2 {
3 graphics = new GraphicsDeviceManager(this);
4 Content.RootDirectory = "Content";
5 Components.Add(new GamerServicesComponent(this));
6 }
第二步 在Update方法中捕获事件。
代码
1 //获取键盘状态
2 KeyboardState keyState = Keyboard.GetState();
3 //Guide不显示
4 if (!Guide.IsVisible)
5 {
6 //键盘按下S
7 if (keyState.IsKeyDown(Keys.S))
8 Guide.BeginShowStorageDeviceSelector(FindStorageDevice, "saveRequest");
9 //键盘按下L
10 if (keyState.IsKeyDown(Keys.L))
11 Guide.BeginShowStorageDeviceSelector(FindStorageDevice, "loadRequest");
12 }
2 KeyboardState keyState = Keyboard.GetState();
3 //Guide不显示
4 if (!Guide.IsVisible)
5 {
6 //键盘按下S
7 if (keyState.IsKeyDown(Keys.S))
8 Guide.BeginShowStorageDeviceSelector(FindStorageDevice, "saveRequest");
9 //键盘按下L
10 if (keyState.IsKeyDown(Keys.L))
11 Guide.BeginShowStorageDeviceSelector(FindStorageDevice, "loadRequest");
12 }
第三步,定义FindStorageDevice方法
代码
1 private void FindStorageDevice(IAsyncResult result)
2 {
3 StorageDevice storageDevice = Guide.EndShowStorageDeviceSelector(result);
4 if (storageDevice != null)
5 {
6 if (result.AsyncState.ToString() == "saveRequest")
7 {
8 //保存游戏
9 SaveGame(storageDevice);
10 }
11 else if (result.AsyncState.ToString() == "loadRequest")
12 {
13 //加载游戏
14 LoadGame(storageDevice);
15 }
16 }
17 }
2 {
3 StorageDevice storageDevice = Guide.EndShowStorageDeviceSelector(result);
4 if (storageDevice != null)
5 {
6 if (result.AsyncState.ToString() == "saveRequest")
7 {
8 //保存游戏
9 SaveGame(storageDevice);
10 }
11 else if (result.AsyncState.ToString() == "loadRequest")
12 {
13 //加载游戏
14 LoadGame(storageDevice);
15 }
16 }
17 }
第四步,定义SaveGame,LoadGame方法:
代码
1 private void SaveGame(StorageDevice storageDevice)
2 {
3 StorageContainer container = storageDevice.OpenContainer("AlexGame");
4 string fileName = Path.Combine(container.Path, "save0001.sav");
5 FileStream saveFile = File.Open(fileName, FileMode.Create);
6 XmlSerializer xmlSerializer = new XmlSerializer(typeof(GameData));
7 XmlSerializer.Serialize(saveFile, gameData);
8 saveFile.Close();
9 }
2 {
3 StorageContainer container = storageDevice.OpenContainer("AlexGame");
4 string fileName = Path.Combine(container.Path, "save0001.sav");
5 FileStream saveFile = File.Open(fileName, FileMode.Create);
6 XmlSerializer xmlSerializer = new XmlSerializer(typeof(GameData));
7 XmlSerializer.Serialize(saveFile, gameData);
8 saveFile.Close();
9 }
代码
private void LoadGame(StorageDevice storageDevice)
{
StorageContainer container = storageDevice.OpenContainer("AlexGame");
string fileName = Path.Combine(container.Path, "save0001.sav");
if (File.Exists(fileName))
{
FileStream saveFile = File.Open(fileName, FileMode.Open);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(GameData));
gameData = (GameData)xmlSerializer.Deserialize(saveFile);
saveFile.Close();
}
}
{
StorageContainer container = storageDevice.OpenContainer("AlexGame");
string fileName = Path.Combine(container.Path, "save0001.sav");
if (File.Exists(fileName))
{
FileStream saveFile = File.Open(fileName, FileMode.Open);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(GameData));
gameData = (GameData)xmlSerializer.Deserialize(saveFile);
saveFile.Close();
}
}
运行游戏后按下键盘S键会在“文档”建立相应的游戏保存文件夹 如AlexGame