在基于文本的游戏中保存/加载玩家位置

时间:2018-10-18 14:56:13

标签: python python-3.x

我是一个全新的初学者,正在尝试学习如何编写python代码。我现在正在练习中,我被赋予创建自己的游戏的任务。因此,我决定制作一个带有保存和加载功能的半角色扮演游戏,该游戏记住 Player_stats和位置,该信息存储为字典。要更改键的值,每当用户输入新区域时,我都会 Player.info [“ location”] ='“水族馆”'

然后,我使用了由Zed Shaw创建的“ Engine”类 Map 类,该类使用返回值移入下一个场景,以保存我使用的播放器“ Pickle” 会在创建新文件时起作用,当我加载播放器hp时,它会显示播放器之前的状态。现在的主要问题是,当我尝试执行返回Player.info [“ location”] 以加载玩家当前位置时,我得到一个错误'NoneType'对象没有属性'enter'< / strong>。我的另一种选择是为用户位置创建一个单独的保存,但是如果可能的话,我希望将其全部保存在一个文件中。我粘贴了代码摘要,问题出在 Class Load:

class Engine:
    def __init__(self, MapScene):
        self.MapScene = MapScene

    def play(self):
        current_scene = self.MapScene.opening_scene()
        last_scene = self.MapScene.next_scene("end")

        while current_scene != last_scene:
            next_scene_name = current_scene.enter()
            current_scene = self.MapScene.next_scene(next_scene_name)

        current_scene.enter()
--------------------------------------------------
class Intro:
    def enter(self):
        print("Start\nExit\nLoad")
        prompt = input("> ")
        if prompt == "Start":
            print("Let the Adventure begin GoodLuck")
            return "aquarium"
        elif prompt == "Exit":
            print("Good Bye!")
            exit()
        elif prompt == "Load":
            return "load"
        else:
            print("Wrong Input")
class Aquarium:
    def enter(self):
        print("Welcome to aquarium 1 = damage. Type save to save file")
        while True:
            print(Player.info["hp"])
            prompt = input("> ")
            if prompt == "1":
                Player.info["hp"] -= 10
            elif prompt == "save":
                Player.info["location"] = '"aquarium"'
                return "save"
            else:
                return "intro"
--------------------------------------------------
class Save:
    def enter(self):
        save_path = "C:/Users/wo/lpthw/Pythontest/Saves"
        fileName = "Savefile"
        save_location = os.path.join(save_path, fileName + ".pkl" ) #save the files
        myFile= open(save_location, "wb")
        pickle.dump(Player.info, myFile)
        myFile.close()
        print(Player.info["location"])
        exit()
----------------------------------------------------
class Load:
    def enter(self):
        save_path = "C:/Users/wo/lpthw/Pythontest/Saves"
        fileName = "Savefile"
        save_location = os.path.join(save_path, fileName + ".pkl" )
        save_file= open(save_location, "rb") ##Load the files
        Player.info = pickle.load(save_file)#will load the player stats
        print(Player.info["hp"])
        return Player.info["location"]# <------ ERROR v
              # >> AttributeError: 'NoneType' object has no attribute 'enter'<<
class Player:
    info = {
    "hp": 100,
    "gold": 0,
    "location": '"intro"'
    }


class Map(object):
    scenes = {
    "intro": Intro(),
    "aquarium": Aquarium(),
    "load": Load(),
    "save": Save(),
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        key = Map.scenes.get(scene_name)
        return key
    def opening_scene(self):
        return self.next_scene(self.start_scene)

0 个答案:

没有答案