繁体   English   中英

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

[英]python 3.x - text based adventure game, save game function

我正在制作一个基于文本的冒险游戏。 我大约完成了一半,在进一步了解故事情节之前,我想实现一个保存游戏功能。 我一直在努力找出整个下午的时间,但是对于我的情况,我似乎找不到任何正确方法的迹象。

这是代码中相当不错的一部分,我有很多我没有粘贴到这里的内容,但基本上只是故事节奏。 我想能够做的就是选择。 5 =保存游戏以保存玩家1的统计信息,加载后将玩家放回游戏的同一位置/ ws enter code here

由于长度原因,我无法在此处粘贴代码,因此我有一个pastebin链接。

http://pastebin.com/gJPa2TEe

非常感谢您的帮助,我正在尝试学习编程。

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

一种可行的方法是使用字典结构来存储文本,等待时间和功能(如果可用,例如战斗),例如页面。 然后使用数组存储这些页面,这将是您的书。 如果您有循环继续阅读这些页面,那么阅读将是另一项功能。 然后,由于已将故事编入索引,因此您现在知道您的位置,因此只需保存状态和位置。

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

编辑:

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都保存有其他状态。 通过使用继续,游戏将从文件中加载。

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

暂无
暂无

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

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