Advertisement
Guest User

Web UI conversation

a guest
Aug 30th, 2024
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.14 KB | None | 0 0
  1. User
  2. Come up with some code. But like... make it a really long codeblock.
  3.  
  4. Assistant
  5. Certainly! I'll create a longer code block for you with a Python script that implements a simple text-based adventure game. This code will demonstrate various programming concepts including classes, functions, and game logic.
  6. ```python
  7. import random
  8. import time
  9.  
  10. class Player:
  11. def __init__(self, name):
  12. self.name = name
  13. self.health = 100
  14. self.inventory = []
  15. self.location = "Start"
  16.  
  17. class Enemy:
  18. def __init__(self, name, health, damage):
  19. self.name = name
  20. self.health = health
  21. self.damage = damage
  22.  
  23. class Item:
  24. def __init__(self, name, description, effect):
  25. self.name = name
  26. self.description = description
  27. self.effect = effect
  28.  
  29. def print_slowly(text):
  30. for char in text:
  31. print(char, end='', flush=True)
  32. time.sleep(0.03)
  33. print()
  34.  
  35. def create_enemies():
  36. return [
  37. Enemy("Goblin", 30, 10),
  38. Enemy("Orc", 50, 15),
  39. Enemy("Troll", 80, 20),
  40. Enemy("Dragon", 150, 30)
  41. ]
  42.  
  43. def create_items():
  44. return [
  45. Item("Health Potion", "A small vial of red liquid", lambda player: setattr(player, "health", min(player.health + 25, 100))),
  46. Item("Sword", "A sharp, gleaming blade", lambda player: player.inventory.append("Sword")),
  47. Item("Shield", "A sturdy wooden shield", lambda player: player.inventory.append("Shield")),
  48. Item("Magic Scroll", "A mysterious scroll with ancient runes", lambda player: player.inventory.append("Magic Scroll"))
  49. ]
  50.  
  51. def explore(player, enemies, items):
  52. print_slowly(f"\n{player.name} is exploring...")
  53. time.sleep(1)
  54.  
  55. if random.random() < 0.6:
  56. enemy = random.choice(enemies)
  57. print_slowly(f"You encounter a {enemy.name}!")
  58. combat(player, enemy)
  59. elif random.random() < 0.3:
  60. item = random.choice(items)
  61. print_slowly(f"You found a {item.name}!")
  62. print_slowly(item.description)
  63. item.effect(player)
  64. print_slowly(f"{item.name} has been added to your inventory.")
  65. else:
  66. print_slowly("You find nothing of interest.")
  67.  
  68. def combat(player, enemy):
  69. print_slowly(f"\nCombat with {enemy.name} begins!")
  70.  
  71. while player.health > 0 and enemy.health > 0:
  72. print_slowly(f"\n{player.name}'s Health: {player.health}")
  73. print_slowly(f"{enemy.name}'s Health: {enemy.health}")
  74.  
  75. action = input("Do you want to (A)ttack or (R)un? ").lower()
  76.  
  77. if action == 'a':
  78. damage = random.randint(5, 20)
  79. enemy.health -= damage
  80. print_slowly(f"You deal {damage} damage to the {enemy.name}.")
  81.  
  82. if enemy.health <= 0:
  83. print_slowly(f"You have defeated the {enemy.name}!")
  84. return
  85.  
  86. player.health -= enemy.damage
  87. print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
  88. elif action == 'r':
  89. if random.random() < 0.5:
  90. print_slowly("You successfully flee from the battle!")
  91. return
  92. else:
  93. print_slowly("You failed to escape!")
  94. player.health -= enemy.damage
  95. print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
  96. else:
  97. print_slowly("Invalid action. You hesitate and lose your turn!")
  98. player.health -= enemy.damage
  99. print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
  100.  
  101. if player.health <= 0:
  102. print_slowly("You have been defeated. Game Over.")
  103. else:
  104. print_slowly(f"You have defeated the {enemy.name}!")
  105.  
  106. def main():
  107. print_slowly("Welcome to the Text Adventure Game!")
  108. player_name = input("Enter your character's name: ")
  109. player = Player(player_name)
  110.  
  111. enemies = create_enemies()
  112. items = create_items()
  113.  
  114. print_slowly(f"\nWelcome, {player.name}! Your adventure begins now.")
  115.  
  116. while player.health > 0:
  117. print_slowly(f"\nCurrent Location: {player.location}")
  118. print_slowly(f"Health: {player.health}")
  119. print_slowly(f"Inventory: {', '.join(player.inventory) if player.inventory else 'Empty'}")
  120.  
  121. action = input("\nWhat would you like to do? (E)xplore, (R)est, or (Q)uit: ").lower()
  122.  
  123. if action == 'e':
  124. explore(player, enemies, items)
  125. elif action == 'r':
  126. heal_amount = random.randint(10, 20)
  127. player.health = min(player.health + heal_amount, 100)
  128. print_slowly(f"You rest and recover {heal_amount} health.")
  129. elif action == 'q':
  130. print_slowly("Thank you for playing. Goodbye!")
  131. break
  132. else:
  133. print_slowly("Invalid action. Please try again.")
  134.  
  135. if player.health <= 0:
  136. print_slowly("Game Over. Your adventure has come to an end.")
  137.  
  138. if __name__ == "__main__":
  139. main()
  140. ```
  141. Would you like me to explain or break down this code?
  142.  
  143. User:
  144. No, can you make it longer? Add more features.
  145.  
  146. Assistant:
  147. Certainly, I'll expand the code to include more features and complexity. Here's an enhanced version of the text adventure game with additional elements:
  148. ```python
  149. import random
  150. import time
  151. import json
  152. import os
  153.  
  154. class Player:
  155. def __init__(self, name):
  156. self.name = name
  157. self.health = 100
  158. self.max_health = 100
  159. self.inventory = {}
  160. self.location = "Town Square"
  161. self.gold = 50
  162. self.experience = 0
  163. self.level = 1
  164.  
  165. def level_up(self):
  166. self.level += 1
  167. self.max_health += 20
  168. self.health = self.max_health
  169. print_slowly(f"Congratulations! You've reached level {self.level}!")
  170. print_slowly(f"Your maximum health has increased to {self.max_health}.")
  171.  
  172. def gain_experience(self, amount):
  173. self.experience += amount
  174. if self.experience >= self.level * 100:
  175. self.experience -= self.level * 100
  176. self.level_up()
  177.  
  178. class Enemy:
  179. def __init__(self, name, health, damage, gold_reward, exp_reward):
  180. self.name = name
  181. self.health = health
  182. self.max_health = health
  183. self.damage = damage
  184. self.gold_reward = gold_reward
  185. self.exp_reward = exp_reward
  186.  
  187. class Item:
  188. def __init__(self, name, description, effect, value):
  189. self.name = name
  190. self.description = description
  191. self.effect = effect
  192. self.value = value
  193.  
  194. class Shop:
  195. def __init__(self):
  196. self.inventory = {}
  197.  
  198. def add_item(self, item, quantity):
  199. if item.name in self.inventory:
  200. self.inventory[item.name]["quantity"] += quantity
  201. else:
  202. self.inventory[item.name] = {"item": item, "quantity": quantity}
  203.  
  204. def remove_item(self, item_name, quantity):
  205. if item_name in self.inventory:
  206. self.inventory[item_name]["quantity"] -= quantity
  207. if self.inventory[item_name]["quantity"] <= 0:
  208. del self.inventory[item_name]
  209.  
  210. class Quest:
  211. def __init__(self, name, description, objective, reward):
  212. self.name = name
  213. self.description = description
  214. self.objective = objective
  215. self.reward = reward
  216. self.completed = False
  217.  
  218. def print_slowly(text):
  219. for char in text:
  220. print(char, end='', flush=True)
  221. time.sleep(0.02)
  222. print()
  223.  
  224. def create_enemies():
  225. return [
  226. Enemy("Goblin", 30, 10, 15, 20),
  227. Enemy("Orc", 50, 15, 25, 35),
  228. Enemy("Troll", 80, 20, 40, 50),
  229. Enemy("Dragon", 150, 30, 100, 150)
  230. ]
  231.  
  232. def create_items():
  233. return {
  234. "Health Potion": Item("Health Potion", "A small vial of red liquid", lambda player: setattr(player, "health", min(player.health + 25, player.max_health)), 20),
  235. "Sword": Item("Sword", "A sharp, gleaming blade", lambda player: player.inventory.update({"Sword": player.inventory.get("Sword", 0) + 1}), 50),
  236. "Shield": Item("Shield", "A sturdy wooden shield", lambda player: player.inventory.update({"Shield": player.inventory.get("Shield", 0) + 1}), 40),
  237. "Magic Scroll": Item("Magic Scroll", "A mysterious scroll with ancient runes", lambda player: player.inventory.update({"Magic Scroll": player.inventory.get("Magic Scroll", 0) + 1}), 30)
  238. }
  239.  
  240. def create_shop(items):
  241. shop = Shop()
  242. for item in items.values():
  243. shop.add_item(item, random.randint(1, 5))
  244. return shop
  245.  
  246. def create_quests():
  247. return [
  248. Quest("Goblin Slayer", "Defeat 5 Goblins", {"Goblin": 5}, {"gold": 50, "exp": 100}),
  249. Quest("Treasure Hunter", "Find 3 Magic Scrolls", {"Magic Scroll": 3}, {"gold": 75, "exp": 150}),
  250. Quest("Dragon Slayer", "Defeat the Dragon", {"Dragon": 1}, {"gold": 200, "exp": 500})
  251. ]
  252.  
  253. def explore(player, enemies, items, quests):
  254. print_slowly(f"\n{player.name} is exploring...")
  255. time.sleep(1)
  256.  
  257. if random.random() < 0.6:
  258. enemy = random.choice(enemies)
  259. print_slowly(f"You encounter a {enemy.name}!")
  260. result = combat(player, enemy)
  261. if result == "victory":
  262. player.gold += enemy.gold_reward
  263. player.gain_experience(enemy.exp_reward)
  264. print_slowly(f"You gained {enemy.gold_reward} gold and {enemy.exp_reward} experience!")
  265. update_quests(player, quests, enemy.name)
  266. elif random.random() < 0.3:
  267. item = random.choice(list(items.values()))
  268. print_slowly(f"You found a {item.name}!")
  269. print_slowly(item.description)
  270. item.effect(player)
  271. print_slowly(f"{item.name} has been added to your inventory.")
  272. update_quests(player, quests, item.name)
  273. else:
  274. print_slowly("You find nothing of interest.")
  275.  
  276. def combat(player, enemy):
  277. print_slowly(f"\nCombat with {enemy.name} begins!")
  278.  
  279. while player.health > 0 and enemy.health > 0:
  280. print_slowly(f"\n{player.name}'s Health: {player.health}/{player.max_health}")
  281. print_slowly(f"{enemy.name}'s Health: {enemy.health}/{enemy.max_health}")
  282.  
  283. action = input("Do you want to (A)ttack, (U)se item, or (R)un? ").lower()
  284.  
  285. if action == 'a':
  286. damage = random.randint(5, 20)
  287. if "Sword" in player.inventory:
  288. damage += 5
  289. enemy.health -= damage
  290. print_slowly(f"You deal {damage} damage to the {enemy.name}.")
  291.  
  292. if enemy.health <= 0:
  293. print_slowly(f"You have defeated the {enemy.name}!")
  294. return "victory"
  295.  
  296. player_damage = max(0, enemy.damage - (5 if "Shield" in player.inventory else 0))
  297. player.health -= player_damage
  298. print_slowly(f"The {enemy.name} deals {player_damage} damage to you.")
  299. elif action == 'u':
  300. use_item(player)
  301. elif action == 'r':
  302. if random.random() < 0.5:
  303. print_slowly("You successfully flee from the battle!")
  304. return "flee"
  305. else:
  306. print_slowly("You failed to escape!")
  307. player.health -= enemy.damage
  308. print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
  309. else:
  310. print_slowly("Invalid action. You hesitate and lose your turn!")
  311. player.health -= enemy.damage
  312. print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
  313.  
  314. if player.health <= 0:
  315. print_slowly("You have been defeated. Game Over.")
  316. return "defeat"
  317. else:
  318. print_slowly(f"You have defeated the {enemy.name}!")
  319. return "victory"
  320.  
  321. def use_item(player):
  322. print_slowly("Your inventory:")
  323. for item, quantity in player.inventory.items():
  324. print_slowly(f"{item}: {quantity}")
  325.  
  326. item_name = input("Enter the name of the item you want to use (or 'cancel'): ")
  327. if item_name.lower() == 'cancel':
  328. return
  329.  
  330. if item_name in player.inventory and player.inventory[item_name] > 0:
  331. item = items[item
  332. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement