简体   繁体   English

如何保存游戏状态?

[英]How to save a game state?

So i am currently working on a simple game project that most people start of with "breakout". 因此,我目前正在研究一个简单的游戏项目,大多数人都从“突破”开始。 part of my goal is to make a gamesave of the current gamestate the simplest way possible. 我目标的一部分是尽可能简单地对当前游戏状态进行游戏保存。 Eg. 例如。

==scenario== ==场景==

then lets say i lose the game or i want to go to bed because i'm tired, so i want to reload the gamestate to where i come back to the same position of the paddle, ball and bricks. 然后说我输了比赛,或者因为我累了而想上床睡觉,所以我想将游戏状态重新加载到桨,球和砖头的相同位置。 the only problem is i have multiple forms and i just want to save this specific form in its specific state. 唯一的问题是我有多种形式,我只想将此特定形式保存为特定状态。

i have an idea on how to approach (i would find a way to save the position of the paddle, ball and the bricks and that exact time) just i have not a clue on how i would execute it. 我对如何处理有一个想法(我会找到一种方法来保存桨,球和砖的位置以及确切的时间),只是我对如何执行它一无所知。 How exactly can i achieve this feat? 我到底如何实现这一壮举?

This can be a very complex problem but in your case I would suggest starting out simple and going from there as your game becomes more complex. 这可能是一个非常复杂的问题,但是在您的情况下,我建议您从简单开始,并随着游戏变得越来越复杂而从那里开始。 Essentially the probablem is known as Serialization . 本质上,该问题称为序列化 Or, writing the memory of your application out to disk or a database in a way that it can be read back into memory later. 或者,将应用程序的内存写出到磁盘或数据库中,以便以后可以将其读回内存中。

There are a lot of techniques for doing this, such as converting your objects into XML or JSON or some other binary file format. 有很多技术可以做到这一点,例如将对象转换为XML或JSON或其他某种二进制文件格式。 A bitmap file for example is just a serialized form of some image structures in memory. 例如,位图文件只是内存中某些图像结构的序列化形式。

I don't really know the technology you are using to make your game other than C# (XNA?) But essentially you want to use some sort of file-system API that's available in your environment and write your serialized objects there. 除了C#(XNA?)之外,我真的不了解您用于制作游戏的技术,但从本质上讲,您想使用环境中可用的某种文件系统API并在其中编写序列化对象。 Based on your question you might need to re-design some of your game to facilitate this. 根据您的问题,您可能需要重新设计一些游戏以简化此过程。 Generally it's good practice to have a single 'source of truth' representation of your game state but even if you don't you can still probably figure it out. 通常,最好是使用单一的“真实来源”表示游戏状态,但是即使您不这样做,您也仍然可以弄清楚。

So here is a rough outline of steps I would take... 所以这是我要采取的步骤的粗略概述...

Serialization 序列化

  1. Write a routine that takes your game state and outputs simpler, serializable objects. 编写一个获取游戏状态并输出更简单的可序列化对象的例程。
    • These simple objects will mostly have all public getter/setter properties with no functionality. 这些简单的对象大部分将具有所有公共的getter / setter属性,而没有任何功能。
    • Try to write out only the bare minimum information you need to recreate your game state again later. 尝试只写出您需要的最低限度的最低信息,以便稍后再次创建游戏状态。
    • for example see how chess serializes game state: chess notation 例如,查看国际象棋如何序列化游戏状态: 国际象棋符号
  2. Serialize those objects into JSON (because its easy and flexible) 将那些对象序列化为JSON (因为它简单而灵活)
  3. Write that json to disk. 将该json写入磁盘。
    • I would probably use a convention base approach here. 我可能会在这里使用基于约定的方法。
    • such as "Saves\\game1.json" is the state for the first game slot. 例如“ Saves \\ game1.json”是第一个游戏位置的状态。
    • Just infer the saved games based on the presence of the files of the right name. 只需根据正确名称的文件来推断已保存的游戏。

Deserialization 反序列化

  1. Look for game saves based on the above convention. 根据以上约定查找游戏保存。
  2. Deserialize the JSON back into the simple serialization objects. 将JSON反序列化回简单的序列化对象。
  3. Recreate the more complex game state by looking at the serialization objects. 通过查看序列化对象来重新创建更复杂的游戏状态。
    • Consider using a visitor pattern. 考虑使用访客模式。
  4. Resume the game using the newly hydrated game state. 使用新的水合游戏状态恢复游戏。

[ EDIT : adding some extra links and a quick code sample] [ 编辑 :添加一些额外的链接和一个快速代码示例]

Here is an open source json library that is quite good: http://json.codeplex.com/ 这是一个相当不错的开源json库: http : //json.codeplex.com/

// somehow convert your game state into a serializable object graph
var saveState = gameState.Save();

// use json.net to convert that to a string
string json = JsonConvert.SerializeObject(saveState, Formatting.Indented);

// save that string to the file system using whatever is available to you.
fileSystemService.WriteAllText("Saves\game1.json", json);

您可以简单地将其保存在XML文件中,然后在启动时读取XML文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM