Advertisement
Guest User

4096 max output length final response

a guest
Aug 30th, 2024
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.14 KB | None | 0 0
  1. Certainly! I'll expand the code to include more features, such as a map system, additional player stats, a level-up mechanism, and a simple quest system. Here's a longer version of the text-based adventure game:
  2.  
  3. ```python
  4. import random
  5. import time
  6. import json
  7.  
  8. class Player:
  9. def __init__(self, name):
  10. self.name = name
  11. self.level = 1
  12. self.exp = 0
  13. self.max_health = 100
  14. self.health = self.max_health
  15. self.strength = 10
  16. self.defense = 5
  17. self.inventory = {}
  18. self.location = "Town Square"
  19. self.quests = []
  20.  
  21. class Enemy:
  22. def __init__(self, name, level, health, damage, exp_reward):
  23. self.name = name
  24. self.level = level
  25. self.health = health
  26. self.max_health = health
  27. self.damage = damage
  28. self.exp_reward = exp_reward
  29.  
  30. class Item:
  31. def __init__(self, name, description, effect, value):
  32. self.name = name
  33. self.description = description
  34. self.effect = effect
  35. self.value = value
  36.  
  37. class Quest:
  38. def __init__(self, name, description, objective, reward):
  39. self.name = name
  40. self.description = description
  41. self.objective = objective
  42. self.reward = reward
  43. self.completed = False
  44.  
  45. class Map:
  46. def __init__(self):
  47. self.locations = {
  48. "Town Square": ["Merchant's Shop", "Tavern", "Forest Path"],
  49. "Merchant's Shop": ["Town Square"],
  50. "Tavern": ["Town Square"],
  51. "Forest Path": ["Town Square", "Dark Cave", "River Bank"],
  52. "Dark Cave": ["Forest Path"],
  53. "River Bank": ["Forest Path", "Mountain Trail"],
  54. "Mountain Trail": ["River Bank", "Mountain Peak"],
  55. "Mountain Peak": ["Mountain Trail"]
  56. }
  57.  
  58. def print_slowly(text):
  59. for char in text:
  60. print(char, end='', flush=True)
  61. time.sleep(0.02)
  62. print()
  63.  
  64. def create_enemies():
  65. return [
  66. Enemy("Goblin", 1, 30, 5, 10),
  67. Enemy("Wolf", 2, 40, 8, 15),
  68. Enemy("Orc", 3, 60, 12, 25),
  69. Enemy("Troll", 4, 100, 18, 40),
  70. Enemy("Dragon", 10, 200, 30, 100)
  71. ]
  72.  
  73. def create_items():
  74. return {
  75. "Health Potion": Item("Health Potion", "Restores 50 HP", lambda player: heal(player, 50), 20),
  76. "Strength Potion": Item("Strength Potion", "Increases strength by 5", lambda player: buff_stat(player, "strength", 5), 30),
  77. "Iron Sword": Item("Iron Sword", "A sturdy iron sword", lambda player: equip_weapon(player, "Iron Sword", 5), 50),
  78. "Steel Armor": Item("Steel Armor", "Protective steel armor", lambda player: equip_armor(player, "Steel Armor", 10), 80),
  79. "Magic Scroll": Item("Magic Scroll", "A scroll with mysterious runes", lambda player: add_to_inventory(player, "Magic Scroll"), 100)
  80. }
  81.  
  82. def create_quests():
  83. return [
  84. Quest("Goblin Slayer", "Defeat 5 Goblins", {"Goblin": 5}, {"exp": 50, "gold": 100}),
  85. Quest("Wolf Hunter", "Defeat 3 Wolves", {"Wolf": 3}, {"exp": 75, "gold": 150}),
  86. Quest("Orc Chieftain", "Defeat the Orc leader", {"Orc": 1}, {"exp": 100, "gold": 200}),
  87. ]
  88.  
  89. def heal(player, amount):
  90. player.health = min(player.health + amount, player.max_health)
  91. print_slowly(f"You restored {amount} HP. Current HP: {player.health}/{player.max_health}")
  92.  
  93. def buff_stat(player, stat, amount):
  94. if stat == "strength":
  95. player.strength += amount
  96. print_slowly(f"Your strength increased by {amount}. Current strength: {player.strength}")
  97. elif stat == "defense":
  98. player.defense += amount
  99. print_slowly(f"Your defense increased by {amount}. Current defense: {player.defense}")
  100.  
  101. def equip_weapon(player, weapon_name, strength_bonus):
  102. player.strength += strength_bonus
  103. print_slowly(f"You equipped {weapon_name}. Strength increased by {strength_bonus}.")
  104.  
  105. def equip_armor(player, armor_name, defense_bonus):
  106. player.defense += defense_bonus
  107. print_slowly(f"You equipped {armor_name}. Defense increased by {defense_bonus}.")
  108.  
  109. def add_to_inventory(player, item_name):
  110. if item_name in player.inventory:
  111. player.inventory[item_name] += 1
  112. else:
  113. player.inventory[item_name] = 1
  114. print_slowly(f"{item_name} added to your inventory.")
  115.  
  116. def level_up(player):
  117. player.level += 1
  118. player.max_health += 20
  119. player.health = player.max_health
  120. player.strength += 2
  121. player.defense += 1
  122. print_slowly(f"Congratulations! You've reached level {player.level}!")
  123. print_slowly(f"Your stats have increased:")
  124. print_slowly(f"Max Health: {player.max_health}")
  125. print_slowly(f"Strength: {player.strength}")
  126. print_slowly(f"Defense: {player.defense}")
  127.  
  128. def check_level_up(player):
  129. exp_required = player.level * 100
  130. if player.exp >= exp_required:
  131. player.exp -= exp_required
  132. level_up(player)
  133. check_level_up(player)
  134.  
  135. def explore(player, game_map, enemies, items):
  136. print_slowly(f"\n{player.name} is exploring {player.location}...")
  137. time.sleep(1)
  138.  
  139. if random.random() < 0.6:
  140. enemy = random.choice([e for e in enemies if e.level <= player.level + 2])
  141. print_slowly(f"You encounter a level {enemy.level} {enemy.name}!")
  142. combat(player, enemy)
  143. elif random.random() < 0.3:
  144. item_name = random.choice(list(items.keys()))
  145. item = items[item_name]
  146. print_slowly(f"You found a {item.name}!")
  147. print_slowly(item.description)
  148. add_to_inventory(player, item.name)
  149. else:
  150. print_slowly("You find nothing of interest.")
  151.  
  152. def combat(player, enemy):
  153. print_slowly(f"\nCombat with level {enemy.level} {enemy.name} begins!")
  154.  
  155. while player.health > 0 and enemy.health > 0:
  156. print_slowly(f"\n{player.name}'s Health: {player.health}/{player.max_health}")
  157. print_slowly(f"{enemy.name}'s Health: {enemy.health}/{enemy.max_health}")
  158.  
  159. action = input("Do you want to (A)ttack, (U)se item, or (R)un? ").lower()
  160.  
  161. if action == 'a':
  162. damage = max(0, player.strength - enemy.level)
  163. enemy.health -= damage
  164. print_slowly(f"You deal {damage} damage to the {enemy.name}.")
  165.  
  166. if enemy.health <= 0:
  167. print_slowly(f"You have defeated the {enemy.name}!")
  168. player.exp += enemy.exp_reward
  169. print_slowly(f"You gained {enemy.exp_reward} experience points.")
  170. check_level_up(player)
  171. update_quests(player, enemy.name)
  172. return
  173.  
  174. player_damage = max(0, enemy.damage - player.defense)
  175. player.health -= player_damage
  176. print_slowly(f"The {enemy.name} deals {player_damage} damage to you.")
  177. elif action == 'u':
  178. use_item(player)
  179. elif action == 'r':
  180. if random.random() < 0.5:
  181. print_slowly("You successfully flee from the battle!")
  182. return
  183. else:
  184. print_slowly("You failed to escape!")
  185. player_damage = max(0, enemy.damage - player.defense)
  186. player.health -= player_damage
  187. print_slowly(f"The {enemy.name} deals {player_damage} damage to you.")
  188. else:
  189. print_slowly("Invalid action. You hesitate and lose your turn!")
  190. player_damage = max(0, enemy.damage - player.defense)
  191. player.health -= player_damage
  192. print_slowly(f"The {enemy.name} deals {player_damage} damage to you.")
  193.  
  194. if player.health <= 0:
  195. print_slowly("You have been defeated. Game Over.")
  196. else:
  197. print_slowly(f"You have defeated the {enemy.name}!")
  198.  
  199. def use_item(player):
  200. if not player.inventory:
  201. print_slowly("Your inventory is empty.")
  202. return
  203.  
  204. print_slowly("Your inventory:")
  205. for item, count in player.inventory.items():
  206. print_slowly(f"{item}: {count}")
  207.  
  208. item_choice = input("Enter the name of the item you want to use (or 'cancel'): ")
  209. if item_choice.lower() == 'cancel':
  210. return
  211.  
  212. if item_choice in player.inventory and player.inventory[item_choice] > 0:
  213. item = items[item_choice]
  214. item.effect(player)
  215. player.inventory[item_choice] -= 1
  216. if player.inventory[item_choice] == 0:
  217. del player.inventory[item_choice]
  218. else:
  219. print_slowly("Invalid item or you don't have any of that item.")
  220.  
  221. def update_quests(player, enemy_name):
  222. for quest in player.quests:
  223. if not quest.completed and enemy_name in quest.objective:
  224. quest.objective[enemy_name] -= 1
  225. if quest.objective[enemy_name] == 0:
  226. del quest.objective[enemy_name]
  227.  
  228. if not quest.objective:
  229. quest.completed = True
  230. print_slowly(f"Quest completed: {quest.name}")
  231. player.exp += quest.reward["exp"]
  232. print_slowly(f"You gained {quest.reward['exp']} experience points.")
  233. check_level_up(player)
  234. # Add gold or other rewards here
  235.  
  236. def show_quests(player):
  237. if not player.quests:
  238. print_slowly("You have no active quests.")
  239. return
  240.  
  241. print_slowly("Your active quests:")
  242. for quest in player.quests:
  243. print_slowly(f"- {quest.name}: {quest.description}")
  244. if not quest.completed:
  245. for enemy, count in quest.objective.items():
  246. print_slowly(f" Defeat {count} {enemy}(s)")
  247. else:
  248. print_slowly(" Completed!")
  249.  
  250. def travel(player, game_map):
  251. print_slowly(f"You are currently in {player.location}.")
  252. print_slowly("You can travel to:")
  253. for i, location in enumerate(game_map.locations[player.location], 1):
  254. print_slowly(f"{i}. {location}")
  255.  
  256. choice = input("Enter the number of your destination (or 'cancel'): ")
  257. if choice.lower() == 'cancel':
  258. return
  259.  
  260. try:
  261. new_location = game_map.locations[player.location][int(choice) - 1]
  262. player.location = new_location
  263. print_slowly(f"You have traveled to {new_location}.")
  264. except (ValueError, IndexError):
  265. print_slowly("Invalid choice. You stay where you are.")
  266.  
  267. def save_game(player):
  268. save_data = {
  269. "name": player.name,
  270. "level": player.level,
  271. "exp": player.exp,
  272. "max_health": player.max_health,
  273. "health": player.health,
  274. "strength": player.strength,
  275. "defense": player.defense,
  276. "inventory": player.inventory,
  277. "location": player.location,
  278. "quests": [
  279. {
  280. "name": quest.name,
  281. "description": quest.description,
  282. "objective": quest.objective,
  283. "reward": quest.reward,
  284. "completed": quest.completed
  285. }
  286. for quest in player.quests
  287. ]
  288. }
  289.  
  290. with open(f"{player.name}_save.json", "w") as f:
  291. json.dump(save_data, f)
  292.  
  293. print_slowly("Game saved successfully.")
  294.  
  295. def load_game():
  296. name = input("Enter the name of the saved game to load: ")
  297. try:
  298. with open(f"{name}_save.json", "r") as f:
  299. save_data = json.load(f)
  300.  
  301. player = Player(save_data["name"])
  302. player.level = save_data["level"]
  303. player.exp = save_data["exp"]
  304. player.max_health = save_data["max_health"]
  305. player.health = save_data["health"]
  306. player.strength = save_data["strength"]
  307. player.defense = save_data["defense"]
  308. player.inventory = save_data["inventory"]
  309. player.location = save_data["location"]
  310. player.quests = [
  311. Quest(q["name"], q["description"], q["objective"], q["reward"])
  312. for q in save_data["quests"]
  313. ]
  314. for i, quest in enumerate(player.quests):
  315. quest.completed = save_data["quests"][i]["completed"]
  316.  
  317. print_slowly("Game loaded successfully.")
  318. return player
  319. except FileNotFoundError:
  320. print_slowly("Save file not found. Starting a new game.")
  321. return None
  322.  
  323. def main():
  324. print_slowly("Welcome to the Extended Text Adventure Game!")
  325.  
  326. load_choice = input("Do you want to (N)ew Game or (L)oad Game? ").lower()
  327. if load_choice == 'l':
  328. player = load_game()
  329.  
  330. if player is None:
  331. player_name = input("Enter your character's name: ")
  332. player = Player(player_name)
  333.  
  334. game_map = Map()
  335. enemies = create_enemies()
  336. global items
  337. items = create_items()
  338. quests = create_quests()
  339.  
  340. print_slowly(f"\nWelcome, {player.name}! Your adventure begins now.")
  341.  
  342. while player.health > 0:
  343. print_slowly(f"\nLocation: {player.location}")
  344. print_slowly(f"Level: {player.level} | Exp: {player.exp}/{player.level * 100}")
  345. print_slowly(f"Health: {player.health}/{player.max_health}")
  346. print_slowly(f"Strength: {player.strength} | Defense: {player.defense}")
  347.  
  348. action = input("\nWhat would you like to do? (E)xplore, (T)ravel, (R)est, (Q)uests, (I)nventory, (S)ave, or (Quit): ").lower()
  349.  
  350. if action == 'e':
  351. explore(player, game_map, enemies, items)
  352. elif action == 't':
  353. travel(player, game_map)
  354. elif action == 'r':
  355. heal_amount = random.randint(10, 20)
  356. heal(player, heal_amount)
  357. elif action == 'q':
  358. show_quests(player)
  359. elif action == 'i':
  360. use_item(player)
  361. elif action == 's':
  362. save_game(player)
  363. elif action == 'quit':
  364. save_choice = input("Do you want to save before quitting? (Y/N): ").lower()
  365. if save_choice == 'y':
  366. save_game(player)
  367. print_slowly("Thank you for playing. Goodbye!")
  368. break
  369. else:
  370. print_slowly("Invalid action. Please try again.")
  371.  
  372. if player.health <= 0:
  373. print_slowly("Game Over. Your adventure has come to an end.")
  374.  
  375. if __name__ == "__main__":
  376. main()
  377. ```
  378.  
  379. Would you like me to explain or break down this expanded code?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement