我该如何在python中为基于文本的冒险游戏创建一个保存系统



我是编程新手,曾试图创建一款基于文本的冒险游戏。我想实现一个保存系统,玩家可以在游戏中随时保存,并从他们停止的地方继续故事。这也必须保存重要的相关变量。我曾考虑过创建一个类似这样的保存函数:

def save():
pickle.dump(race,open(r'C:Users%username%Desktoptestrace.dat','wb'))
print("Saved!")
return

这将适用于整个故事中永久存在的变量,如黄金、健康等,但这对整个故事的进展不起作用。如果我想让球员能够继续他们中断的故事,我该怎么做?谢谢!

我所说的故事进展是指这样的事情:

import pickle
import os
#defining save, add any variables to this that you want to save following the same format
def save():
pickle.dump(race,open(r'C:Users%username%Desktoptestrace.dat','wb'))
print("Saved!")
return
#checking if a save file already exists
race_exist=os.path.isfile(r'C:Users%username%Desktoptestrace.dat')
#if the save file exists, start from here
if race_exist==True:
race=pickle.load(open(r'C:Users%username%Desktoptestrace.dat','rb'))
if race=='human':
direction=input("You arrive at a crossroads during your travel, would you like to go left or right")
if direction=='save':
save()
else:print('someone stabbed you because theyre racist.')
#if save file does not exist, start from here:
elif race_exist==False:
race=input("Are you an orc or human?")
pickle.dump(race,open(r'C:Users%username%Desktoptestrace.dat','wb'))
if race=='orc':
print("People dislike orcs, you get stabbed during your sleep and die!")
elif race=='human':
direction=input("You arrive at a crossroads during your travel, would you like to go left or right")
if direction=='save':
save()

在这里,整个故事的进展就像是,他们来到十字路口,决定去哪里等等。例如,如果玩家决定向左走,然后挽救了比赛,当他回来时,我希望他能够从他离开的地方继续故事(在十字路口向左走(,而不是必须一路返回,重新选择他的比赛和方向。非常感谢。

使用字典是件好事。

你可以创建一个这样的。

events = {
"first_crossing": false // choice made at first crossing
}

接下来,检查变量中的数据。

direction = events["first_crossing"]
if !direction: # if direction has not been chosen
direction=input("You arrive at a crossroads during your travel, would you like to go left or right")

然后,如果值发生更改,则更新事件。

events["first_direction"] = direction

例如,有许多类型的数据可以通过这种方式存储。

events = {
"chapter1": {
"completed_tutorial1": false,
"completed_event1": fasle,
...
},
...
}

请确保保存此变量。

最新更新