User Interaction

mail@pastecode.io avatar
unknown
python
a month ago
3.8 kB
0
Indexable
Never
import random

class Entity:
    def __init__(self, name, health, attack_power, defense):
        self.name = name
        self.health = health
        self.attack_power = attack_power
        self.defense = defense

    def attack(self):
        return random.randint(1, self.attack_power)

    def take_damage(self, damage):
        self.health -= max(0, damage - self.defense)

class Player(Entity):
    def __init__(self, name, health=100, attack_power=20, defense=5):
        super().__init__(name, health, attack_power, defense)
        self.score = 0
        self.weapon = "Sword"

class Monster(Entity):
    def __init__(self, name, health, attack_power, defense, treasure_value):
        super().__init__(name, health, attack_power, defense)
        self.treasure_value = treasure_value

    def is_alive(self):
        return self.health > 0

class Treasure:
    def __init__(self, name, value):
        self.name = name
        self.value = value

def combat(player, monster):
    print(f"{player.name} engages {monster.name} in combat!")
    while player.health > 0 and monster.health > 0:
        # Player's turn
        player_damage = player.attack()
        print(f"{player.name} attacks {monster.name} with {player.weapon} for {player_damage} damage!")
        monster.take_damage(player_damage)
        if not monster.is_alive():
            print(f"{monster.name} is defeated!")
            player.score += monster.treasure_value
            return True

        # Monster's turn
        monster_damage = monster.attack()
        print(f"{monster.name} attacks {player.name} for {monster_damage} damage!")
        player.take_damage(monster_damage)
        if player.health <= 0:
            print("You were defeated by", monster.name)
            return False

def display_player_status(player):
    print(f"Player: {player.name}")
    print(f"HP: {player.health}")
    print(f"Attack Power: {player.attack_power}")
    print(f"Weapon: {player.weapon}")
    print(f"Score: {player.score}")

def display_room_description():
    print("You stand in a dimly lit cavern.")
    print("A slimy troll blocks the passage north.")

def main():
    player_name = input("Enter your name: ")
    player = Player(player_name)
    monsters = [
        Monster("Goblin", 50, 10, 2, 20),
        Monster("Skeleton", 40, 8, 3, 25),
        Monster("Orc", 60, 12, 5, 30)
    ]
    treasures = [
        Treasure("Gold Coin", 10),
        Treasure("Silver Coin", 5),
        Treasure("Ruby", 50)
    ]
    dungeon_exit = Treasure("Dungeon Exit", 100)

    print("Welcome to the Dungeon!")
    display_player_status(player)
    display_room_description()

    while True:
        # Encounter a monster
        current_monster = random.choice(monsters)
        print(f"\nYou encounter a {current_monster.name}!")
        if combat(player, current_monster):
            print(f"You gain {current_monster.treasure_value} points for defeating {current_monster.name}!")
        else:
            print("Game Over!")
            break

        # Collect treasure
        found_treasure = random.choice(treasures)
        print(f"You find {found_treasure.name} worth {found_treasure.value} points!")
        player.score += found_treasure.value

        # Check for win condition
        if player.score >= dungeon_exit.value:
            print("Congratulations! You found the dungeon exit and won the game!")
            break

        # Continue or end game
        choice = input("Continue exploring? (yes/no): ").lower()
        if choice != 'yes':
            break

    display_player_status(player)
    print(f"Your final score is: {player.score}")

if __name__ == "__main__":
    main()
Leave a Comment