Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class Creature:
- def __init__(self, name, attack, defense, health, char_class=None):
- self.name = name
- self.char_class = char_class
- self.attack = attack
- self.defense = defense
- self.health = health
- self.max_health = health
- def take_damage(self, damage):
- self.health -= damage
- if self.health < 0:
- self.health = 0
- def is_dead(self):
- return self.health == 0
- # Class choices
- class_choices = [
- ["Warrior", 10, 5],
- ["Mage", 5, 10],
- ["Rogue", 7, 7]
- ]
- # Monster choices
- monster_choices = [
- ["Orc", 15, 0, 50],
- ["Troll", 10, 5, 75],
- ["Goblin", 5, 5, 25]
- ]
- # Create character
- print("You are trapped in a dungeon and must fight your way out!")
- print("Choose your class:")
- for i, c in enumerate(class_choices):
- print(f"{i+1}: {c[0]} (Attack: {c[1]}, Defense: {c[2]})")
- choice = int(input()) - 1
- name = input("Enter your character's name: ")
- char_class = class_choices[choice][0]
- attack = class_choices[choice][1]
- defense = class_choices[choice][2]
- character = Creature(name, attack, defense, 200, char_class)
- healing_potions = 3
- gold = 0
- battles_won = 0
- # Game loop
- while True:
- if battles_won == 5:
- print("You emerge from the dungeon, victorious!")
- print(f"You won {battles_won} battles, found {gold} gold, and have {healing_potions} healing potions left.")
- exit()
- # Generate a random monster
- monster_choice = random.randint(0, len(monster_choices)-1)
- monster_name = monster_choices[monster_choice][0]
- monster_attack = monster_choices[monster_choice][1]
- monster_defense = monster_choices[monster_choice][2]
- monster_health = monster_choices[monster_choice][3]
- monster = Creature(monster_name, monster_attack, monster_defense, monster_health)
- print(f"A wild {monster.name} appears!")
- tried_to_flee = False
- while True:
- # Display stats
- print(f"{character.name} (Class: {character.char_class}, Health: {character.health}, Attack: {character.attack}, Defense: {character.defense})")
- print(f"{monster.name} (Health: {monster.health}, Attack: {monster.attack}, Defense: {monster.defense})")
- # Display options
- print("Options:")
- print("1. Fight")
- if healing_potions > 0:
- print("2. Heal")
- if not tried_to_flee:
- print("3. Run")
- choice = int(input())
- # Fight
- if choice == 1:
- # Character attacks monster
- monster.take_damage(character.attack)
- print(f"{character.name} attack the {monster.name} for {character.attack} damage")
- if monster.is_dead():
- print(f"{monster.name} has been defeated!")
- gold += random.randint(10, 20)
- if random.randint(1, 4) == 1:
- healing_potions += 1
- print(f"You found a healing potion! You now have {healing_potions}.")
- battles_won += 1
- character.health += random.uniform(0.25, 0.5) * character.health
- character.health = min(character.health, 100)
- tried_to_flee = False
- break
- # Monster has a chance to run away
- if monster.health < 0.25 * monster.max_health:
- if random.randint(1, 2) == 1:
- print(f"{monster.name} ran away!")
- battles_won += 1
- gold += random.randint(10, 20)
- character.health += random.uniform(0.25, 0.5) * character.max_health
- character.health = min(character.health, character.max_health)
- tried_to_flee = False
- break
- # Monster attacks character
- character.take_damage(monster.attack)
- print(f"{monster.name} attack {character.name} for {monster.attack} damage")
- if character.is_dead():
- print(f"{character.name} has been defeated :(")
- exit()
- # Heal
- elif choice == 2:
- healing_potions -= 1
- character.health += 0.5 * character.max_health
- character.health = min(character.health, 100)
- print(f"You used a healing potion and healed for {int(0.5 * character.health)} health. You now have {healing_potions} healing potions left.")
- # Run
- elif choice == 3:
- if random.randint(1, 2) == 1:
- print("You successfully fled!")
- tried_to_flee = False
- break
- else:
- print("You failed to flee.")
- tried_to_flee = True
- # Automatic rest after battle
- if not character.is_dead():
- print(f"You rest after the battle and heal for {int(random.uniform(0.25, 0.5) * character.max_health)} health.")
- character.health += random.uniform(0.25, 0.5) * character.max_health
- character.health = min(character.health, character.max_health)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement