Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Created July 8, 2013 18:58
Show Gist options
  • Save thomasballinger/5951470 to your computer and use it in GitHub Desktop.
Save thomasballinger/5951470 to your computer and use it in GitHub Desktop.
prices = {'lollipop': 200}
candies = 100
xp = 100
has_map = False
def needs_map(func):
def new_func():
if has_map:
return func()
else:
print 'you need the map'
return new_func
def do_nothing(func):
return func
def foo(x):
print x
foo = do_nothing(foo)
def get_experience_if_finish(amount):
def decorator(func):
def new_func():
global xp
finished = func()
if finished:
xp += amount
print 'you finished the quest, you get', xp, 'xp'
else:
print 'you failed the quest!'
return new_func
return decorator
@needs_map
@get_experience_if_finish(10)
def dungeon():
print 'yay fighting'
if candies > 100:
print 'yay'
return True
else:
print 'you died'
return False
#dungeon = get_experience_if_finish(10)(needs_map(dungeon))
dungeon = needs_map(get_experience_if_finish(10)(dungeon))
def final_dungeon():
if has_map:
def get_experience_version_of_dungeon():
global xp
finished = dungeon()
if finished:
xp += 10
print 'you finished the quest, you get', xp, 'xp'
else:
print 'you failed the quest!'
return get_experience_version_of_dungeon()
else:
print 'you need the map'
@needs_map
def park():
print 'it\'s very peaceful'
def check_if_have_enough(func):
def new_func():
global candies
item = func.__name__[4:]
if candies < prices[item]:
print 'you can\'t afford that'
else:
func()
return new_func
@check_if_have_enough
def buy_lollipop():
global candies
candies = candies - prices['lollipop']
print 'delicious'
buy_lollipop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment