Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- User
- Come up with some code. But like... make it a really long codeblock.
- Assistant
- 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.
- ```python
- import random
- import time
- class Player:
- def __init__(self, name):
- self.name = name
- self.health = 100
- self.inventory = []
- self.location = "Start"
- class Enemy:
- def __init__(self, name, health, damage):
- self.name = name
- self.health = health
- self.damage = damage
- class Item:
- def __init__(self, name, description, effect):
- self.name = name
- self.description = description
- self.effect = effect
- def print_slowly(text):
- for char in text:
- print(char, end='', flush=True)
- time.sleep(0.03)
- print()
- def create_enemies():
- return [
- Enemy("Goblin", 30, 10),
- Enemy("Orc", 50, 15),
- Enemy("Troll", 80, 20),
- Enemy("Dragon", 150, 30)
- ]
- def create_items():
- return [
- Item("Health Potion", "A small vial of red liquid", lambda player: setattr(player, "health", min(player.health + 25, 100))),
- Item("Sword", "A sharp, gleaming blade", lambda player: player.inventory.append("Sword")),
- Item("Shield", "A sturdy wooden shield", lambda player: player.inventory.append("Shield")),
- Item("Magic Scroll", "A mysterious scroll with ancient runes", lambda player: player.inventory.append("Magic Scroll"))
- ]
- def explore(player, enemies, items):
- print_slowly(f"\n{player.name} is exploring...")
- time.sleep(1)
- if random.random() < 0.6:
- enemy = random.choice(enemies)
- print_slowly(f"You encounter a {enemy.name}!")
- combat(player, enemy)
- elif random.random() < 0.3:
- item = random.choice(items)
- print_slowly(f"You found a {item.name}!")
- print_slowly(item.description)
- item.effect(player)
- print_slowly(f"{item.name} has been added to your inventory.")
- else:
- print_slowly("You find nothing of interest.")
- def combat(player, enemy):
- print_slowly(f"\nCombat with {enemy.name} begins!")
- while player.health > 0 and enemy.health > 0:
- print_slowly(f"\n{player.name}'s Health: {player.health}")
- print_slowly(f"{enemy.name}'s Health: {enemy.health}")
- action = input("Do you want to (A)ttack or (R)un? ").lower()
- if action == 'a':
- damage = random.randint(5, 20)
- enemy.health -= damage
- print_slowly(f"You deal {damage} damage to the {enemy.name}.")
- if enemy.health <= 0:
- print_slowly(f"You have defeated the {enemy.name}!")
- return
- player.health -= enemy.damage
- print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
- elif action == 'r':
- if random.random() < 0.5:
- print_slowly("You successfully flee from the battle!")
- return
- else:
- print_slowly("You failed to escape!")
- player.health -= enemy.damage
- print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
- else:
- print_slowly("Invalid action. You hesitate and lose your turn!")
- player.health -= enemy.damage
- print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
- if player.health <= 0:
- print_slowly("You have been defeated. Game Over.")
- else:
- print_slowly(f"You have defeated the {enemy.name}!")
- def main():
- print_slowly("Welcome to the Text Adventure Game!")
- player_name = input("Enter your character's name: ")
- player = Player(player_name)
- enemies = create_enemies()
- items = create_items()
- print_slowly(f"\nWelcome, {player.name}! Your adventure begins now.")
- while player.health > 0:
- print_slowly(f"\nCurrent Location: {player.location}")
- print_slowly(f"Health: {player.health}")
- print_slowly(f"Inventory: {', '.join(player.inventory) if player.inventory else 'Empty'}")
- action = input("\nWhat would you like to do? (E)xplore, (R)est, or (Q)uit: ").lower()
- if action == 'e':
- explore(player, enemies, items)
- elif action == 'r':
- heal_amount = random.randint(10, 20)
- player.health = min(player.health + heal_amount, 100)
- print_slowly(f"You rest and recover {heal_amount} health.")
- elif action == 'q':
- print_slowly("Thank you for playing. Goodbye!")
- break
- else:
- print_slowly("Invalid action. Please try again.")
- if player.health <= 0:
- print_slowly("Game Over. Your adventure has come to an end.")
- if __name__ == "__main__":
- main()
- ```
- Would you like me to explain or break down this code?
- User:
- No, can you make it longer? Add more features.
- Assistant:
- 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:
- ```python
- import random
- import time
- import json
- import os
- class Player:
- def __init__(self, name):
- self.name = name
- self.health = 100
- self.max_health = 100
- self.inventory = {}
- self.location = "Town Square"
- self.gold = 50
- self.experience = 0
- self.level = 1
- def level_up(self):
- self.level += 1
- self.max_health += 20
- self.health = self.max_health
- print_slowly(f"Congratulations! You've reached level {self.level}!")
- print_slowly(f"Your maximum health has increased to {self.max_health}.")
- def gain_experience(self, amount):
- self.experience += amount
- if self.experience >= self.level * 100:
- self.experience -= self.level * 100
- self.level_up()
- class Enemy:
- def __init__(self, name, health, damage, gold_reward, exp_reward):
- self.name = name
- self.health = health
- self.max_health = health
- self.damage = damage
- self.gold_reward = gold_reward
- self.exp_reward = exp_reward
- class Item:
- def __init__(self, name, description, effect, value):
- self.name = name
- self.description = description
- self.effect = effect
- self.value = value
- class Shop:
- def __init__(self):
- self.inventory = {}
- def add_item(self, item, quantity):
- if item.name in self.inventory:
- self.inventory[item.name]["quantity"] += quantity
- else:
- self.inventory[item.name] = {"item": item, "quantity": quantity}
- def remove_item(self, item_name, quantity):
- if item_name in self.inventory:
- self.inventory[item_name]["quantity"] -= quantity
- if self.inventory[item_name]["quantity"] <= 0:
- del self.inventory[item_name]
- class Quest:
- def __init__(self, name, description, objective, reward):
- self.name = name
- self.description = description
- self.objective = objective
- self.reward = reward
- self.completed = False
- def print_slowly(text):
- for char in text:
- print(char, end='', flush=True)
- time.sleep(0.02)
- print()
- def create_enemies():
- return [
- Enemy("Goblin", 30, 10, 15, 20),
- Enemy("Orc", 50, 15, 25, 35),
- Enemy("Troll", 80, 20, 40, 50),
- Enemy("Dragon", 150, 30, 100, 150)
- ]
- def create_items():
- return {
- "Health Potion": Item("Health Potion", "A small vial of red liquid", lambda player: setattr(player, "health", min(player.health + 25, player.max_health)), 20),
- "Sword": Item("Sword", "A sharp, gleaming blade", lambda player: player.inventory.update({"Sword": player.inventory.get("Sword", 0) + 1}), 50),
- "Shield": Item("Shield", "A sturdy wooden shield", lambda player: player.inventory.update({"Shield": player.inventory.get("Shield", 0) + 1}), 40),
- "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)
- }
- def create_shop(items):
- shop = Shop()
- for item in items.values():
- shop.add_item(item, random.randint(1, 5))
- return shop
- def create_quests():
- return [
- Quest("Goblin Slayer", "Defeat 5 Goblins", {"Goblin": 5}, {"gold": 50, "exp": 100}),
- Quest("Treasure Hunter", "Find 3 Magic Scrolls", {"Magic Scroll": 3}, {"gold": 75, "exp": 150}),
- Quest("Dragon Slayer", "Defeat the Dragon", {"Dragon": 1}, {"gold": 200, "exp": 500})
- ]
- def explore(player, enemies, items, quests):
- print_slowly(f"\n{player.name} is exploring...")
- time.sleep(1)
- if random.random() < 0.6:
- enemy = random.choice(enemies)
- print_slowly(f"You encounter a {enemy.name}!")
- result = combat(player, enemy)
- if result == "victory":
- player.gold += enemy.gold_reward
- player.gain_experience(enemy.exp_reward)
- print_slowly(f"You gained {enemy.gold_reward} gold and {enemy.exp_reward} experience!")
- update_quests(player, quests, enemy.name)
- elif random.random() < 0.3:
- item = random.choice(list(items.values()))
- print_slowly(f"You found a {item.name}!")
- print_slowly(item.description)
- item.effect(player)
- print_slowly(f"{item.name} has been added to your inventory.")
- update_quests(player, quests, item.name)
- else:
- print_slowly("You find nothing of interest.")
- def combat(player, enemy):
- print_slowly(f"\nCombat with {enemy.name} begins!")
- while player.health > 0 and enemy.health > 0:
- print_slowly(f"\n{player.name}'s Health: {player.health}/{player.max_health}")
- print_slowly(f"{enemy.name}'s Health: {enemy.health}/{enemy.max_health}")
- action = input("Do you want to (A)ttack, (U)se item, or (R)un? ").lower()
- if action == 'a':
- damage = random.randint(5, 20)
- if "Sword" in player.inventory:
- damage += 5
- enemy.health -= damage
- print_slowly(f"You deal {damage} damage to the {enemy.name}.")
- if enemy.health <= 0:
- print_slowly(f"You have defeated the {enemy.name}!")
- return "victory"
- player_damage = max(0, enemy.damage - (5 if "Shield" in player.inventory else 0))
- player.health -= player_damage
- print_slowly(f"The {enemy.name} deals {player_damage} damage to you.")
- elif action == 'u':
- use_item(player)
- elif action == 'r':
- if random.random() < 0.5:
- print_slowly("You successfully flee from the battle!")
- return "flee"
- else:
- print_slowly("You failed to escape!")
- player.health -= enemy.damage
- print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
- else:
- print_slowly("Invalid action. You hesitate and lose your turn!")
- player.health -= enemy.damage
- print_slowly(f"The {enemy.name} deals {enemy.damage} damage to you.")
- if player.health <= 0:
- print_slowly("You have been defeated. Game Over.")
- return "defeat"
- else:
- print_slowly(f"You have defeated the {enemy.name}!")
- return "victory"
- def use_item(player):
- print_slowly("Your inventory:")
- for item, quantity in player.inventory.items():
- print_slowly(f"{item}: {quantity}")
- item_name = input("Enter the name of the item you want to use (or 'cancel'): ")
- if item_name.lower() == 'cancel':
- return
- if item_name in player.inventory and player.inventory[item_name] > 0:
- item = items[item
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement