2015-09-25 102 views
0

错误:第29行,在地图'empty_room'中: 我尝试了重写我的类和其他小东西,但一直未能拿出任何东西解决方案。我的缩进在记事本++上是正确的(或者他们看起来),他们只是没有很好地转移到SOF。任何疑难解答赞赏:)谢谢!错误:EmptyRoom(),NameError:名称'EmptyRoom'未定义

P.S.我正在用“Learn Python the Hard Way”一书自学,我正在做一个类似于zork的游戏。希望这可以帮助。

from sys import exit 

class Scene(object): 
def enter(self): 
    print "This scene is not configured" 
    exit(1) 


class Engine(object): 

##calling Engine(x) x is the mapScene 
def __init__(self, mapScene): 
    self.mapScene = mapScene 

def play(self): 
    currentScene = self.mapScene.openingScene() 

    while True: 

     print "\n-------------------" 
     nextSceneName = currentScene.enter() 
     currentScene = self.mapScene.nextScene(nextSceneName) 

class Map(object): 

scenes = { 
    'empty_room': EmptyRoom(), 
    'living_room': LivingRoom(), 
    'office': Office(), 
    'hallway': Hallway(), 
    'kitchen': Kitchen(), 
    'master_bedroom': MasterBedroom(), 
    'kids_bedroom': KidsBedroom(), 
    'attic': Attic() 
    } 

##when calling Map(x) x is the startscene 
def __init__(self, startScene): 
    self.startScene = startScene 


    ##When calling nextScene(x) x is the sceneName 
def nextScene(self, sceneName): 
    return Map.scenes.get(sceneName) 


    ##???? 
def openingScene(self): 
    return self.nextScene(self.startScene) 



class EmptyRoom(Scene): 
def enter(self): 
    print "" 

    action = raw_input("> ") 

    if action == "open door": 
     return 'living_room' 



class LivingRoom(Scene): 
def enter(self): 
    print "" 

    action = raw_input("> ") 

    if action == "kitchen": 
     return 'kitchen' 

    elif action == "stairs" or "go upstairs": 
     print "The steps creek as you ascend to the unknown..." 
     print "Are you sure you want to go up here?" 
     action = raw_input("> ") 

     if action == "yes" or "kinda": 
      return 'hallway' 

     else: 
      return 'living_room' 

    elif action == "empty room": 
     return 'empty_room' 


    else: 
     return 'living_room' 



class Kitchen(Scene): 
def enter(self): 
    print "" 

    action = raw_input("> ") 

    if action == "office" or "go right": 
     return 'office' 


    elif action == "living room": 
     return 'living_room' 

    else: 
     return 'kitchen' 


class Office(Scene): 
def enter(self): 
    print "" 

    action = raw_input("> ") 

    if action == "kitchen": 
     return 'kitchen' 



class MasterBedroom(Scene): 
def enter(self): 
    print "" 

    action = raw_input("> ") 

    if action == "hallway": 
     return 'hallway' 


class KidsBedroom(Scene): 
def enter(self): 
    print "" 

    action = raw_input("> ") 

    if action == "hallway": 
     return 'hallway' 


class Hallway(Scene): 
def enter(self): 
    print "" 

    action = raw_input("> ") 

    if action == "downstairs" or "stairs" or "living room": 
     return 'living_room' 

    elif action == "bedroom" or "master bedroom" or "left": 
     return 'master_bedroom' 

    elif action == "kids room" or "kids bedroom" or "right": 
     return 'kids_bedroom' 

    elif action == "pull string": 
     print"You have just opened the attic staircase would you like to go up?" 
     action = raw_input("> ") 

     if action == "yes": 
      return 'attic' 

     elif action == "no" or "nope": 
      return 'hallway' 

     else: 
      print "I wouldn't have went either\n" 
      print "SMASH, the attic door springs shut\n" 
      return 'hallway' 

    else: 
     return 'hallway' 


class Attic(Scene): 
def enter(self): 
    print "" 

    action = raw_input("> ") 

    if action == "downstairs" or "hallway": 
     return 'hallway' 

aMap = Map('empty_room') 
aGame = Engine(aMap) 
aGame.play() 
+0

名称在它们存在之前不存在。 –

回答

1

类定义在使用之前应先到达。

class EmptyRoom(Scene): 
    def enter(self): 
     print "" 
     action = raw_input("> ") 
     if action == "open door": 
      return 'living_room' 

class Map(object): 
    scenes = { 
     'empty_room': EmptyRoom(), 
     ... 
    } 

同为LivingRoomOfficeHallwayKitchenMasterBedroomKidsBedroomAttic

0

Map里面的scenes的定义是在构建类时运行的,而不是当你稍后调用该类来实例化它时。那时,EmptyRoom还不存在 - Python从代码顶部开始工作,所以EmptyRoom只有在class EmptyRoom:下面的缩进块结束后才存在。所以为了这个工作,Map需要在之后而不是之前的所有其他类。

+0

谢谢!我觉得很愚蠢,我不知道!再次感谢!! – Dramabeatz82