[英]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 序列化
Deserialization 反序列化
[ 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.