您如何制作保存游戏文件? [英] how do you make a save game file?

查看:136
本文介绍了您如何制作保存游戏文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个a子手游戏,玩家在游戏中用自己的话来创造自己的关卡.完成后,他们可以创建某种游戏文件来保存他们的游戏.这样,他们就可以将文件提供给朋友,并且可以将其加载到计算机上(他们需要游戏本身的副本才能运行文件).

除此之外,这些是我的问题,

1.如何保存游戏文件?

2.如何将这些文件加载​​到我的游戏中并运行它?


预先感谢.

Hi, I want to make a hangman game where the player creates their own levels with their own words. When they are finished they can create some sort of game file that saves their game. That way they can give the file to their friends and they can load it on their computer(they will need a copy of the game itself to run the file).

Other then that these are my questions,

1. how do you make a save game file?

2. how do I load these files to my game and run it?


Thanks in advance.

推荐答案

:笑:

这是一个比您想的要难解决的问题...:D

很难回答的原因是保存游戏(或与此相关的任何文件数据)都不尽相同.有的是纯文本,有的是XML,有的是二进制,有的是数据库,还有的是完全专有的格式,使用其中的每个位!因此,很难说这是代码,它可以保存您的游戏",因为它实际上取决于您认为保存游戏文件中的重要内容:您是否想让任何希望在记事本中打开它的人都可以读取它?还是您想对它进行加密,以使黑客很难玩您的游戏?两种类型(以及两者之间的所有内容)都已用于保存游戏.

保存子手游戏的最简单方法可能是使用带有两行文本的纯文本文件:
:laugh:

That''s a harder question to answer than you think... :D

The reason it''s hard to answer is that save games (or any file data for that matter) are not all the same. Some are straight text, others are XML, some are binary, others are databases, yet others are completely proprietary formats that use bits of each of these! So it''s hard to say "Here is the code, it saves your game" because it really depends on what you feel is important in a save game file: do you want it readable by anyone who cares to open it in Notepad? Or do you want it encrypted so that it is hard for hackers to play with your game? Both types (and everything inbetween) have been used for save games.

The simplest way to save a hangman game is probably as a straight text file with two lines of text:
MATCHWORD
GUESSESOFAR

因此,如果我正在玩"CROSSFIRE"一词,而我猜到了"A","R","S"和"P",则您的保存文件将是: br/>

So if I was playing with the word "CROSSFIRE" and I had guessed ''A'', ''R'', ''S'' and ''P'' your save file would be:

CROSSFIRE
ARSP

生成文件很容易:

To generate the file is easy:

string matchWord = "CROSSFIRE";
string triedLetters = "ARSP";
File.WriteAllText(@"D:\Temp\mySaveFile.SAV", string.Format("{0}\n{1}", matchWord, triedLetters));

读起来也很容易:

To read it back is pretty easy too:

string[] lines = File.ReadAllLines(@"D:\Temp\mySaveFile.SAV");
if (lines.Length == 2)
   {
   matchWord = lines[0];
   foreach (char c in lines[1])
      {
      Guess(c);
      }
   }



[edit]我忘了一个括号:O-OriginalGriff [/edit]



[edit]I forgot a close bracket :O - OriginalGriff[/edit]


这是一个非常广泛的问题,但我会尽力指出正确的方向:

1)找出需要保存的值.如果可以用代码生成它,则不必担心(例如,如果要保存有关矩形的信息,则无需保存高度,宽度和面积这三者,因为其中任何一个都可以由其他两个决定)
2)确定格式.最简单的方法是制作一个文本文件,每行一个值,或逗号/制表符/等.分开. INI文件使用的格式也非常简单,并且易于手动读取以检查错误.由于标记了此C#,因此XML也是一个不错的选择(请查看 LINQ to XML [ ^ ]).
3)写入文件.创建文件的在线记录很好,如果您不知道怎么做,可以进行快速Google搜索.

加载只是读取值(必要时进行翻译/解析)并将它们分配到正确的位置.
This is a very broad question, but I''ll try to point you in the right direction:

1) Figure out what values need to be saved. If it can be generated in code don''t worry about it (e.g. if you want to save information about a rectangle, you don''t need to save all three of height, width, and area, because any one of those can be determined from the other two)
2) Decide a format. The easiest thing to do is to make a text file, one value on each line, or comma/tab/etc. separated. The format used by INI files is also fairly simple, and easy to manually read to check for errors. Since you tagged this C#, XML is also a good choice (check out LINQ to XML[^]).
3) Write the file. Creating files is well documented online, do a quick Google search if you don''t know how.

Loading is just reading in the values (translating/parsing as necessary) and assigning them to the proper places.




尚未考虑此案,我不是游戏开发者.
但是您必须保存所有设置/状态,例如达到哪个级别以及可能在哪个级别中,播放器的健康状况,也许您已获得的项目(简称):所有与您不同的事物起点.
我会使用BinaryReader/Writer将其另存为.DAT文件或用于存储/加载信息的文件.

祝你好运,应该不难实现它;)

此致
Hi,

didn''t had to think about this case yet, I''m not a game developer.
But you surly have to save all settings/states like which level you are reached and maybe in which section of the level, the health of your player, maybe items you''ve earned, in short form: all things which are different to your starting point.
I would use a BinaryReader/Writer to save it as a .DAT file or something to store/load information.

Good Luck, it shouldn''t be to hard to achieve it ;)

Best Regards


这篇关于您如何制作保存游戏文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆