Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import pickle
- import time
- class Display():
- def draw(self, game):
- raise NotImplementedError()
- def update(self, game):
- raise NotImplementedError()
- class GameDisplay(Display):
- def draw(self, game):
- print(game.get_description())
- print(game.get_options())
- def update(self, game):
- user_input = input().strip()
- switch = {
- "E":game.search,
- "I":game.inventory,
- "S":game.save_game,
- "C":game.character,
- "M":game.merchant}
- if user_input in switch:
- switch[user_input]()
- else:
- print(user_input, "is not a valid command!")
- class MerchantDisplay(Display):
- def draw(self, game):
- #print("You press the button, in an instant you are teleported to a small, cluttered room with a cash register on a glass desk\nThere appears to be a skeleton with glasses manning it.")
- if User.shop_visit==0:
- print("you can also see a battered shield and a wooden sword in the room, amongst the mess, and a machine that apparently turns slime to gold?")
- input("Press Enter to continue...")
- print(" /--------------------\ ")
- print(" / | | \ ")
- print(" / | | '-' \ ")
- print("| '-' \ ")
- print("| --------- -------- | ")
- print("|=====| o |==| o | | ")
- print("| --------- -------- | ")
- print("\ / ")
- print(" \ / ")
- print(" \ | | | | | |/ ")
- print(" \_____| | | | | | ")
- print(" |__|__|__|__|__| \n")
- print("oh. uh. hi?")
- print("I don't really know how to run this shop, so uh, just give me money and i'll try not to break reality??")
- input("Press Enter to nod awkwardly...")
- print("I'll save you some key presses and just tell you what i've got.")
- print(" /--------------------\ ")
- print(" / | | \ ")
- print(" / | | '-' \ ")
- print("| '-' \ ")
- print("| ========= ======== | ")
- print("|=====|oo |==|oo | | ")
- print("| ========- =======- | ")
- print("\ / ")
- print(" \ / ")
- print(" \ | | | | | |/ ")
- print(" \_____| | | | | | ")
- print(" |__|__|__|__|__| \n")
- print("Just got to find the stuff i'm meant to sell I guess? Give me a mo.")
- print("The skeleton goes into the back room.")
- time.sleep(1)
- print("...")
- time.sleep(1)
- print("...")
- time.sleep(1)
- print("...")
- time.sleep(1)
- print("You hear crashing from the back room.")
- time.sleep(1)
- print("...")
- time.sleep(1)
- print("...")
- time.sleep(1)
- print("You hear xylophones from the back room.")
- time.sleep(1)
- print("...")
- time.sleep(1)
- print("...")
- time.sleep(1)
- print("You hear clown horns from the back room.")
- time.sleep(1)
- print("...")
- time.sleep(1)
- print("...")
- time.sleep(1)
- print("You decide to just buy the sword and shield you can see. Both are of poor quality, but they both have price tags you can see.")
- User.shop_visit+=1
- print("You have",game.player.gold,"Gold Pieces.")
- print("1.",SwordItem.get_name(SwordItem),"(5gp)")
- print("2.",ShieldItem.get_name(ShieldItem),"(5gp)")
- print("3. Put some",SlimeItem.get_name(SlimeItem),"in the strange machine")
- print("Press B to leave the shop")
- def update(self, game):
- user_input = input().strip() #buying a sword
- if user_input == "1":
- if game.player.gold < 5:
- print("You don't have enough!")
- if game.player.gold >= 5:
- print("You pick up the sword. A voice from the back says 'You pick it up, you bought it!', so I suppose you've bought it.")
- game.player.gold-=5
- game.player.inventory.append(SwordItem())
- if user_input == "2": #buying a shield
- if game.player.gold < 5:
- print("You don't have enough!")
- if game.player.gold >= 5:
- print("You pick up the shield. A voice from the back says 'You pick it up, you bought it!', so I suppose you've bought it.")
- game.player.gold-=5
- game.player.inventory.append(ShieldItem())
- if user_input == "3": # selling slime goobly
- slimenumber=0
- item_count=0
- for item in game.player.inventory:
- if item.get_name() == "Slime Goobly": # Kinda wanted this to put all the slime goobly in at once, but eh well.
- slimenumber+=1
- item_count+=1
- i = 0
- while i < (len(game.player.inventory)): # This while loop goes through the inventory and removes anything called "slime goobly"
- if game.player.inventory[i].get_name() == "Slime Goobly":
- game.player.inventory.pop(i)
- else:
- i += 1
- if slimenumber==0:
- print("You don't have any",SlimeItem.get_name(SlimeItem),"\nWhat a shame.")
- if slimenumber>0:
- print("You put all your", SlimeItem.get_name(SlimeItem)+"s","into the funnel at the top of the strange machine and",slimenumber, "gold dropped out of the bottom!")
- game.player.gold+=slimenumber
- if user_input == "B":
- print("The skeleton pops their head out from the door in the back")
- print(" /--------------------\ ")
- print(" / \ ")
- print(" / \ ")
- print("| \ ")
- print("| --------- -------- | ")
- print("|=====| /\ |==| /\ | | ")
- print("| --------- -------- | ")
- print("\ +++ +++ / ")
- print(" \ / ")
- print(" \ | | | | | |/ ")
- print(" \_____| | | | | | ")
- print(" |__|__|__|__|__| \n")
- print("'Oh leaving already? Well, you know where to find me!'\n\n")
- game.screen= GameDisplay()
- class InventoryDisplay(Display):
- def draw(self, game):
- enumberate=1
- for item in game.player.inventory:
- print(enumberate," ", item.get_name())
- enumberate+=1
- print(game.player.gold,"gold pieces.")
- print("Press B to go Back to the main screen")
- def update(self, game):
- user_input = input().strip()
- if user_input == "B":
- game.screen= GameDisplay()
- #isinstance(user_input, int):
- else:
- choice = int(user_input)-1
- item_selection= game.player.inventory[choice]
- if item_selection.can_use():
- item_selection.use(game)
- game.player.inventory.pop(choice)
- #else: print("Invalid Command: ",user_input)
- class CharacterDisplay(Display):
- def draw(self, game):
- print("Equipped Items:")
- if len(game.player.equipped) == 0:
- print("None")
- else:
- for item in game.player.equipped:
- print(item.get_name())
- print("Health:",game.player.health,"/100")
- print("Attack:", User.calc_attack(User))
- print("Defence:", User.calc_defence(User))
- print("\nPress B to go Back\nPress I to access your Inventory")
- def update(self, game):
- user_input = input().strip()
- if user_input == "B":
- game.screen= GameDisplay()
- if user_input == "I":
- game.screen= InventoryDisplay()
- class BattleDisplay(Display):
- mob = None
- game = None
- def __init__(self, mob):
- self.mob = mob
- def attack(self):
- self.mob.defend(self.game.player)
- self.mob.attack(self.game.player)
- def run(self):
- if random.random() < 0.5:
- print("You run away.")
- self.game.screen = GameDisplay()
- else:
- self.mob.attack(self.game.player)
- print("You cannot escape!")
- def draw(self, game):
- print("Fighting", self.mob.name)
- print(self.mob.name, "health:", self.mob.health)
- print("Player Health:", game.player.health)
- print("1:", "Attack")
- print("2:", "Run")
- def update(self, game):
- self.game = game
- user_input = input().strip()
- switch = {
- "1":self.attack,
- "2":self.run
- }
- if user_input in switch:
- switch[user_input]()
- else:
- print(user_input, "is not a valid command!")
- if self.mob.health <= 0:
- self.mob.on_death(game)
- class LootDisplay(Display):
- items = []
- def __init__(self, items):
- self.items = items
- def draw(self, game):
- print("A", "Loot All")
- print("D", "Discard All")
- for i in range(len(self.items)):
- print(i, self.items[i].get_name())
- def update(self, game):
- user_input = input().strip()
- if isinstance(user_input, int):
- choice = int(user_input)
- if choice > 0 and choice < len(self.items):
- game.player.inventory.append(self.items[choice])
- self.items.pop(choice)
- else:
- if user_input == "A":
- for item in self.items:
- game.player.inventory.append(item)
- game.screen = GameDisplay()
- elif user_input == "D":
- game.screen = GameDisplay()
- class MenuDisplay(Display):
- game_ref = None
- def help(self):
- self.draw()
- def draw(self, game):
- print("----- Menu -----")
- print("| 1. New Game |")
- print("| 2. Load Game |")
- print("| 3. Help |")
- print("----------------")
- def update(self, game):
- self.game_ref = game
- user_input = input().strip()
- switch = {
- "1":game.new_game,
- "2":game.load_game,
- "3":self.help
- }
- if user_input in switch:
- switch[user_input]()
- else:
- print("Command not supported!")
- class Item():
- def can_use(self):
- return False
- def get_description(self):
- return ""
- def get_name(self):
- return ""
- def use(self, game):
- raise NotImplementedError()
- class PotionItem(Item):
- def can_use(self):
- return True
- def get_description(self):
- return "A potion of health."
- def get_name(self):
- return "Healing Potion"
- def use(self, game):
- game.player.heal(50)
- class BaconItem(Item):
- def can_use(self):
- return True
- def get_description(self):
- return "A slab of uncooked bacon"
- def get_name(self):
- return "Uncooked Bacon"
- def use(self, game):
- game.player.heal(10)
- class SlimeItem(Item):
- def can_use(self):
- return True
- def get_description(self):
- return "A nasty glob of slime."
- def get_name(self):
- return "Slime Goobly"
- def use(self, game):
- game.player.heal(-5)
- print("Now why did you eat that?")
- class EquipItem(Item):
- def can_use(self):
- return True
- def get_item_type(self):
- return "DEFAULT"
- def get_name(self):
- return "DEFAULT"
- def get_attack(self):
- return 0
- def get_defence(self):
- return 0
- def use(self, game):
- game.player.equipped.append(self)
- class SwordItem(EquipItem):
- def get_description(self):
- return "A basic sword."
- def get_name(self):
- return "Rusted Sword"
- def get_item_type(self):
- return "Sword"
- def get_attack(self):
- return 5
- def get_defence(self):
- return 1
- def use(self,game): #instead of beinbg called weapon_count just have it override Use
- game.player.equipped.append(self)
- if User.wpn_count==0:
- print("You equip the sword")
- elif User.wpn_count==1:
- print("You equip a second sword. Duel wielding heck yea!")
- elif User.wpn_count==2:
- print("You equip a... Third sword??? I guess you can use your teeth to hold a sword???")
- elif User.wpn_count==3:
- print("A fourth sword, really? Fine. Whatever.")
- elif User.wpn_count==4:
- print("Five swords? REALLY? What are you doing that needs five swords???")
- elif User.wpn_count>4:
- print("You are out of spaces for swords, so you eat it and still somehow gain the attack power.")
- User.wpn_count+=1
- #Want to do a thing where if you have more than 2 swords the narrator gets snarky
- class ShieldItem(EquipItem):
- def can_use(self):
- return True
- def get_description(self):
- return "A basic shield."
- def get_name(self):
- return "Battered Shield"
- def get_attack(self):
- return 0
- def get_defence(self):
- return 5
- class Mob():
- health = 1
- atk = 1
- defence = 1
- loot = []
- gold=0
- name=""
- def attack(self, player):
- player.health -= self.atk / player.calc_defence()
- def defend(self, player):
- self.health -= player.calc_attack() / self.defence
- def on_death(self, game):
- items = random.randint(0, 3)
- gold = random.randint(0, self.gold)
- print("You loot", gold, "gold.")
- game.player.gold += gold
- item_list = []
- for i in range(items):
- item_list.append(random.choice(self.loot))
- game.screen = LootDisplay(item_list)
- class Boar(Mob):
- def __init__(self):
- self.atk = 5
- self.health = 15
- self.gold = 10
- self.loot = [SwordItem(), BaconItem(), PotionItem()]
- self.name="Boar"
- class Slime(Mob):
- def __init__(self):
- self.atk = 2
- self.health = 10
- self.gold = 3
- self.loot = [SlimeItem()]
- self.name="Green Slime"
- class User():
- inventory=[]
- health=100
- max_health=100
- equipped=[]
- gold=0
- shop_visit=0
- wpn_count=0
- def heal(self, amount):
- self.health = min(self.health+amount, 100)
- def calc_attack(self):
- attk = 2
- for item in self.equipped:
- attk += item.get_attack()
- return attk
- def calc_defence(self):
- defence = 2
- for item in self.equipped:
- defence += item.get_defence()
- return defence
- class Game():
- player=None
- screen = None
- playing = False
- def __init__(self):
- self.screen = self.load_menu()
- self.playing = True
- self.player = User()
- def new_game(self):
- self.screen = GameDisplay()
- def load_menu(self):
- return MenuDisplay()
- def in_progress(self):
- return self.playing
- def update(self):
- self.screen.update(self)
- def draw(self):
- self.screen.draw(self)
- def search(self):
- if random.random() < 0.5:
- self.screen = BattleDisplay(Boar() if random.random()<0.5 else Slime())
- def inventory(self):
- self.screen= InventoryDisplay()
- def character(self):
- self.screen= CharacterDisplay()
- def merchant(self):
- self.screen= MerchantDisplay()
- if User.shop_visit==0:
- print("You cast 'Speedus Sellus' and in an instant you are teleported to a small, cluttered room with a cash register on a glass desk\nThere appears to be a skeleton with glasses manning it.")
- else:
- print("You cast 'Speedus Sellus' and in an instant you are teleported to a small, cluttered room with a cash register on a glass desk.\nThere are various loud noises coming from the back room.")
- def get_options(self):
- return "E: Explore\nI: Inventory\nS: Save\nC: Character\nM: Teleport to Merchant"
- def get_description(self):
- return "You are in a grassy field. What would you like to do?"
- def save_game(self):
- with open("data.save","wb+") as f:
- pickle.dump(self,f),
- with open("inventory.save","wb+") as inv:
- pickle.dump(User.inventory,inv),
- with open("equipment.save","wb+") as eqp:
- pickle.dump(User.equipped,eqp),
- with open("variables.save","wb+") as vrb:
- pickle.dump(User.shop_visit,vrb)
- print("Game Saved!")
- def load_game(self):
- with open("data.save","rb+") as f:
- loaded_game= pickle.load(f)
- self.player= loaded_game.player
- self.screen= loaded_game.screen
- self.playing= True
- with open("inventory.save","rb+") as inv:
- loaded_inv= pickle.load(inv)
- User.inventory= loaded_inv
- with open("equipment.save","rb+") as eqp:
- loaded_eqp= pickle.load(eqp)
- User.equipped= loaded_eqp
- with open("variables.save","rb+") as vrb:
- loaded_vrb= pickle.load(vrb)
- User.shop_visit= loaded_vrb
- print("Game Loaded!")
- if __name__ == "__main__":
- game = Game()
- while game.in_progress():
- game.draw()
- game.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement