0

私は2つのファイルを持っています。myclasses.py にはゲームのすべてのクラスがあり、game.py はすべてのオブジェクトを作成してゲームを実行します。GameEngine オブジェクトが secondRoom オブジェクトを認識できないため、エラーが発生していると思います。グローバル スコープで secondRoom を作成しました。なぜセカンドルームが GameEngine から見えないのか理解できませんか?

## game.py
from myclasses import *
## Creating objects
smallKey = Key("Small Key")
bossKey = Key("Boss Key")
firstRoom = Room("Room", ['north', 'south'], [smallKey, bossKey])
secondRoom = Room("ROOOM 2", ['south', 'east'], [])
player = Person(raw_input("Please enter your name: "), firstRoom)
## Setting class variables
firstRoom.description = "This is the first room"
secondRoom.description = "This is the second room..." 
firstRoom.connects_to = {"north": secondRoom}
secondRoom.connects_to = {"south": firstRoom}
list_of_rooms = [firstRoom, secondRoom]  
## Running the game
mygame = GameEngine()
mygame.StartGame(player, firstRoom)

myclass.py:

from sys import exit

class Person(object):
    ## Class Variables
    inventory = []

    ## Class Functions
    def __init__(self, name, room):
        ## Instance Variables
        self.name = name
        self.current_room = room
    def Move(self, room):
        self.current_room = room
    def pickup(item):
        pass

class Item(object):
    def __init__(self, name):
        self.name = name

class Key(Item):
    def __init__(self, name):
        self.name = name


class Room(object):
    ## Class Variables
    description = ''
    connects_to = {}

    ## Class Functions
    def __init__(self, name, directions, items):
        self.name = name 
        self.list_of_directions = directions
        self.items = items
    def print_description(self):
        print self.description

    def print_items_in_room(self):
        for item in self.items:
            print item.name

class GameEngine(object):
    list_of_commands = ['move <direction>',
                        'pickup <item>',
                        'room description',
                        'show inventory',
                        'quit\n']      
    def GetCommand(self):
        return raw_input('> ')

    def StartGame(self, player, room):
        player = player
        room = room
        gameIsRunning = False
        print "The game has officially started... Good Luck!\n\n"

        while(not gameIsRunning):
            print "List of Directions:"
            print "------------------"
            for direction in room.list_of_directions:
                print direction

            print "\nList of Items in Room:"
            print "----------------------"
            for item in room.items:
                print item.name

            print "\nList of Commands:"
            print "-----------------"
            for action in self.list_of_commands:
                print action

            command = self.GetCommand()
            if command == 'quit':
                exit(0)
            if command == 'move north':
                if 'north' in room.list_of_directions and room.connects_to['north'] == secondRoom:
                    player.Move(secondRoom)
                    room = secondRoom
            if command == 'move south':
                if 'south' in room.list_of_directions and room.connects_to['south'] == firstRoom:
                    player.Move(firstRoom)
                    room = firstRoom
                else:
                    print "You can't go that direction."
            if command == 'move east':
                if 'east' in room.list_of_directions:
                    print 'You just died >:}'
                    exit(0)
            if command == 'pickup small key':
                for item in room.items:
                    print item.name
                    if item.name == 'Small Key':
                        player.inventory.append(item)
                        room.items.remove(item)
4

3 に答える 3