SHARE
TWEET

Untitled

a guest Nov 18th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. next_state = "intro"
  4.  
  5. flags_set = {}
  6. state_counter = 2
  7.  
  8. player_health = 30
  9. bare_hand = random.randrange(8, 15)
  10. flee_chances = random.randrange(1, 6)
  11. defend_damange = random.randrange(3, 5)
  12. enemy1 = 25
  13.  
  14.  
  15. def determine_flag_state(required_flags, current_flags):
  16.     for key in required_flags:
  17.         if current_flags.get(key, False) != required_flags[key]:
  18.             return False
  19.     return True
  20.  
  21.  
  22. def rpg_game():
  23.     nonlocal player_health
  24.     nonlocal enemy1
  25.     print("A monster approaches")
  26.     print("What will you do?\n"
  27.           "1)Attack\n"
  28.           "2)Defend\n"
  29.           "3)Flee")
  30.     while True:
  31.         action = input()
  32.         if action == "1":
  33.             print("You attacked.\n"
  34.                   "You dealt a total of " + str(bare_hand))
  35.             enemy1 -= bare_hand
  36.             print(enemy1)
  37.             break
  38.         elif action == "2":
  39.             print("You pulled out your shield and took less damage.")
  40.             player_health -= defend_damange
  41.             print(player_health)
  42.             continue
  43.         elif action == "3":
  44.             if flee_chances <= 3:
  45.                 print("You escaped.")
  46.             else:
  47.                 print("You couldn't escape")
  48.                 continue
  49.         print("That is not a valid answer.")
  50.  
  51.  
  52. states = {
  53.     "intro": {
  54.         "text": [
  55.             "This is the intro.",
  56.             "I hope I am able to become a programmer one day!"
  57.         ],
  58.         "choices": [
  59.             {"text": "Go to the middle", "action": "middle"},
  60.             {"text": "Go to the end", "action": "end"}
  61.         ],
  62.     },
  63.     "middle": {
  64.         "text": [
  65.             "You have reached the middle.",
  66.             "This is not the end"
  67.         ],
  68.         "jump": "extra"
  69.     },
  70.     "extra": {
  71.         "text": [
  72.             "This is a section you arrived to automatically.",
  73.             "You see a door with the word 'Exit' on top written in big red letters."
  74.         ],
  75.         "choices": [
  76.             {"text": "Open door", "action": "not end", "flag_check": {"light is on": False}},
  77.             {"text": "Open door the door", "action": "end", "flag_check": {"light is on": True}}
  78.         ]
  79.     },
  80.     "not end": {
  81.         "text": [
  82.             "You are close to the end, but it is to dark.",
  83.             "While trying to feel your way out, you find a light switch."
  84.         ],
  85.         "choices": [
  86.             {"text": "Turn on the light", "action": "light is turned on"},
  87.             {"text": "Keep walking", "action": "light was ignored"}
  88.         ]
  89.     },
  90.     "light is turned on": {
  91.         "text": [
  92.             "You turned on the light.",
  93.             "You can see the way out"
  94.         ],
  95.         "jump": "end",
  96.         "flags": {
  97.             "light is on": True
  98.         }
  99.     },
  100.     "light was ignored": {
  101.         "text": [
  102.             "You ignored the light switch.",
  103.             "You're not sure that you will ever reach the end.",
  104.             "After being lost in the darkness for to long you search the walls",
  105.             "hoping to find another light switch.",
  106.             "As you search the wall your hand gets covered in cobwebs,",
  107.             "before the spiders have a chance to come out,",
  108.             "you find another light switch!"
  109.         ],
  110.         "jump": "light is turned on"
  111.     },
  112.     "end": {
  113.         "text": [
  114.             "You have arrived at the end, but is it really over?",
  115.             "That will be for you to decide"
  116.         ],
  117.         "choices": [
  118.             {"text": "Not ready to go.", "action": "middle"},
  119.             {"text": "Pull the plug", "action": None}
  120.         ]
  121.     }
  122. }
  123.  
  124. while next_state != None:
  125.  
  126.     if state_counter <= 0:
  127.         rpg_game()
  128.         state_counter = 2
  129.     else:
  130.         state_counter -= 1
  131.  
  132.     current_state = states[next_state]
  133.     print(*current_state["text"], sep="\n")
  134.  
  135.     if "flags" in current_state:
  136.         flags_set.update(current_state["flags"])
  137.  
  138.     if "jump" in current_state:
  139.         next_state = current_state["jump"]
  140.         continue
  141.  
  142.     enabled_choices = []
  143.     counter = 1
  144.     for choice in current_state["choices"]:
  145.         if "flag_check" not in choice or determine_flag_state(choice["flag_check"], flags_set, ):
  146.             print(str(counter) + ") " + choice["text"])
  147.             counter = counter + 1
  148.             enabled_choices.append(choice)
  149.  
  150.     while True:
  151.         answer = input()
  152.         length = len(enabled_choices)
  153.         if answer.isdigit():
  154.             position = int(answer)
  155.             if 0 < position <= length:
  156.                 break
  157.         print("That is not a valid answer.")
  158.  
  159.     answer = int(answer) - 1
  160.     choice = enabled_choices[answer]
  161.  
  162.     next_state = choice["action"]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top