|
TOWARDS AN ADVENTURE SYSTEM IN PYTHON, PART I
I've been tinkering with interactive fiction (read: text adventures) lately. There's a plethora of toolkits and even languages out there to write these games. A toolkit written in Python seems to be missing, though. Would such a kit be useful? Obviously, if the player (and to a lesser extent, the developer) are required to download Python plus the toolkit, they are in for a hefty download. This is not the case when they need, for example, an AGT generated executable, or a Z-machine. Therefore, to make it worthwile for someone to use our kit, it should provide substantial benefits over (most) existing systems. I will think about, and deal with, such benefits later. Right now I just want to focus on the (rapid) implementation of an, initially rough and unpolished, adventure system. The system itself should be as small as possible, for the sake of genericity. (I'm thinking about using this same system for e.g. MUDs and maybe other ideas, like RPG/adventure crossovers.) It should have a core that can be extended for adventures, MUDs etc. Maybe there will be a related library with some typical adventure classes. While I might (gradually) build a library with "default" code for basic adventure routines (for example, getting and dropping items, inventory, container, etc) and likewise for MUDs, such code should not be part of the standard package. The library (should) exist so the adventure writer shouldn't have to start entirely from scratch; however, s/he can, if necessary, ignore this library entirely. ... The parsing and executing of commands is based on two principles:
I envision these as strings laying out a "template" for the command. For example: "get Object"Every word starting with uppercase can be the name of an existing Python class. By default, the system comes with a few base classes: Object for objects, NPC for non-player characters and creatures, etc. Entity is the superclass for all these. Normally your classes will derive from Object or NPC, although you can also introduce a whole new class of "things" and derive from Entity. Anything that does not derive from Entity is not taken into account by the template parser. So, "take Object" tells the parser, that there is a command that starts with "take", followed by an object (instance of Object). The parser can find these instances in certain places, e.g. the player's inventory, or the current room. It will not scan all objects it knows, only those that are visible. In a similar way, "talk to NPC" will be recognized as a string containing the word "talk", then "to", then an NPC. "score" on the other hand is just that-- one word, with no "variables". Questions to ponder:
Basically you write your adventure by creating classes for every object, creature, location, etc. in the game. For example, a small key may be coded like this:
class SmallKey(Object):
id = "small_key"
name = "small key" # yet to be defined...
# etc...
Now, if we have a command "get Object", we can define its effects on this small key, simply by adding a method for it. For now, we'll say that such methods start with a prefix cmd_, followed by the name of the command. In this case, it will be cmd_get:
class SmallKey(Object):
...
def cmd_get(self):
print "You take", self.the()
engine.player.add_to_inventory(self)
When cmd_get() is called, a message is printed, stating that you take the small key. (The the() method obviously returns something like "the small key", or in general, the name of the object with "the" in front of it. In the final version, this may or may not be a separate method.) The object is then added to the player's inventory.(Note that all code so far is indicative; not a single byte of code has been written so far, so things may, and most likely will, be different in the real version. These are just examples to indicate what the code can do.) Questions to ponder:
Ideally, and eventually, I want this system:
2002.06.22 0.01 |