在Python中访问类的变量时遇到的问题

时间:2015-12-19 16:36:03

标签: python python-2.7

这里是新学员。我尝试了here指示的方法,但没有成功。你能帮我解决下面的代码吗?我似乎无法让Escape Pod类访问Central Corridor类中的gothonlife变量。继续收到此错误消息“类型对象'CentralCorridor'没有属性'gothonlife'?谢谢!

class Scene(object):

    def enter(self):
        pass


class Engine(object):

    def __init__(self, scene_map):
        print "On the floor you find a map with the following options. \n 1) Central Corridor \n 2) Laser Weapon Armory \n 3) The Bridge \n 4) Escape Pod. \n Where do you want to go?"
    door = raw_input("> ")

    count = 0
    while (count < 1):

        if door == "Central Corridor":
            count = count + 1
            object1 = CentralCorridor()
            object1.enter()

        elif door == "Laser Weapon Armory":
            count = count + 1

        elif door == "The Bridge":
            count = count + 1

        elif door == "Escape Pod":
            count = count + 1
            object5 = EscapePod()
            object5.enter()

        else:
            print "Erm... where exactly do you want to go again?"
            door = raw_input("> ")

class Death(Scene):

    def enter(self):
        print "You die, thanks for playing. Hope you had fun."

class CentralCorridor(Scene):

    def enter(self):
        print "There stands a Gothon. What do you want to do with him? \n 1) Offensive attack \n 2) Defensive attack \n 3) Suicide"

        gothonlife = 10
        herolife = 10

        def __init__(self):
            self.gothonlife = gothonlife

    while (gothonlife > 0 and herolife > 0):
        action = raw_input("What now? Offensive attack, defensive attack or suicide?")

        if action == "Offensive attack":
            gothonlife = gothonlife -3
            herolife = herolife -2      
            print "your life stands at %s, the enemy life be at %s" % (herolife, gothonlife)

        elif action == "Defensive attack":
            gothonlife = gothonlife -2
            herolife = herolife -0  
            print "your life stands at %s, the enemy life be at %s" % (herolife, gothonlife)

        elif action == "Suicide":
            herolife = herolife -11
            print "your life stands at %s, the enemy life be at %s" % (herolife, gothonlife)

        else:
            print "Erm... where exactly do you want to go again?"

    if herolife <0:
        object2 = Death()
        object2.enter()
    elif herolife >0:
        print "You defeated the Gothon, where do you want to go next?"
        object3 = EscapePod()
        object3.enter()

class LaserWeaponArmory(Scene):

    def enter(self):
        pass

class TheBridge(Scene):

    def enter(self):
        pass

class EscapePod(CentralCorridor):

    def enter(self):
        if CentralCorridor.gothonlife > 0:
            print "A gothon stands guarding the escape pod."
            object4 = CentralCorridor()
            object4.enter()
        elif CentralCorridor.gothonlife < 0:
            print "You escape from Gothon! Congrats!"



class Map(object):

    def __init__(self, start_scene):
        print "You are at the entrance, there is a Gothon in front of you"

    def next_scene(self, scene_name):
        pass

    def opening_scene(self):
        pass


a_map = Map('central_corridor')
a_game = Engine(a_map)

0 个答案:

没有答案