Guest User

Untitled

a guest
Mar 1st, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.07 KB | None | 0 0
  1. import random
  2.  
  3. class World:
  4. def __init__(self):
  5. self.environments = []
  6.  
  7. def add_environment(self, environment):
  8. self.environments.append(environment)
  9.  
  10. def get_environment(self, name):
  11. for environment in self.environments:
  12. if environment.name == name:
  13. return environment
  14. return None
  15.  
  16.  
  17. def move(self, direction):
  18. # Find the new environment in the specified direction
  19. new_environment = None
  20. if direction == "north":
  21. new_environment = self.current_environment.north
  22. elif direction == "east":
  23. new_environment = self.current_environment.east
  24. elif direction == "south":
  25. new_environment = self.current_environment.south
  26. elif direction == "west":
  27. new_environment = self.current_environment.west
  28.  
  29. # Check if the new environment exists
  30. if new_environment is None:
  31. print("You can't go that way!")
  32. return
  33.  
  34. # Update the current environment and display its description
  35. self.current_environment = new_environment
  36. print(self.current_environment.description)
  37.  
  38. # Check for a random encounter
  39. if random.random() < self.current_environment.encounter_rate:
  40. enemy = self.current_environment.get_random_enemy()
  41. combat(self.player, enemy)
  42.  
  43.  
  44. def play(self):
  45. self.player = create_player()
  46. self.current_environment = self.environments[0]
  47. print(f"Hello, {self.player.name}!")
  48. print(f"You are in {self.current_environment.name}.")
  49. print(f"{self.current_environment.description}")
  50. while True:
  51. print("What would you like to do?")
  52. print("1. Move to a new environment")
  53. print("2. Check your inventory")
  54. print("3. Equip an Item")
  55. print("4. Quit the game")
  56. if(isinstance(self.current_environment,Inn)):
  57. print("Press I to talk with innkeeper")
  58. print("Press P to talk with Patron")
  59. choice = input("> ")
  60. if choice == "1":
  61. print("Which direction do you want to go? (north, east, south, or west)")
  62. direction = input("> ").lower()
  63. self.move(direction)
  64. # Check for a random encounter
  65. if random.random() < self.current_environment.encounter_rate:
  66. enemy = self.current_environment.get_random_enemy()
  67. combat(self.player, enemy)
  68. elif choice == "2":
  69. self.player.display_inventory()
  70.  
  71. elif choice == "3":
  72. item_name = input("Which item do you want to equip? ")
  73. for i in self.player.inventory:
  74. if item_name.lower() == i.name.lower():
  75. self.player.equip_item(i)
  76. elif choice == "4":
  77. print("Thanks for playing!")
  78. break
  79. elif(isinstance(self.current_environment,Inn)):
  80. if choice == "I":
  81. self.current_environment.talk_to("innkeeper",self.player)
  82. if choice == "P":
  83. self.current_environment.talk_to("patron",self.player)
  84.  
  85.  
  86.  
  87.  
  88.  
  89. # Create a player class to store player information
  90. class Player:
  91. def __init__(self, name,playerClass,startingItems=[]):
  92. self.name = name
  93. self.health = playerClass.health
  94. self.max_health = playerClass.max_health
  95. self.attack = playerClass.attack
  96. self.defense = playerClass.defense
  97. self.speed = playerClass.speed
  98. self.gold = 0
  99. self.inventory = startingItems
  100. self.playerClass = playerClass
  101. self.equipped_items = {}
  102.  
  103. def display_stats(self):
  104. print(f"{self.name}'s Stats:")
  105. print(f"health: {self.health}/{self.max_health}")
  106. print(f"Attack: {self.attack}")
  107. print(f"Defense: {self.defense}")
  108. print(f"Gold: {self.gold}")
  109. print(f"Inventory: {self.inventory}")
  110. print()
  111. def display_inventory(self):
  112. print(f"{self.name}'s inventory:")
  113. for item in self.inventory:
  114. print(item.name)
  115.  
  116. def equip_item(self,item):
  117. if item in self.equipped_items.values():
  118. print("That item is already equipped.")
  119. return
  120. if isinstance(item, Weapon):
  121. if "Weapon" in self.equipped_items:
  122. print("You can only equip one weapon at a time.")
  123. return
  124. self.equipped_items["Weapon"] = item
  125. self.inventory.remove(item)
  126. self.attack += item.attack_bonus
  127. print(f"You have equipped the {item.name}.")
  128. elif isinstance(item, Armor):
  129. if "Armor" in self.equipped_items:
  130. print("You can only equip one set of armor at a time.")
  131. return
  132. self.equipped_items["Armor"] = item
  133. self.inventory.remove(item)
  134. self.defense += item.defense_bonus
  135. print(f"You have equipped the {item.name}.")
  136. else:
  137. print("That item cannot be equipped.")
  138.  
  139. # Define a list of enemies for the player to encounter
  140. enemies = [
  141. {"name": "Goblin", "health": 50, "attack": 5, "defense": 5, "gold": 10},
  142. {"name": "Orc", "health": 75, "attack": 10, "defense": 10, "gold": 25},
  143. {"name": "Dragon", "health": 200, "attack": 25, "defense": 20, "gold": 100},
  144. ]
  145.  
  146. class Environment:
  147. def __init__(self, name, description, encounter_rate, enemies=[], items=[],rest_bonus = 2):
  148. self.name = name
  149. self.description = description
  150. self.encounter_rate = encounter_rate
  151. self.enemies = enemies if enemies else []
  152. self.items = items if items else []
  153. self.rest_bonus = rest_bonus
  154. self.north = None
  155. self.east = None
  156. self.south = None
  157. self.west = None
  158.  
  159. def add_neighbour(self, direction, environment):
  160. if direction == "north":
  161. self.north = environment
  162. environment.south = self
  163. elif direction == "east":
  164. self.east = environment
  165. environment.west = self
  166. elif direction == "south":
  167. self.south = environment
  168. environment.north = self
  169. elif direction == "west":
  170. self.west = environment
  171. environment.east = self
  172.  
  173. def get_random_enemy(self):
  174. return random.choice(self.enemies)
  175.  
  176. def get_random_item(self):
  177. return random.choice(self.items)
  178.  
  179.  
  180. def display_info(self):
  181. print(self.name)
  182. print(self.description)
  183. print()
  184. if self.enemies:
  185. print("Enemies:")
  186. for enemy in self.enemies:
  187. print(f"- {enemy['name']}")
  188. print()
  189. if self.items:
  190. print("Items:")
  191. for item in self.items:
  192. print(f"- {item}")
  193. print()
  194.  
  195. class Inn(Environment):
  196. def __init__(self, name, description, hp_regen, items=None):
  197. super().__init__(name, description, 0.0,items=items)
  198. self.hp_regen = hp_regen
  199. self.conversation_options = {
  200. "innkeeper": {
  201. "greeting": "Welcome to our inn, weary traveler. Would you like a room for the night?",
  202. "room": "Certainly! Our rooms are cozy and comfortable. It will be {} gold pieces for the night. Would you like to proceed?",
  203. "thanks": "Thank you for choosing our inn. I hope you have a pleasant stay!",
  204. "farewell": "Farewell and safe travels!"
  205. },
  206. "patron": {
  207. "greeting": "Greetings, adventurer. Have you seen any interesting sights on your travels?",
  208. "story": "Ah, I remember the time when I journeyed to the forbidden forest. The trees were so thick you could barely see the sky...",
  209. "rumor": "Have you heard about the cursed temple in the mountains? They say it's guarded by fierce monsters and filled with treasure...",
  210. "farewell": "May your travels be safe and fruitful!"
  211. }
  212. }
  213.  
  214. def rest(self, player):
  215. player.health += self.hp_regen
  216. print(f"You rest at the {self.name} and recover {self.hp_regen} HP.")
  217.  
  218. def talk_to(self, person,player):
  219. options = self.conversation_options.get(person)
  220. if options:
  221. print(options["greeting"])
  222. while True:
  223. choice = input("What would you like to say? ")
  224. if choice.lower() == "room":
  225. cost = self.rest_bonus * 10
  226. print(options["room"].format(cost))
  227. confirm = input("Proceed with the booking? (Y/N) ")
  228. if confirm.lower() == "y":
  229. print(options["thanks"])
  230. self.rest(player)
  231. elif choice.lower() == "story":
  232. print(options["story"])
  233. elif choice.lower() == "rumor":
  234. print(options["rumor"])
  235. elif choice.lower() == "farewell":
  236. print(options["farewell"])
  237. break
  238. else:
  239. print("Sorry, I don't understand. Could you repeat that?")
  240. else:
  241. print("Sorry, there's no one here by that name.")
  242.  
  243.  
  244.  
  245.  
  246. class Warrior:
  247. def __init__(self):
  248. self.name = "Warrior"
  249. self.health = 20
  250. self.max_health = 20
  251. self.attack = 10
  252. self.defense = 8
  253. self.speed = 2
  254.  
  255. class Mage:
  256. def __init__(self):
  257. self.name = "Mage"
  258. self.health = 15
  259. self.max_health = 15
  260. self.attack = 4
  261. self.defense = 3
  262. self.speed = 3
  263.  
  264. class Rogue:
  265. def __init__(self):
  266. self.name = "Rogue"
  267. self.health = 18
  268. self.max_health = 18
  269. self.attack = 4
  270. self.defense = 3
  271. self.speed = 5
  272.  
  273.  
  274. class Weapon:
  275. def __init__(self, name, attack_bonus):
  276. self.name = name
  277. self.attack_bonus = attack_bonus
  278.  
  279. class Armor:
  280. def __init__(self, name, defense_bonus):
  281. self.name = name
  282. self.defense_bonus = defense_bonus
  283.  
  284.  
  285. basic_sword = Weapon("Sword",5)
  286. basic_armor = Weapon("Armor",5)
  287.  
  288. def create_player():
  289. print("Welcome, adventurer! Let's create your character.")
  290. name = input("What is your character's name? ")
  291. print("Choose a class:")
  292. print("1. Warrior")
  293. print("2. Mage")
  294. print("3. Rogue")
  295. class_choice = input("Enter the number of your class choice: ")
  296. while class_choice not in ["1", "2", "3"]:
  297. class_choice = input("Invalid choice. Enter the number of your class choice: ")
  298. if class_choice == "1":
  299. player_class = Warrior()
  300. starting_items = [basic_sword,basic_armor]
  301. elif class_choice == "2":
  302. player_class = Mage()
  303. starting_items = ["wand", "spellbook"]
  304. elif class_choice == "3":
  305. player_class = Rogue()
  306. starting_items = ["dagger", "thieves' tools"]
  307. player = Player(name, player_class, starting_items)
  308. return player
  309.  
  310.  
  311. # Add the environments to the world
  312. world = World()
  313.  
  314.  
  315.  
  316. inn = Inn("The Rusty Sword Inn", "A cozy inn with a roaring fireplace.", 10, items=["potion", "scroll"])
  317. dungeon = Environment("Dungeon", "You are in a dank dungeon.", 0.3,enemies)
  318. room1 = Environment("Room 1", "You are in a dusty room.", 0.1,enemies)
  319. room2 = Environment("Room 2", "You are in a dark room.", 0.2,enemies)
  320. hallway1 = Environment("Hallway 1", "You are in a dimly lit hallway.", 0.05,enemies)
  321. hallway2 = Environment("Hallway 2", "You are in a narrow hallway.", 0.1,enemies)
  322. exit_room = Environment("Exit Room", "You are in a room with a door.", 0.05,enemies)
  323.  
  324.  
  325.  
  326. inn.add_neighbour("north",dungeon)
  327. dungeon.add_neighbour("north", room1)
  328. room1.add_neighbour("south", dungeon)
  329. room1.add_neighbour("north", hallway1)
  330. hallway1.add_neighbour("south", room1)
  331. hallway1.add_neighbour("north", room2)
  332. hallway1.add_neighbour("east", hallway2)
  333. hallway2.add_neighbour("west", hallway1)
  334. room2.add_neighbour("south", hallway1)
  335. room2.add_neighbour("east", exit_room)
  336. exit_room.add_neighbour("west", room2)
  337.  
  338. world.add_environment(inn)
  339. world.add_environment(dungeon)
  340. world.add_environment(room1)
  341. world.add_environment(room2)
  342. world.add_environment(hallway1)
  343. world.add_environment(hallway2)
  344. world.add_environment(exit_room)
  345.  
  346.  
  347. # Define a function for combat
  348. def combat(player, enemy):
  349. print(f"You encounter a {enemy['name']}!")
  350. while player.health > 0 and enemy['health'] > 0:
  351. # Player's turn
  352. print(f"What do you want to do, {player.name}?")
  353. print("1. Attack")
  354. print("2. Defend")
  355. choice = input("> ")
  356. if choice == "1":
  357. damage = random.randint(player.attack // 2, player.attack)
  358. damage -= enemy['defense']
  359. if damage < 0:
  360. damage = 0
  361. enemy['health'] -= damage
  362. print(f"You deal {damage} damage to the {enemy['name']}!")
  363. elif choice == "2":
  364. player.defense += 5
  365. print(f"You brace yourself for the {enemy['name']}'s attack!")
  366.  
  367. # Enemy's turn
  368. if enemy['health'] > 0:
  369. damage = random.randint(enemy['attack'] // 2, enemy['attack'])
  370. damage -= player.defense
  371. if damage < 0:
  372. damage = 0
  373. player.health -= damage
  374. print(f"The {enemy['name']} deals {damage} damage to you!")
  375.  
  376. # Display stats
  377. player.display_stats()
  378. print(f"The {enemy['name']}'s health: {enemy['health']}\n")
  379.  
  380. if player.health <= 0:
  381. print("You have been defeated!")
  382. quit()
  383. else:
  384. print(f"You defeated the {enemy['name']}!")
  385. player.gold += enemy['gold']
  386. player.display_stats()
  387. print(f"You gained {enemy['gold']} gold!\n")
  388.  
  389.  
  390. # Game loop
  391. world.play()
  392.  
Advertisement
Add Comment
Please, Sign In to add comment