[Tutor] More class questions

Paul McGuire paul at alanweberassociates.com
Sat Sep 8 11:31:38 CEST 2007


Ara -

I found your question about the Pyparsing-based adventure game that I wrote.
You can find more info on this from the presentation I made at PyCon'06,
(http://www.python.org/pycon/2006/papers/4/).  This link opens up at the
title page, there are navigation controls in the lower right corner of the
screen if you move your mouse over them.

This program uses the following classes:
- Room
- Item
- Player
- Command

The Room and Item instances are created during the game setup.  Each Room
contains pointers to neighboring rooms to the north, south, east, and west.
Rooms also have a list of items that are currently in the room.  As the game
ensues, items can be picked up and dropped, so this list will change over
time.  I guess you could change the room connections also - wouldn't be hard
- perhaps as a result of using a special Item while in the Room.

Items are fairly passive, free-standing objects, containing some attributes,
and a possible useAction.  They don't have much behavior, they don't know
what room they are in, they can be picked up, dropped, and used, and they
have a name that describes them when you look in a room, or list your
player's inventory.

Player is the "status" object of the game.  Player has an inventory of
Items, and has a reference to the Room the player is currently in.  I think
an easy mistake when writing a game is to make the Player status and
attributes global variables.  This will work okay, but by keeping this info
in an object, the game could easily extend to having multiple players, just
by adding a second instance, and adding support for the players to take
turns giving commands.

Command is the class that actually makes things happen.  Command itself is
an "abstract" class, that defines the basic form of what different commands
can do, and how they are created.  There are several subclasses of Command:
- TakeCommand
- DropCommand
- InventoryCommand
- UseCommand
- LookCommand
- DoorsCommand
- MoveCommand
- HelpCommand
- QuitCommand

Commands are created based on the input that the game player types in at the
game prompt (this is where pyparsing comes in).  The pyparsing grammar
parses the input string, and if it is a valid command, the grammar's parse
actions create the appropriate Command subclass.  For instance, typing in
"help" will create a HelpCommand instance.  Typing in "take shovel" will
create a TakeCommand, with the target object of "shovel".  After the Command
is created, it is executed against the Player object.  The results of the
Command can:
- have the Player take something from the current Room
- have the Player drop something in his inventory, and leave it in the
current Room
- list the Player's inventory
- etc.
The MoveCommand will move the player to an adjoining room.

To tie it all together, the game engine runs in a basic loop:

    # create a player, let's call him Bob
    player = Player("Bob")

    # give Bob the sword for protection
    player.take( Item.items["sword"] )

    # read commands, and then invoke them on Bob (and his surroundings)
    while not player.gameOver:
        cmdstr = raw_input(">> ")
        cmd = parser.parseCmd(cmdstr)
        if cmd is not None:
            cmd.command( player )

And that's it.  All of the logic about the moving from room to room is
captured in the N,S,E,W references between Room objects.  Moving Bob from
room to room is done by MoveCommands, as they are dynamically created based
on user input.  

I hope that gives you a little more idea of how the pyparsing adventure game
works.

-- Paul



More information about the Tutor mailing list