public
anonymous / gist:4473757
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
## C O M M A N D S ##
'''
This could be neat, because you could store the command list as a constant somewhere that is populated during run-time with all the commands that are contained inside a particular module. ie. Put each command inside its own file with a list of synonyms as one of the variables, then dynamically import it and populate the COMMAND_LIST dictionary with its synonyms and function. Just a fun thought. :D
'''
 
COMMAND_LIST = {
'give': give,
'provide': give,
'yield': give,
}
 
def error_cmd(*args):
raise ValueError('Not a valid command. Valid commands include: {0}'.format(COMMAND_LIST.keys()))
 
def give(*args): # just take in anything and manually process it
# Example:
# something = 'money', to = 'to', someone = 'vendor'
something = None
someone = None
try:
something, to, someone = args
if to != 'to':
raise ValueError
except ValueError:
raise ValueError('Invalid Syntax: give <something> to <someone>')
 
# perform the action based on the arguments
player.remove_from_inventory(something)
vendor.place_in_inventory(something)
 
 
def take(item, *args): # Mix it up when the aruments are optional
# Example:
# take scroll; item = 'scroll', from = 'from', someone = 'vagrant'
# take scroll from vagrant
 
# do a thing...
pass
 
 
def eat(item): # Be strict
# Example:
# item = 'apple'
 
# do a thing...
pass
## / C O M M A N D S ##
 
## R O O M S ##
'''
Perhaps store the functions of the game in a yaml file to (a) reduce the amount of classes with the same name [Tile] and (b) make maintainability a bit easier. Different user's additions could be stored in different yaml files that described the dungeon in a human-readable way.
'''
 
rooms:
- coords: 0,0
desc: 'You are surrounded by blah blah blah.'
leave: 'You exit the blah blah blah.'
object: money
action: take
object: fountain
action: drink, bathe
mob: vendor
action: list, buy, blah...
- coords: 0,1
desc: ...
leave: ...
...
## / R O O M S ##
 
## H A N D L E R ##
'''
This would more or less be the same as what you've already got in place. :D
'''
 
class Handler(do):
# Examples:
# give money to vendor
# take scroll
# eat apple
# construct the action, subject, preposition, and object
args = do.split() # ['give', 'money', 'to', 'vendor']
action = do.pop(0) # action = 'give', args = ['money', 'to', 'vendor']
 
# process action
try:
COMMAND_LIST.get(action, error_cmd)(*args)
except InGameException as e: # something expected, like not having the item
print e
except ValueError as e:
print e
 
## / H A N D L E R ##

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.