[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Text-Based RPG



Dave LeCompte (really) wrote:
"Jonah Fishel" <globalparadox@xxxxxxxxxxx> was asking:

Could I make a Room class that has functions based on different
variables and then make a separate class for each room like
AnthroTavern(Room) so I could base it on the rom class and use the
functions for the room class?

So, the answer to "Could I make a class..." is "yes". A good next question
would be "would I want to?", and in this case I think the answer is "no".

You certainly could make a Tavern Room subclass of the Room class, but it
probably makes more sense to make a generic Location class, and then use
data (loaded from text files like I think you're already doing, or loading
from a database, or... any number of other data-driven solutions) to give
it the specific characteristics that you want (Dark Room, Tavern, Cave, In
Front of House, Flood Control Gate #8).


Dave LeCompte is right. This is my take on the issue:

What's different about the tavern versus a regular room? Most likely it's just some variables that you might as well put into a single Room class. For instance, if a room can be light or dark, you could give the Room class a "light" variable by putting in the __init__ function:
self.light = options.get("light",True) ## Which makes True the default value

One way to manage movement between rooms would be to have a RoomManager class that has a database of rooms, each with an index, so that each room can have a variable like "exits" that gives indexes of each room that it leads to. I'm not sure whether it's better programming practice to have these exits be the rooms' ID numbers or actual references:
exits = {} ## Create a dictionary
tavern = Room(name="Anthro Tavern",ID=42) ## Make a room
## Method 1:
exits["north"] = tavern.ID
exits.get("north") ## returns the number 42
## Method 2:
exits["north"] = tavern
exits.get("north") ## Returns the actual Room

I was playing with the idea of doing a text adventure earlier... actually at several points. Here's the code for the most recent experiment with that; you're welcome to copy/steal/use any of it, though it's poorly commented and maybe not well-designed.
<http://kschnee.xepher.net/code/ifdoodle.py.txt>