0

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_util.py", line 7
6, in exec_file
    exec(code_obj, global_variables)
  File "c:\users\tim\documents\visual studio 2013\Projects\Text Game\Text Game\T
ext_Game.py", line 57, in <module>
    a_game.play()
  File "c:\users\tim\documents\visual studio 2013\Projects\Text Game\Text Game\T
ext_Game.py", line 12, in play
    next_scene_name = current_scene.enter() #prints scene's info
AttributeError: 'function' object has no attribute 'enter'

这是我的代码(用 Python 2.7 编写):

    from sys import exit
from random import randint

class Engine(object):
    def __init__(self,scene_map):
        self.scene_map = scene_map
    def play(self):
        current_scene = self.scene_map.start_scenes
        while True: #this is always running until Death, this pulls next scene up
            print "\n--------"
            next_scene_name = current_scene.enter() #prints scene's info
            current_scene = self.scene_map.next_scene() #this has to take result of scene to play next scene

class Player(object): #once rest of code works we implement more moplicated player traits.
    pass

class Scene(object):
    def enter(self):
        print "Scene info"
        exit(1)

class Death(Scene):
    quips = [
             "Wow. Much Death, so sad. Wow.",
             "Hah you suck at this!",
             "Try Again!",
             ]
    def enter(self):
        print Death.quips[randint(0, len(self.quips)-1)]
        exit(1)

class Room1(Scene):
    def enter(self):
        print "Scene Intro"
        choice_i = raw_input("\n >")
        choice=choice_i.lower()
        if choice == "left" or choice == "l":
            return "room10"
        elif choice == "right" or choice == "r":
            return "room2"
        else:
            print "Choose left or right (l/r)"
            return "Room1" #Engine or Map will take the room names to restart a room (or death or finish to end game)

class Map(object):
     scenes = {"room1": Room1(), "death": Death()}
     def __init__(self, start_scene):
        self.start_scene = start_scene
     def next_scene(self, scene_name):
        return Map.scenes.get(scene_name)
     def start_scenes(self):
        return self.next_scene(self.start_scene)

a_map = Map("Room1")
a_game = Engine(a_map)
a_game.play()

--------

我是初学者,课程相对容易让我感到困惑。我正在尝试制作一个基于文本的游戏(正如您在 learnpythonthehardway 的课程中​​可能注意到的那样),我想在通过房间后添加许多额外的功能,但我似乎无法让房间正常工作。

很抱歉信息转储,但如果你能提供帮助,那就太棒了

.

4

1 回答 1

1

您忘记了实际调用该方法。

def play(self):
    current_scene = self.scene_map.start_scenes()
     ...
于 2013-11-07T06:36:39.400 回答