Advertisement
Guest User

Untitled

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