Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
4.0 kB
1
Indexable
Never
import random
import time

# Player class to hold player information
class Player:
    def __init__(self, name):
        self.name = name
        self.level = 1
        self.hp = 100
        self.max_hp = 100
        self.attack = 10
        self.exp = 0
        self.exp_to_next_level = 100
    
    def display_stats(self):
        print(f"{self.name}'s Stats:")
        print(f"Level: {self.level}")
        print(f"HP: {self.hp}/{self.max_hp}")
        print(f"Attack: {self.attack}")
        print(f"Experience: {self.exp}/{self.exp_to_next_level}")
    
    def level_up(self):
        self.level += 1
        self.max_hp += 20
        self.hp = self.max_hp
        self.attack += 5
        self.exp -= self.exp_to_next_level
        self.exp_to_next_level = int(self.exp_to_next_level * 1.5)
        print(f"Congratulations! {self.name} leveled up to Level {self.level}!")
        print(f"You gained 20 Max HP and 5 Attack.")
        print(f"Next level requires {self.exp_to_next_level} experience points.")

# Enemy class for creating different enemies
class Enemy:
    def __init__(self, name, hp, attack):
        self.name = name
        self.hp = hp
        self.attack = attack
    
    def display_enemy(self):
        print(f"You've encountered a {self.name}!")
        print(f"HP: {self.hp}")
        print(f"Attack: {self.attack}")

# Function to simulate battle between player and enemy
def battle(player, enemy):
    print("\n=== Battle Start ===")
    enemy.display_enemy()
    
    while player.hp > 0 and enemy.hp > 0:
        # Player's turn
        print(f"\n{player.name}'s turn:")
        print("1. Attack")
        print("2. Run")
        choice = input("Choose your action (1 or 2): ")
        
        if choice == '1':
            damage_dealt = random.randint(player.attack // 2, player.attack)
            enemy.hp -= damage_dealt
            print(f"You dealt {damage_dealt} damage to the {enemy.name}.")
        elif choice == '2':
            print("You managed to escape from the battle.")
            return False
        
        # Check if enemy is defeated
        if enemy.hp <= 0:
            print(f"You defeated the {enemy.name}!")
            player.exp += random.randint(10, 20)
            if player.exp >= player.exp_to_next_level:
                player.level_up()
            return True
        
        # Enemy's turn
        print(f"\n{enemy.name}'s turn:")
        damage_received = random.randint(enemy.attack // 2, enemy.attack)
        player.hp -= damage_received
        print(f"The {enemy.name} dealt {damage_received} damage to you.")
        
        # Check if player is defeated
        if player.hp <= 0:
            print(f"Game Over! {player.name} was defeated by the {enemy.name}.")
            return False
        
        # Display current stats
        player.display_stats()

# Main function to start the game
def main():
    print("Welcome to Text-Based RPG!")
    player_name = input("Enter your character's name: ")
    player = Player(player_name)
    
    while True:
        print("\n=== Main Menu ===")
        print("1. Explore")
        print("2. Display Stats")
        print("3. Quit")
        choice = input("Enter your choice (1-3): ")
        
        if choice == '1':
            # Explore - encounter a random enemy
            enemy_name = random.choice(["Goblin", "Orc", "Slime"])
            enemy_hp = random.randint(20, 50)
            enemy_attack = random.randint(5, 15)
            enemy = Enemy(enemy_name, enemy_hp, enemy_attack)
            if battle(player, enemy):
                print(f"You continue your journey.")
            else:
                break
        elif choice == '2':
            # Display player stats
            player.display_stats()
        elif choice == '3':
            # Quit the game
            print("Thanks for playing!")
            break
        else:
            print("Invalid choice. Please enter 1, 2, or 3.")

# Run the game
if __name__ == "__main__":
    main()
Leave a Comment