0

我正在尝试将 elf_rescued 的值从 InterrogationRoom 类传递给 Armory 类。如果精灵被救出,那么我希望用户能够打开武器储物柜,但如果他不是,那么储物柜应该保持不变(因为那时他不会告诉你你的装备)。我对每次尝试都感到更加困惑。谁能给我一个提示我做错了什么,好吗?

我用这种方法得到的错误是:

You enter the armory, where you see a weapons locker and 3 orcs in the back of the room.
What do you do?

打开储物柜 Traceback(最近一次通话最后):文件“C:/Dropbox/Dropbox/Python/Python2/You make a game/start.py”,第 231 行,在 a_game.play() 文件“C:/Dropbox/Dropbox /Python/Python2/你制作了一个游戏/start.py”,第 47 行,正在播放 next_scene_name = current_scene.enter() 文件“C:/Dropbox/Dropbox/Python/Python2/你制作了一个游戏/start.py”,第 189 行,在操作中输入 elif "open locker" 和 elf_rescued: NameError: global name 'elf_rescued' is not defined

from sys import exit
from random import randint, randrange

class Scene(object):

    def enter(self):
        print "This scene is not yet configured. Subclass it and implement enter()."


class Engine(object):

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


    def play(self):
        current_scene = self.scene_map.opening_scene()

        while True:
            print "\n-------------------------------------------------------------------------------------------------"
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

class Death(Scene):

    deaths = [
        "You have died. GAME OVER."
        "You failed. Restart the script to try again."
    ]

    def enter(self):
        print Death.deaths[randint(0, len(self.deaths)-1)]
        exit(1)


class Prison(Scene):

    def enter(self):
        print "Wandering the Groovy Basin Woods with your elven comrade you were attacked by a pack of Orcs."
        print "Their Shaman cast a spell on you which made you unconcious."
        print "You wake up and find yourself in a prison cell, with no sign of your partner."
        print "One of the guards is sitting at a table at the end of the room."
        print "The green beast is half-drunk, half-sleeping."
        print "What do you do?"

        got_bone = False

        while True:

            action = raw_input("> ")

            if action == "search cell" and not got_bone:
                print "You start searching your cell and find a sharp object that must have " \
                    "been someone's 'Fibula'(the smaller of the two leg bones located below the knee cap) a while ago."
                print "You pick it up."
                got_bone = True

            elif action == "call guard" and not got_bone:
                print "You whistle the guard(who doesn't like your attitude and hates your race in general), " \
                    "he jumps up, opens your cell and starts beating you. Once he amused himself enough, he closes " \
                    "your cell and goes back to his chair."

            elif action == "call guard" and got_bone:
                print "The guard comes and opens your cell, hoping he can beat the living soul out of you."
                print "Your true warrior instinct does it's job and makes you stab the bone in his throat."
                print "You leave the cell and go on to the next room."
                return 'inter_room'

            elif action == 'open cell' and got_bone:
                print "You attempt to open the cell with your extra Figula, and succeed."
                print "Luckily the prison is dark enough and the orc is drunk enough for you to leave the room " \
                    "unnoticed."
                return 'inter_room'

            else:
                print "You can't do that."
                return 'prison_cell'


class InterrogationRoom(Scene):

    def enter(self):
        print "Coming out of the prison area you find yourself on a long, narrow corridor with doors on each side."
        print "Passing one of the doors you see your comrade tied up to a strappado."
        print "He is clearly in a lot of pain and by the looks of his body this isn't the first torture device " \
            "he was experimented with."
        print "Taking him with you could be risky as he isn't in the shape to move on his own."
        print "However, he is your comrade ever since you fought together in the 'War of the Seven Armies'."
        print "Will you ease his pain and make sure he won't be tortured by the orcs anymore or will you take him " \
            "with you?"


        def situation_change():
            return elf_rescued

        action = raw_input("> ")

        if "leave him" in action:
            print "As hard it is, you decide to leave him behind. You grab a dagger from the table and " \
            "push it right in his chest. You leave the room."
            return 'armory'

        elif "rescue him" in action:
            print "You cut him down from the rope. He tells you that all your gear has been taken to the Armory. " \
            "However, one of the guards has the key to open the weapon locker."
            print "You decide to go to the Guards' Quarters."
            elf_rescued = True
            situation_change()
            return 'guards_quarters'

        else:
            print "You can't do that."
            return 'inter_room'


class GuardsQuarters(Scene):

    def enter(self):
        print "You find yourself at the entrance of the Guards' Quarters."
        print "There are at least two dozen drunk orcs in this place. Some playing cards and some doing, whatever" \
            "drunk orcs do in a barack at night time."
        print "You see a key chain hanged on a nail at the other end of the room. There are some barrels around it."
        print "How do you approach it?"

        action = raw_input("> ")

        if "sneak over" in action:
            print "You start sneaking "

        elif "hide behind barrel" in action:
            print "You hide behind one of the barrels. Lucky for you a guard was just passing by and didn't" \
                "notice you."
            print "You grab the key and sneak out the room."
            return 'armory'

        else:
            print "You can't do that."
            return 'guards_quarters'


class Armory(Scene):

    def override(self):

        super(InterrogationRoom).situation_change()

    def enter(self):
        print "You enter the armory, where you see a weapons locker and 3 orcs in the back of the room."
        print "What do you do?"

        action = raw_input("> ")

        while True:

            if "attack" in action and not elf_rescued:
                print "You can't attack them without a weapon."
                return 'armory'

            elif "leave" in action:
                print "You leave and keep going ahead on the main corridor."
                return 'main_hall'

            elif "open locker" in action and elf_rescued:
                print "You open the weapons locker, where you get a nice weapon and shield."
                locker_opened = True

            elif "attack" in action and locker_opened:
                print "You attack the orc pack from behind. Cutting down two heads right away."
                print "The third one jumps away and looks angry as hell."
                print "Prepare for battle."

            else:
                print "You can't do that."
                return 'armory'



class Map(object):

    scenes = {
        'prison_cell': Prison(),
        'inter_room': InterrogationRoom(),
        'guards_quarters': GuardsQuarters(),
        'armory': Armory(),
        'main_hall': BarackMainHall(),
        '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 opening_scene(self):
        return self.next_scene(self.start_scene)

a_map = Map('prison_cell')
a_game = Engine(a_map)
a_game.play()
4

1 回答 1

0

在所有类之前,定义elf_rescued为全局变量:

    elf_rescued= False

然后,在您使用的每个方法中elf_rescued声明

    global elf_rescued

在使用它之前,尤其是分配。

我不明白你想用 method 做什么situation_change。您可能只需要设置elf_rescued= True并摆脱此方法。

于 2013-09-02T05:36:23.137 回答