Advertisement
FranzVuttke

text_adventure_v2_decorators_6_44.py

Nov 28th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | Source Code | 0 0
  1.  
  2. import random
  3. import functools
  4. from time import sleep
  5.  
  6. character = "Sir Bob"
  7. health = 14
  8. xp = 0
  9.  
  10. def character_action(func):
  11.     @functools.wraps(func)
  12.     def wrapper(*args, **kwargs):
  13.         if health <= 0:
  14.             print(f"{character} is too weak.")
  15.             return
  16.         result = func(*args, **kwargs)
  17.         print(f"    Health: {health} | XP: {xp}")
  18.         return result
  19.     return wrapper
  20.    
  21. @character_action
  22. def eat_food(food):
  23.     global health
  24.     print(f"{character} ate {food}.")
  25.     health += 1
  26.    
  27. @character_action
  28. def fight_monster(monster, strength):
  29.     global health, xp
  30.     if random.randint(1, 20) >= strength:
  31.         print(f"{character} defeated {monster}.")
  32.         xp += 10
  33.     else:
  34.         print(f"{character} flees from {monster}.")
  35.         health -= 10
  36.         xp += 5
  37.  
  38. def pause(how_long):
  39.     for i in range(3):
  40.         print(".", end="", flush=True)
  41.  
  42.         sleep(how_long)
  43.     print()
  44.  
  45. print("Eating...")
  46. pause(1)
  47. eat_food("bread")
  48.  
  49. print("Fight with Imp")
  50. pause(1)
  51. fight_monster("Imp", 15)
  52.  
  53. print("Fight with Direwolf")
  54. pause(1)
  55. fight_monster("Direwolf",15)
  56.  
  57. print("Fight with Minotaur")
  58. pause(1)
  59. fight_monster("Minotaur",19)
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement