Advertisement
furas

Python - RPG game

May 20th, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.15 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # global variable
  4. inv = ["Steel Sword" , "Shield" , "Health Potion" , "Strange Bun" , "Gold"]
  5.  
  6. def room_shop(previous_room):
  7.     global inv # to have access to external variable `inv` without passing it as function's argument
  8.  
  9.     shop = ["Health Potion" , "Boots" , "Strange Bun" , "Mysterious Crystal"]
  10.  
  11.     # room description
  12.     print("--------------------------------------------------------")
  13.     print("Welcome to the shop!")
  14.     print("You can buy something or exit.")
  15.     print("--------------------------------------------------------")
  16.            
  17.     # command loop
  18.     while True:
  19.         print("Below is the list of items we have in stock.")
  20.         print(shop)
  21.  
  22.         command = input("So, what can I get for you today, traveller? ")
  23.         command = command.strip().lower().split(1)
  24.  
  25.         print('DEBUG:', command)
  26.        
  27.         if command[0] in ("exit", "quit", "bye"):
  28.             print("Good Bye! And come back later.")
  29.             return previous_room
  30.         elif command in ("inv", "inventory", "show inventory"):
  31.             print("This is your current inventory")
  32.             print(inv)
  33.         elif command in ("items",):
  34.             print("Below is the list of items we have in stock.")
  35.             print(shop)
  36.         elif command == "health potion":
  37.             print("That'll be 10G, thank you!")
  38.             inv.append("Health Potion")
  39.             print(inv)
  40.         elif command == "boots":
  41.             print("That'll be 40G, thank you!")
  42.             inv.append("Boots")
  43.             print(inv)
  44.         elif command == "strange bun":
  45.             print("That'll be 5G, thank you!")
  46.             inv.append("Strange Bun")
  47.             print(inv)
  48.         elif command == "mysterious crystal":
  49.             print("That.. I don't remember having this.. Oh well, make it 100G")
  50.             inv.append("Mysterious Crystal")
  51.             print(inv)
  52.         else:
  53.             print("I clearly don't sell that, mate.")
  54.    
  55. def room_main(previous_room):
  56.     global inv # to have access to external variable `inv` without passing it as function's argument
  57.  
  58.     # room description
  59.     print("--------------------------------------------------------")
  60.     print("You are in the main room.")
  61.     print("You can go forward, left, right, backword, shop or exit.")
  62.     print("--------------------------------------------------------")
  63.  
  64.     # command loop
  65.     while True:
  66.         print("This is your current inventory")
  67.         print(inv)
  68.    
  69.         command = input("Which commandion would you like to travel? ")
  70.         command = command.strip().lower()
  71.  
  72.         if command in ("exit", "quit", "bye"):
  73.             print("Bye!")
  74.             break # exist loop
  75.             return
  76.         elif command == "forward":
  77.             print("You found a Health Potion!")
  78.             inv.append("Health Potion")
  79.             print(inv)
  80.         elif command == "right":
  81.             choice = input("You encounter an enemy! What do you do? ")
  82.             choice = choice.strip().lower()
  83.             if choice == "steel sword":
  84.                 print("You slayed a monster! You gain 34XP and lose 5HP")
  85.             else:
  86.                 print("You used the wrong item and the monster killed you. The end.")
  87.         elif command == "left":
  88.             choice = input("Somebody shoots an arrow at you! What do you do? ")
  89.             choice = choice.strip().lower()
  90.             if choice == "shield":
  91.                 print("You successfuly defended against the arrows, you gain 50XP")
  92.         elif command == "backwards":
  93.             choice = input("You walk into a trap, and lose 30HP. What do you do? ")
  94.             if choice == "health potion":
  95.                 print("You use the Health Potion and gain 20HP.")
  96.         elif command == "shop":
  97.             return 'shop'
  98.  
  99. # --- start ---
  100.  
  101. rooms = {
  102.     'main': room_main,
  103.     'shop': room_shop,
  104. }
  105.  
  106. previous_room = ''
  107. current_room = 'main'
  108.  
  109. while True:
  110.     function = rooms[current_room]
  111.     next_room = function(previous_room)
  112.     if next_room is None:
  113.         break # exit `while` loop and exit game
  114.     previous_room = current_room
  115.     current_room = next_room
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement