保存并加载 .bat 游戏

发布于 2024-12-08 14:04:34 字数 303 浏览 1 评论 0原文

我正在制作一个用bat编写的文本游戏,并且游戏已经完成,(或者更多,其中的很大一部分,例如命令,以及可以玩它的阶段);但是,我想添加保存游戏并再次加载的功能。

我认为可以通过让 .bat 文件写入需要保存的变量(例如项目变量)来做到这一点;但是,我不知道该怎么做。任何帮助将不胜感激,谢谢。

我应该说,我可以通过使用以下方式加载它:

 for /f "delims=" %%x in (config.txt) do (set "%%x")

但是,我不知道如何使 .bat 写入文件并“保存”。

I'm making a text game written in bat, and the game has been done, (or more, a good part of it, such as the commands, and at the stage where you can play it); however, I want to add the power to save your game, and load it again.

I think one could do this by having the .bat file write the variables which need to be saved (such as the item variables); however, I don't know how to do this. Any help would be appreciated, thanks.

I should have said, I can make it load, by use of:

 for /f "delims=" %%x in (config.txt) do (set "%%x")

However, I don't know how to make the .bat write to the file and so "save".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情定在深秋 2024-12-15 14:04:34

您还可以仅保存/加载值,例如

(
  echo %highscore%
  echo %playername%
  echo %points%
) > savegame.sav

使用

< savegame.sav (
  set /p highscore=
  set /p playername=
  set /p points=
)

“第一部分”将“echo”输出重定向到文件。
加载部分还使用文件重定向,但在本例中作为输入源。
块中的set /p命令可以从文件中连续读取行。

You could also save/load with only values, like

(
  echo %highscore%
  echo %playername%
  echo %points%
) > savegame.sav

and load them with

< savegame.sav (
  set /p highscore=
  set /p playername=
  set /p points=
)

The first part simply redirects the echo outputs to a file.
The loading part using also file redirection, but in this case as input source.
set /p commands in a block can read consecutively lines from the file.

余生共白头 2024-12-15 14:04:34

尝试这样的操作:

@echo @ECHO OFF           > savegame.cmd
@echo SET ITEMS=%ITEMS%   >> savegame.cmd
@echo SET HEALTH=%HEALTH% >> savegame.cmd
@echo SET MONEY=%MONEY%   >> savegame.cmd

将这三个变量“保存”在 savegame.cmd 中。然后您可以调用该文件来重新加载变量。

(使用 for /f 来实现这一点相当棘手。)

Try something like this:

@echo @ECHO OFF           > savegame.cmd
@echo SET ITEMS=%ITEMS%   >> savegame.cmd
@echo SET HEALTH=%HEALTH% >> savegame.cmd
@echo SET MONEY=%MONEY%   >> savegame.cmd

will "save" those three variables in savegame.cmd. Then you can call that file to reload the variables.

(Doing it with a for /f is quite a bit trickier.)

趴在窗边数星星i 2024-12-15 14:04:34

或者


@回声关闭

> savegame.bat echo。设置项目=%ITEMS%

>> savegame.bat echo。设置健康=%HEALTH%

>> savegame.bat echo。SET MONEY=​​%MONEY%

>> savegame.bat echo。设置级别=%LEVEL%


粗体字是bat文件中要写入的变量。

“savegame.bat”是您放置变量将保存到的文件名的位置。

最后是>箭头表示它将删除所有内容并从文件的开头开始,从>>>开始。箭头告诉它将该行添加到文件最底部所有文字的最后。

我希望这有帮助,也可以帮助有相同问题的其他人:D

Or


@echo off

> savegame.bat echo.SET ITEMS=%ITEMS%

>> savegame.bat echo.SET HEALTH=%HEALTH%

>> savegame.bat echo.SET MONEY=%MONEY%

>> savegame.bat echo.SET LEVEL=%LEVEL%


The Words in Bold Are The Variables to be Written on the bat file.

The "savegame.bat" is where you will put the file's name that the Variables will be saved to.

And lastly the > Arrow mean that it will delete everything and start from the beginning of the file as of the >> arrow is telling it to add that line to the very end of all the writing at the very bottom of the file.

I Hope This Helps, as-well as Help anyone else who has the same question :D

暮年 2024-12-15 14:04:34

这是我目前正在做的一项工作。

您可以使用 INI 文件来保存游戏,并使用 dir 命令加载它们。

这就是我加载保存游戏列表的方法。
这 ”。”分隔符是为了不显示 .ini 扩展名,以免混淆用户。然而,这不允许用户在保存游戏名称中添加句点。

set randomDirIndex=%random%dirindex
dir /b savegames\>%temp%\%randomDirIndex%
for /f "delims=." %%a in (%temp%\%randomDirIndex%) do (
    echo %%a
)

这是 savegame INI 文件的示例:

[PlayerStats]
health=100
energy=75
mana=50
[Inventory]
sword=1
key=0
[Info]
name=John Doe

我还使用了这个问题(检查第一个答案)获取加载 INI 脚本。

现在要加载它,您将使用一系列 for /f 命令:

for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini PlayerStats health') do (
    set health=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini PlayerStats energy') do (
    set energy=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini PlayerStats mana') do (
    set mana=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini Inventory sword') do (
    set hasSword=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini Inventory key') do (
    set hasKey=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini Info name') do (
    set playerName=%%a
)

最后,要保存游戏,您只需将所有内容回显到文件中,本质上是重写 INI 文件。 (参考其他人的回答。)

Here's one that I am actually working on currently.

You use INI files for the savegames, and load them with a dir command.

This is how I went about loading a list of savegames in.
The "." delimiter is so that it will not show the .ini extension, as not to confuse users. This however does not allow users to put periods in the savegame names.

set randomDirIndex=%random%dirindex
dir /b savegames\>%temp%\%randomDirIndex%
for /f "delims=." %%a in (%temp%\%randomDirIndex%) do (
    echo %%a
)

Here's an example of the savegame INI file:

[PlayerStats]
health=100
energy=75
mana=50
[Inventory]
sword=1
key=0
[Info]
name=John Doe

I also used this question (check the first answer) to get the load INI script.

Now to load it you would use a series of for /f commands:

for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini PlayerStats health') do (
    set health=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini PlayerStats energy') do (
    set energy=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini PlayerStats mana') do (
    set mana=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini Inventory sword') do (
    set hasSword=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini Inventory key') do (
    set hasKey=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini Info name') do (
    set playerName=%%a
)

And finally to save the game you just have to echo all of the things into a file, essentially rewriting the INI file. (refer to other people's answers.)

怪我闹别瞎闹 2024-12-15 14:04:34

我曾经在 DOS 5 时代写过一个批处理游戏(类似文本冒险)。然后,我选择将游戏中的每个位置设为磁盘上的单独文件夹。每个位置都有自己的 bat 文件来初始化该位置,例如显示其描述并初始化可以从该位置移动到哪些位置。

物体和人都有自己的 bat 文件。因此,当与一个人交谈时,该人的bat文件需要处理该问题,并保存对话信息。拾取对象就像将 object.bat 从位置目录移动到清单目录一样简单。

这样,我不仅可以轻松地将位置、物体和人物添加到游戏中,而且大部分信息都会自动保存,因为状态保存在文件内或文件的位置中。其余部分是通过将相关值保存到在加载时运行的批处理文件来保存的,其方式与 Mat 在他的回答中描述的方式类似。

我必须说,当时我不知道所有 for 循环技巧和字符串操作的可能性,如果它们甚至可以在 MSDOS 5 中实现,否则它可能会更容易一点,更不用说更快了。 ;)

I've once written a batch game (text-adventure like) back in DOS 5 era. I've then chosen to make each location in the game a separate folder on disk. Each location had its own bat file to initialize the location, like showing its description and to initialize which locations could be moved to from there.

Objects and persons had their own bat file. So when talking to a person, the bat file of that person needed to handle that, and save conversation info. Picking up an object was as simple as moving object.bat from the location directory to the inventory directory.

That way, not only could I easily add locations, objects and persons to the game, but also most of the information was saved automatically, because the status was kept inside files, or in the location of files. The rest was saved by saving relevant values to a batch file that was run on load, in a similar way as Mat described in his answer.

I must say, I didn't know then of all the for loop trickery and string manipulation possibilities, if they even were possible MSDOS 5, otherwise it could have been a little easier, not to mention faster. ;)

内心旳酸楚 2024-12-15 14:04:34

试试这个:

 (echo items=%items%) >> gamesave.txt
 (echo money=%money%) >> gamesave.txt

等等。

Try this:

 (echo items=%items%) >> gamesave.txt
 (echo money=%money%) >> gamesave.txt

etc.

郁金香雨 2024-12-15 14:04:34

你可以尝试这个:

创建一个变量

set /p savegame=

,然后创建此代码,

echo %savegame% > save.txt

然后你可以打开文件,输入任何内容,它将另存为 save.txt ;)

you can try this :

make a variable

set /p savegame=

then make this code

echo %savegame% > save.txt

then you can open the file,type anything and it will save as save.txt ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文