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的课程中注意到它)我希望在经过房间后添加许多额外的功能但我似乎无法让房间工作。
对不起信息转储,但是如果你能提供帮助那就太棒了
答案 0 :(得分:1)
你忘了实际调用这个方法。
def play(self):
current_scene = self.scene_map.start_scenes()
...