简体   繁体   English

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

[英]How can I go about creating a saving system for a text based adventure game in python?

I'm new to programming and was trying to create a text based adventure game.我是编程新手,正在尝试创建一个基于文本的冒险游戏。 I want to implement a saving system where players are able to save anytime during the game, and continue the story from where they left off.我想实现一个保存系统,玩家可以在游戏过程中随时保存,并从他们离开的地方继续故事。 This would also have to save important relevant variables.这还必须保存重要的相关变量。 I have thought about creating a save function like this:我曾想过像这样创建一个保存 function :

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

This would work for variables that will exist throughout the story permanently, like gold, health etc, but this doesn't work for progress through the story.这适用于整个故事中永久存在的变量,例如黄金、健康等,但这不适用于故事的进展。 If I want the player to be able to continue the story where they left off how could I do that?如果我希望玩家能够继续他们中断的故事,我该怎么做? Thankyou!谢谢!

By progress through the story I mean something like this,:通过故事的进展,我的意思是这样的:

    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%\Desktop\test\race.dat','wb'))
    print("Saved!")
    return
#checking if a save file already exists
race_exist=os.path.isfile(r'C:\Users\%username%\Desktop\test\race.dat')
#if the save file exists, start from here
if race_exist==True:
    race=pickle.load(open(r'C:\Users\%username%\Desktop\test\race.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%\Desktop\test\race.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()

here progress throughout the story is like, them coming to crossroads, deciding where to go etc, For example, if the player decides to go left, then saves the game, When he comes back I want him to be able to continue the story from where he left off (going left on the crossroads) rather than have to start all the way back choosing his race and direction again.这里整个故事的进展就像,他们走到了十字路口,决定去哪里 go 等等,例如,如果玩家决定离开 go ,然后保存游戏,当他回来时我希望他能够继续故事他离开的地方(在十字路口向左走),而不是不得不一直回到选择他的种族和方向。 Thank you!谢谢!

A dictionary would be a good thing to use.使用字典将是一件好事。

You can create one like so.你可以像这样创建一个。

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

Next, check the variable for the data.接下来,检查数据的变量。

        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")

Then, update the event if the value changes.然后,如果值更改,则更新事件。

events["first_direction"] = direction

There are many types of data you could store this way, for example.例如,您可以通过这种方式存储多种类型的数据。

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

Make sure to save this variable.确保保存此变量。

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

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