0

我有两个文件。myclasses.py 包含我的游戏的所有类,game.py 创建所有对象并运行游戏。我想我收到了错误,因为我的 GameEngine 对象看不到 secondRoom 对象。我在全局范围内创建了 secondRoom。我不明白为什么 GameEngine 看不到 secondRoom?

## 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)

我的类.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

0

简短的回答:全局意味着模块全局。

长答案:实际上你的引擎不需要看到第二个房间,因为它在connects_to

尝试更换:

if command == 'move north':
    if 'north' in room.list_of_directions and 'north' in room.connects_to:
        player.Move(room.connects_to['north'] )
        room = room.connects_to['north']

南方也一样。

或更一般地说:

if command.startswith ('move '):
    direction = command [4:].strip ()
    if direction in room.list_of_directions and direction in room.connects_to:
        player.Move(room.connects_to[direction] )
        room = room.connects_to[direction]

甚至:

if command.startswith ('move '):
    direction = command [4:].strip ()
    if direction in room.list_of_directions:
        if direction in room.connects_to:
            player.Move(room.connects_to[direction] )
            room = room.connects_to[direction]
        else:
            print 'You fell off the world' #(valid direction but no room behind)
    else:
        print 'Thou shalst not move this way.'
于 2013-08-16T20:00:39.897 回答
0

将 secondRoom 传递给 StartGame:

mygame.StartGame(player, firstRoom, secondRoom)

此外,StartGame 方法中的变量应该用

this.player = player
this.firstRoom = firstRoom
this.secondRoom = secondRoom
this.gameIsRunning = False

稍后将它们与this.first/secondRoom.

于 2013-08-16T19:58:01.513 回答
-1

你给你的secondRoom一个空的项目列表。

secondRoom = Room("ROOOM 2", ['south', 'east'], [])

也许你应该在你的init方法参数中添加一个None值:

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

    ## Class Functions
    def __init__(self, name, directions, items=None):
        pass
于 2013-08-16T19:56:22.047 回答