Question

I have two files. myclasses.py has all the classes for my game and game.py creates all the objects and runs the game. I think I am getting the error because my GameEngine object can't see the secondRoom object. I created secondRoom in the global scope. I don't understand why secondRoom can't be seen by 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)
Was it helpful?

Solution

Short answer: global means module global.

Long answer: Actually your engine doesn't need to see the second room, as it is referenced in connects_to

Try replacing:

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']

Same thing for south.

Or more generally:

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]

Or even:

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.'

OTHER TIPS

Pass secondRoom to StartGame:

mygame.StartGame(player, firstRoom, secondRoom)

Also, variables in the StartGame method should be initialized with

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

and use them later with this.first/secondRoom.

You gave your secondRoom an empty items list.

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

Maybe you should add a None value your init method parameters:

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

    ## Class Functions
    def __init__(self, name, directions, items=None):
        pass
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top