Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # global variable
- inv = ["Steel Sword" , "Shield" , "Health Potion" , "Strange Bun" , "Gold"]
- def room_shop(previous_room):
- global inv # to have access to external variable `inv` without passing it as function's argument
- shop = ["Health Potion" , "Boots" , "Strange Bun" , "Mysterious Crystal"]
- # room description
- print("--------------------------------------------------------")
- print("Welcome to the shop!")
- print("You can buy something or exit.")
- print("--------------------------------------------------------")
- # command loop
- while True:
- print("Below is the list of items we have in stock.")
- print(shop)
- command = input("So, what can I get for you today, traveller? ")
- command = command.strip().lower().split(1)
- print('DEBUG:', command)
- if command[0] in ("exit", "quit", "bye"):
- print("Good Bye! And come back later.")
- return previous_room
- elif command in ("inv", "inventory", "show inventory"):
- print("This is your current inventory")
- print(inv)
- elif command in ("items",):
- print("Below is the list of items we have in stock.")
- print(shop)
- elif command == "health potion":
- print("That'll be 10G, thank you!")
- inv.append("Health Potion")
- print(inv)
- elif command == "boots":
- print("That'll be 40G, thank you!")
- inv.append("Boots")
- print(inv)
- elif command == "strange bun":
- print("That'll be 5G, thank you!")
- inv.append("Strange Bun")
- print(inv)
- elif command == "mysterious crystal":
- print("That.. I don't remember having this.. Oh well, make it 100G")
- inv.append("Mysterious Crystal")
- print(inv)
- else:
- print("I clearly don't sell that, mate.")
- def room_main(previous_room):
- global inv # to have access to external variable `inv` without passing it as function's argument
- # room description
- print("--------------------------------------------------------")
- print("You are in the main room.")
- print("You can go forward, left, right, backword, shop or exit.")
- print("--------------------------------------------------------")
- # command loop
- while True:
- print("This is your current inventory")
- print(inv)
- command = input("Which commandion would you like to travel? ")
- command = command.strip().lower()
- if command in ("exit", "quit", "bye"):
- print("Bye!")
- break # exist loop
- return
- elif command == "forward":
- print("You found a Health Potion!")
- inv.append("Health Potion")
- print(inv)
- elif command == "right":
- choice = input("You encounter an enemy! What do you do? ")
- choice = choice.strip().lower()
- if choice == "steel sword":
- print("You slayed a monster! You gain 34XP and lose 5HP")
- else:
- print("You used the wrong item and the monster killed you. The end.")
- elif command == "left":
- choice = input("Somebody shoots an arrow at you! What do you do? ")
- choice = choice.strip().lower()
- if choice == "shield":
- print("You successfuly defended against the arrows, you gain 50XP")
- elif command == "backwards":
- choice = input("You walk into a trap, and lose 30HP. What do you do? ")
- if choice == "health potion":
- print("You use the Health Potion and gain 20HP.")
- elif command == "shop":
- return 'shop'
- # --- start ---
- rooms = {
- 'main': room_main,
- 'shop': room_shop,
- }
- previous_room = ''
- current_room = 'main'
- while True:
- function = rooms[current_room]
- next_room = function(previous_room)
- if next_room is None:
- break # exit `while` loop and exit game
- previous_room = current_room
- current_room = next_room
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement