python 3.x - 基于文本的冒险游戏,保存游戏功能

时间:2014-10-04 01:30:38

标签: python python-3.x

我正在制作基于文本的冒险游戏。我差不多完成了一半,在我对故事情节进行更深入的研究之前,我想实现一个保存游戏功能。我一直试图在整个下午弄明白,但我似乎无法找到任何关于如何正确行事的指示,无论如何我的情景。

这是代码的一个不错的部分,我有更多,我没有粘贴在这里,但它只是故事基本上起搏。我想要做的是有一个选择即。 5 =保存游戏,保存玩家1的统计数据,加载时将玩家放回游戏中的同一位置/ {s enter code here

由于长度的原因,我无法将代码粘贴到此处,所以我有一个pastebin链接。

http://pastebin.com/gJPa2TEe

非常感谢任何帮助,我正在努力学习编程。

2 个答案:

答案 0 :(得分:0)

你似乎有一些定义状态的东西:self .__ player_health,self .__ player_name,self .__ health_potion,self .__ big_wrench,self .__ blue_keycard。我需要仔细阅读,但找不到定义进展的状态。使用定义状态的所有内容,您可以保存并从文本文件中读取 - 如果您的目标是简单。

一种方法是使用dict结构来存储文本,等待时间和功能(如果可用,如战斗),如页面。然后使用数组来存储这些页面,这将是你的书。如果你有一个循环来继续阅读这些页面 - 阅读将是另一个功能。然后,由于故事被索引,你现在知道你在哪里,所以只需保存状态和位置。

无关:只需在顶部使用导入而不是每次都使用导入。

编辑:

import sys, traceback
import json

userQuited = False

def _quit(dummy):
    global userQuited
    userQuited = True

def _save(dummy):
    f=open("game.sav",'w+')
    json.dump(states, f)
    f.close

def _continue(dummy):
    f=open("game.sav",'r+')   
    states = json.load(f)
    f.close

def user(name):
    states["player_name"] = name

states = {
    "player_health" : 100,
    "player_name" : "",
    "health_potion" : 0,
    "big_wrench" : 0,
    "blue_keycard" : 0,
    "current_page" : "page1"
}

book = { 
    "page1": {
        "text": """You are in an underwater research facility that has been
attacked by unknown assailants. As the assistant to the head
researcher, you were hired in to assist Dr. Weathers with
the Lightning Sword project. Project Lightning Sword still
remains in large a mystery to you. Although you worked closely
with the doctor you were never allowed to see the big picture.
Right now that's the least of your worries, as chaos ensues around you.
Alarms were triggered a while ago. As you sit in your room,
you hear short bursts of automatic rifle fire and only one
thing is for certain, you have to survive.\n """,
        "inputText": "What would you like your player to be named?",
        "function": user,
        "next": "page2"
    },
    "page2": {
        "text": "Welcome, %(player_name)s please choose what you would like to do",
        "inputText": "0 = Quit, 1 = Start New Game, 2 = Continue",
        "choices": {
            "0" : "Quit",
            "1" : "page3",
            "2" : "Continue"
        }
    },
    "page3" : {
        "text" : """You are standing in your room, door is locked and you're wondering
if you will get out of this alive. The gunfire keeps getting closer and you know that you
can't stay here, you have to make a move and try and get out. Otherwise it's just a matter
of time before they find you and that option doesn't look very promising.\n\n

You are ready to try your escape but first you should try and get any useful
things from your room to use along the way. Your room-office is a bit bigger than a walk in closet,
textile flooring and gray walls encompas your view and the only things in the room are the bed you
are sitting on (not very comfortable, looks more like a prisoners bed), a framed picture on the wall
and your work desk which has barely enough space for your equipment and computer.""",
        "inputText": "1 = Walk over to the picture frame, 2 = Walk over to the desk, 3 = Exit the door",
        "choices": {
            "1" : "page4",
            "2" : "page5",
            "3" : "page7"
        }
    },

    "Quit" : {
        "text": "goodbye!",
        "inputText": "",
        "function" : _quit
    },
    "Continue" : {
        "text": "welcome back!",
        "inputText": "",
        "function" : _continue
    }
}



def processPage(page):
    answer = ""
    print(page["text"] % states)
    if len(page["inputText"]) > 1 :
        answer = raw_input(page["inputText"] % states)
    if "function" in page:
        page["function"](answer)
    if "next" in page:
        return page["next"]
    if "choices" in page:
        for choice in page["choices"]:
            if choice == answer:
                print page["choices"][choice]
                return page["choices"][choice]
    return ""

def main():
    global userQuited
    while(userQuited==False):
        states["current_page"] = processPage(book[states["current_page"] ])
        if (states["current_page"] != "Quit" and states["current_page"] != "Continue"):
            _save("")

main()

所以很多代码都在重复......我试图将它保留在一个模式中。这里的想法是在您的故事中打印和导航的循环。对于每次迭代,current_page都与其他状态一起保存。通过使用继续,游戏将从文件中加载。

其余的是我根据您的代码模式制作的小型文本游戏引擎。修改并在其上创建。

答案 1 :(得分:0)

我找到了最好的方法:加密它然后让它问你以前是否玩过。如果有,请将它们粘贴到保存代码中以对其进行解密。如果你需要一个例子我有一个,请问