Advertisement
Guest User

Untitled

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