Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.05 KB | None | 0 0
  1. import random
  2. import pickle
  3. import time
  4. class Display():
  5.  
  6. def draw(self, game):
  7. raise NotImplementedError()
  8.  
  9. def update(self, game):
  10. raise NotImplementedError()
  11.  
  12. class GameDisplay(Display):
  13. def draw(self, game):
  14. print(game.get_description())
  15. print(game.get_options())
  16.  
  17. def update(self, game):
  18. user_input = input().strip()
  19.  
  20. switch = {
  21. "E":game.search,
  22. "I":game.inventory,
  23. "S":game.save_game,
  24. "C":game.character,
  25. "M":game.merchant}
  26.  
  27. if user_input in switch:
  28. switch[user_input]()
  29. else:
  30. print(user_input, "is not a valid command!")
  31.  
  32. class MerchantDisplay(Display):
  33.  
  34. def draw(self, game):
  35. #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.")
  36.  
  37. if User.shop_visit==0:
  38. 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?")
  39. input("Press Enter to continue...")
  40.  
  41. print(" /--------------------\ ")
  42. print(" / | | \ ")
  43. print(" / | | '-' \ ")
  44. print("| '-' \ ")
  45. print("| --------- -------- | ")
  46. print("|=====| o |==| o | | ")
  47. print("| --------- -------- | ")
  48. print("\ / ")
  49. print(" \ / ")
  50. print(" \ | | | | | |/ ")
  51. print(" \_____| | | | | | ")
  52. print(" |__|__|__|__|__| \n")
  53. print("oh. uh. hi?")
  54. 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??")
  55. input("Press Enter to nod awkwardly...")
  56. print("I'll save you some key presses and just tell you what i've got.")
  57. print(" /--------------------\ ")
  58. print(" / | | \ ")
  59. print(" / | | '-' \ ")
  60. print("| '-' \ ")
  61. print("| ========= ======== | ")
  62. print("|=====|oo |==|oo | | ")
  63. print("| ========- =======- | ")
  64. print("\ / ")
  65. print(" \ / ")
  66. print(" \ | | | | | |/ ")
  67. print(" \_____| | | | | | ")
  68. print(" |__|__|__|__|__| \n")
  69. print("Just got to find the stuff i'm meant to sell I guess? Give me a mo.")
  70. print("The skeleton goes into the back room.")
  71. time.sleep(1)
  72. print("...")
  73. time.sleep(1)
  74. print("...")
  75. time.sleep(1)
  76. print("...")
  77. time.sleep(1)
  78. print("You hear crashing from the back room.")
  79. time.sleep(1)
  80. print("...")
  81. time.sleep(1)
  82. print("...")
  83. time.sleep(1)
  84. print("You hear xylophones from the back room.")
  85. time.sleep(1)
  86. print("...")
  87. time.sleep(1)
  88. print("...")
  89. time.sleep(1)
  90. print("You hear clown horns from the back room.")
  91. time.sleep(1)
  92. print("...")
  93. time.sleep(1)
  94. print("...")
  95. time.sleep(1)
  96. 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.")
  97. User.shop_visit+=1
  98.  
  99. print("You have",game.player.gold,"Gold Pieces.")
  100. print("1.",SwordItem.get_name(SwordItem),"(5gp)")
  101. print("2.",ShieldItem.get_name(ShieldItem),"(5gp)")
  102. print("3. Put some",SlimeItem.get_name(SlimeItem),"in the strange machine")
  103. print("Press B to leave the shop")
  104. def update(self, game):
  105. user_input = input().strip() #buying a sword
  106. if user_input == "1":
  107. if game.player.gold < 5:
  108. print("You don't have enough!")
  109. if game.player.gold >= 5:
  110. 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.")
  111. game.player.gold-=5
  112. game.player.inventory.append(SwordItem())
  113. if user_input == "2": #buying a shield
  114. if game.player.gold < 5:
  115. print("You don't have enough!")
  116. if game.player.gold >= 5:
  117. 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.")
  118. game.player.gold-=5
  119. game.player.inventory.append(ShieldItem())
  120. if user_input == "3": # selling slime goobly
  121. slimenumber=0
  122. item_count=0
  123.  
  124. for item in game.player.inventory:
  125.  
  126. if item.get_name() == "Slime Goobly": # Kinda wanted this to put all the slime goobly in at once, but eh well.
  127. slimenumber+=1
  128. item_count+=1
  129. i = 0
  130. while i < (len(game.player.inventory)): # This while loop goes through the inventory and removes anything called "slime goobly"
  131. if game.player.inventory[i].get_name() == "Slime Goobly":
  132. game.player.inventory.pop(i)
  133. else:
  134. i += 1
  135. if slimenumber==0:
  136. print("You don't have any",SlimeItem.get_name(SlimeItem),"\nWhat a shame.")
  137. if slimenumber>0:
  138. 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!")
  139. game.player.gold+=slimenumber
  140.  
  141. if user_input == "B":
  142. print("The skeleton pops their head out from the door in the back")
  143. print(" /--------------------\ ")
  144. print(" / \ ")
  145. print(" / \ ")
  146. print("| \ ")
  147. print("| --------- -------- | ")
  148. print("|=====| /\ |==| /\ | | ")
  149. print("| --------- -------- | ")
  150. print("\ +++ +++ / ")
  151. print(" \ / ")
  152. print(" \ | | | | | |/ ")
  153. print(" \_____| | | | | | ")
  154. print(" |__|__|__|__|__| \n")
  155. print("'Oh leaving already? Well, you know where to find me!'\n\n")
  156. game.screen= GameDisplay()
  157.  
  158.  
  159.  
  160.  
  161.  
  162. class InventoryDisplay(Display):
  163. def draw(self, game):
  164. enumberate=1
  165. for item in game.player.inventory:
  166. print(enumberate," ", item.get_name())
  167. enumberate+=1
  168. print(game.player.gold,"gold pieces.")
  169.  
  170. print("Press B to go Back to the main screen")
  171. def update(self, game):
  172. user_input = input().strip()
  173. if user_input == "B":
  174. game.screen= GameDisplay()
  175. #isinstance(user_input, int):
  176. else:
  177. choice = int(user_input)-1
  178. item_selection= game.player.inventory[choice]
  179. if item_selection.can_use():
  180. item_selection.use(game)
  181. game.player.inventory.pop(choice)
  182. #else: print("Invalid Command: ",user_input)
  183.  
  184.  
  185. class CharacterDisplay(Display):
  186. def draw(self, game):
  187. print("Equipped Items:")
  188. if len(game.player.equipped) == 0:
  189. print("None")
  190. else:
  191. for item in game.player.equipped:
  192. print(item.get_name())
  193.  
  194.  
  195. print("Health:",game.player.health,"/100")
  196. print("Attack:", User.calc_attack(User))
  197. print("Defence:", User.calc_defence(User))
  198. print("\nPress B to go Back\nPress I to access your Inventory")
  199. def update(self, game):
  200. user_input = input().strip()
  201. if user_input == "B":
  202. game.screen= GameDisplay()
  203. if user_input == "I":
  204. game.screen= InventoryDisplay()
  205.  
  206.  
  207.  
  208. class BattleDisplay(Display):
  209.  
  210. mob = None
  211. game = None
  212.  
  213. def __init__(self, mob):
  214. self.mob = mob
  215.  
  216. def attack(self):
  217. self.mob.defend(self.game.player)
  218. self.mob.attack(self.game.player)
  219.  
  220. def run(self):
  221. if random.random() < 0.5:
  222. print("You run away.")
  223. self.game.screen = GameDisplay()
  224. else:
  225. self.mob.attack(self.game.player)
  226. print("You cannot escape!")
  227.  
  228.  
  229.  
  230. def draw(self, game):
  231. print("Fighting", self.mob.name)
  232. print(self.mob.name, "health:", self.mob.health)
  233. print("Player Health:", game.player.health)
  234. print("1:", "Attack")
  235. print("2:", "Run")
  236.  
  237. def update(self, game):
  238. self.game = game
  239. user_input = input().strip()
  240.  
  241. switch = {
  242. "1":self.attack,
  243. "2":self.run
  244. }
  245.  
  246. if user_input in switch:
  247. switch[user_input]()
  248. else:
  249. print(user_input, "is not a valid command!")
  250.  
  251. if self.mob.health <= 0:
  252. self.mob.on_death(game)
  253.  
  254. class LootDisplay(Display):
  255.  
  256. items = []
  257.  
  258. def __init__(self, items):
  259. self.items = items
  260.  
  261. def draw(self, game):
  262.  
  263. print("A", "Loot All")
  264. print("D", "Discard All")
  265.  
  266. for i in range(len(self.items)):
  267. print(i, self.items[i].get_name())
  268.  
  269. def update(self, game):
  270. user_input = input().strip()
  271.  
  272. if isinstance(user_input, int):
  273. choice = int(user_input)
  274.  
  275. if choice > 0 and choice < len(self.items):
  276. game.player.inventory.append(self.items[choice])
  277. self.items.pop(choice)
  278. else:
  279. if user_input == "A":
  280. for item in self.items:
  281. game.player.inventory.append(item)
  282. game.screen = GameDisplay()
  283. elif user_input == "D":
  284. game.screen = GameDisplay()
  285.  
  286.  
  287.  
  288. class MenuDisplay(Display):
  289.  
  290. game_ref = None
  291. def help(self):
  292. self.draw()
  293.  
  294. def draw(self, game):
  295. print("----- Menu -----")
  296. print("| 1. New Game |")
  297. print("| 2. Load Game |")
  298. print("| 3. Help |")
  299. print("----------------")
  300.  
  301. def update(self, game):
  302. self.game_ref = game
  303. user_input = input().strip()
  304.  
  305. switch = {
  306. "1":game.new_game,
  307. "2":game.load_game,
  308. "3":self.help
  309. }
  310.  
  311. if user_input in switch:
  312. switch[user_input]()
  313. else:
  314. print("Command not supported!")
  315.  
  316. class Item():
  317. def can_use(self):
  318. return False
  319.  
  320. def get_description(self):
  321. return ""
  322.  
  323. def get_name(self):
  324. return ""
  325.  
  326. def use(self, game):
  327. raise NotImplementedError()
  328.  
  329. class PotionItem(Item):
  330. def can_use(self):
  331. return True
  332.  
  333. def get_description(self):
  334. return "A potion of health."
  335.  
  336. def get_name(self):
  337. return "Healing Potion"
  338.  
  339. def use(self, game):
  340. game.player.heal(50)
  341.  
  342. class BaconItem(Item):
  343. def can_use(self):
  344. return True
  345.  
  346. def get_description(self):
  347. return "A slab of uncooked bacon"
  348.  
  349. def get_name(self):
  350. return "Uncooked Bacon"
  351.  
  352. def use(self, game):
  353. game.player.heal(10)
  354.  
  355.  
  356. class SlimeItem(Item):
  357. def can_use(self):
  358. return True
  359.  
  360. def get_description(self):
  361. return "A nasty glob of slime."
  362.  
  363. def get_name(self):
  364. return "Slime Goobly"
  365.  
  366. def use(self, game):
  367. game.player.heal(-5)
  368. print("Now why did you eat that?")
  369.  
  370. class EquipItem(Item):
  371.  
  372. def can_use(self):
  373. return True
  374.  
  375. def get_item_type(self):
  376. return "DEFAULT"
  377.  
  378. def get_name(self):
  379. return "DEFAULT"
  380.  
  381. def get_attack(self):
  382. return 0
  383.  
  384. def get_defence(self):
  385. return 0
  386.  
  387. def use(self, game):
  388. game.player.equipped.append(self)
  389.  
  390. class SwordItem(EquipItem):
  391.  
  392. def get_description(self):
  393. return "A basic sword."
  394.  
  395. def get_name(self):
  396. return "Rusted Sword"
  397.  
  398. def get_item_type(self):
  399. return "Sword"
  400.  
  401. def get_attack(self):
  402. return 5
  403.  
  404. def get_defence(self):
  405. return 1
  406.  
  407. def use(self,game): #instead of beinbg called weapon_count just have it override Use
  408. game.player.equipped.append(self)
  409. if User.wpn_count==0:
  410. print("You equip the sword")
  411. elif User.wpn_count==1:
  412. print("You equip a second sword. Duel wielding heck yea!")
  413. elif User.wpn_count==2:
  414. print("You equip a... Third sword??? I guess you can use your teeth to hold a sword???")
  415. elif User.wpn_count==3:
  416. print("A fourth sword, really? Fine. Whatever.")
  417. elif User.wpn_count==4:
  418. print("Five swords? REALLY? What are you doing that needs five swords???")
  419. elif User.wpn_count>4:
  420. print("You are out of spaces for swords, so you eat it and still somehow gain the attack power.")
  421. User.wpn_count+=1
  422.  
  423.  
  424.  
  425. #Want to do a thing where if you have more than 2 swords the narrator gets snarky
  426.  
  427.  
  428. class ShieldItem(EquipItem):
  429. def can_use(self):
  430. return True
  431.  
  432. def get_description(self):
  433. return "A basic shield."
  434.  
  435. def get_name(self):
  436. return "Battered Shield"
  437.  
  438. def get_attack(self):
  439. return 0
  440.  
  441. def get_defence(self):
  442. return 5
  443.  
  444. class Mob():
  445. health = 1
  446. atk = 1
  447. defence = 1
  448. loot = []
  449. gold=0
  450. name=""
  451.  
  452. def attack(self, player):
  453. player.health -= self.atk / player.calc_defence()
  454.  
  455. def defend(self, player):
  456. self.health -= player.calc_attack() / self.defence
  457.  
  458. def on_death(self, game):
  459. items = random.randint(0, 3)
  460. gold = random.randint(0, self.gold)
  461.  
  462. print("You loot", gold, "gold.")
  463. game.player.gold += gold
  464. item_list = []
  465. for i in range(items):
  466. item_list.append(random.choice(self.loot))
  467.  
  468. game.screen = LootDisplay(item_list)
  469.  
  470. class Boar(Mob):
  471.  
  472. def __init__(self):
  473. self.atk = 5
  474. self.health = 15
  475. self.gold = 10
  476. self.loot = [SwordItem(), BaconItem(), PotionItem()]
  477. self.name="Boar"
  478.  
  479. class Slime(Mob):
  480.  
  481. def __init__(self):
  482. self.atk = 2
  483. self.health = 10
  484. self.gold = 3
  485. self.loot = [SlimeItem()]
  486. self.name="Green Slime"
  487.  
  488.  
  489. class User():
  490. inventory=[]
  491. health=100
  492. max_health=100
  493. equipped=[]
  494. gold=0
  495. shop_visit=0
  496. wpn_count=0
  497.  
  498. def heal(self, amount):
  499. self.health = min(self.health+amount, 100)
  500.  
  501. def calc_attack(self):
  502. attk = 2
  503. for item in self.equipped:
  504. attk += item.get_attack()
  505.  
  506. return attk
  507.  
  508. def calc_defence(self):
  509. defence = 2
  510.  
  511. for item in self.equipped:
  512. defence += item.get_defence()
  513.  
  514. return defence
  515.  
  516. class Game():
  517. player=None
  518. screen = None
  519. playing = False
  520. def __init__(self):
  521. self.screen = self.load_menu()
  522. self.playing = True
  523. self.player = User()
  524.  
  525. def new_game(self):
  526. self.screen = GameDisplay()
  527.  
  528.  
  529.  
  530.  
  531.  
  532. def load_menu(self):
  533. return MenuDisplay()
  534.  
  535. def in_progress(self):
  536. return self.playing
  537.  
  538. def update(self):
  539. self.screen.update(self)
  540.  
  541. def draw(self):
  542. self.screen.draw(self)
  543.  
  544.  
  545.  
  546. def search(self):
  547. if random.random() < 0.5:
  548. self.screen = BattleDisplay(Boar() if random.random()<0.5 else Slime())
  549.  
  550.  
  551. def inventory(self):
  552. self.screen= InventoryDisplay()
  553.  
  554. def character(self):
  555. self.screen= CharacterDisplay()
  556.  
  557. def merchant(self):
  558.  
  559. self.screen= MerchantDisplay()
  560. if User.shop_visit==0:
  561. 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.")
  562. else:
  563. 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.")
  564. def get_options(self):
  565. return "E: Explore\nI: Inventory\nS: Save\nC: Character\nM: Teleport to Merchant"
  566.  
  567. def get_description(self):
  568. return "You are in a grassy field. What would you like to do?"
  569.  
  570. def save_game(self):
  571. with open("data.save","wb+") as f:
  572. pickle.dump(self,f),
  573. with open("inventory.save","wb+") as inv:
  574. pickle.dump(User.inventory,inv),
  575. with open("equipment.save","wb+") as eqp:
  576. pickle.dump(User.equipped,eqp),
  577. with open("variables.save","wb+") as vrb:
  578. pickle.dump(User.shop_visit,vrb)
  579. print("Game Saved!")
  580.  
  581. def load_game(self):
  582.  
  583. with open("data.save","rb+") as f:
  584. loaded_game= pickle.load(f)
  585. self.player= loaded_game.player
  586. self.screen= loaded_game.screen
  587. self.playing= True
  588.  
  589. with open("inventory.save","rb+") as inv:
  590. loaded_inv= pickle.load(inv)
  591. User.inventory= loaded_inv
  592.  
  593. with open("equipment.save","rb+") as eqp:
  594. loaded_eqp= pickle.load(eqp)
  595. User.equipped= loaded_eqp
  596.  
  597. with open("variables.save","rb+") as vrb:
  598. loaded_vrb= pickle.load(vrb)
  599. User.shop_visit= loaded_vrb
  600.  
  601. print("Game Loaded!")
  602.  
  603.  
  604. if __name__ == "__main__":
  605. game = Game()
  606.  
  607. while game.in_progress():
  608. game.draw()
  609.  
  610. game.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement