Game Mechanics

mail@pastecode.io avatar
unknown
python
21 days ago
3.3 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

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 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} 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("Player is defeated!")
            return False

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!")

    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

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

if __name__ == "__main__":
    main()

Leave a Comment