Advertisement
Guest User

Untitled

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