Name: Anonymous 2011-08-20 19:09
I'm thinking about writing a text adventure similar to Colossal Cave Adventure and Rogue. It'll probably take place in a mansion or something where all the rooms are randomly generated and populated with predefined and dynamically generated items and furniture, the properties of which will determine how the player can interact with them, and then have generic functions to handle the interaction.
I experimented with classes and inheritance before settling on a simple data structure:
This simple script defines a room, the objects within, and simple function that explores the room and describes it in plain English:
in the bedroom you find: bed, desk
on the bed you find: bedding
under the bed you find: shoe box
in the shoe box you find: desk key
on the desk you find: desk lamp
in the desk you find: Wizard Book
in the Wizard Book you find: Satori
Writing functions to parse user commands and handle interaction with the game world should be trivial. Then the game can be expanded simply by defining rooms, object, NPCs, enemies, etc.
I experimented with classes and inheritance before settling on a simple data structure:
#!/usr/bin/env python
# items
desk_key = {
'name': 'desk key',
}
shoe_box = {
'name': 'shoe box',
'in': [
desk_key,
],
}
desk_lamp = {
'name': 'desk lamp',
}
wizard_book = {
'name': 'Wizard Book',
'in': [
{
'name': 'Satori',
},
],
}
# furniture
bed = {
'name': 'bed',
'on': [
{'name': 'bedding'},
],
'under': [
shoe_box,
],
}
desk = {
'name': 'desk',
'is_closed': True,
'is_locked': True,
'locks_with': desk_key,
'on': [
desk_lamp,
],
'in': [
wizard_book,
],
}
# rooms
bedroom = {
'name': 'bedroom',
'in': [
bed,
desk,
],
}
# funcs
def examine(thing):
for position in ['on', 'in', 'under']:
if position in thing:
names = [other['name'] for other in thing[position]]
print '%s the %s you find: %s' % (position, thing['name'],
', '.join(names))
for other in thing[position]:
examine(other)
if __name__ == '__main__':
for room in [bedroom]:
examine(room)This simple script defines a room, the objects within, and simple function that explores the room and describes it in plain English:
in the bedroom you find: bed, desk
on the bed you find: bedding
under the bed you find: shoe box
in the shoe box you find: desk key
on the desk you find: desk lamp
in the desk you find: Wizard Book
in the Wizard Book you find: Satori
Writing functions to parse user commands and handle interaction with the game world should be trivial. Then the game can be expanded simply by defining rooms, object, NPCs, enemies, etc.