Advertisement
Guest User

Untitled

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