Untitled
unknown
plain_text
a year ago
3.5 kB
6
Indexable
import random
# Player class
class Player:
def __init__(self, name):
self.name = name
self.health = 100
self.inventory = []
def attack(self):
return random.randint(5, 15)
def take_damage(self, damage):
self.health -= damage
def is_alive(self):
return self.health > 0
# Monster class
class Monster:
def __init__(self, name, health):
self.name = name
self.health = health
def attack(self):
return random.randint(5, 10)
def take_damage(self, damage):
self.health -= damage
def is_alive(self):
return self.health > 0
# Game class
class Game:
def __init__(self, player_name):
self.player = Player(player_name)
self.monsters = [
Monster("Goblin", 30),
Monster("Skeleton", 50),
Monster("Orc", 80)
]
self.game_over = False
def play(self):
print(f"Welcome, {self.player.name}, to the dungeon!")
while not self.game_over:
self.show_status()
self.player_action()
if not self.player.is_alive():
print("You have been defeated. Game over.")
self.game_over = True
def show_status(self):
print(f"\n{self.player.name}'s health: {self.player.health}")
print(f"Inventory: {', '.join(self.player.inventory) if self.player.inventory else 'Empty'}")
if self.monsters:
print(f"Monsters remaining: {', '.join(monster.name for monster in self.monsters)}")
def player_action(self):
action = input("\nWhat do you want to do? (explore/attack/run): ").lower()
if action == "explore":
self.explore()
elif action == "attack":
self.attack_monster()
elif action == "run":
self.run_away()
else:
print("Invalid action. Please choose again.")
def explore(self):
if self.monsters:
monster = random.choice(self.monsters)
print(f"You encounter a {monster.name}!")
self.combat(monster)
else:
print("The dungeon is empty. You have defeated all the monsters!")
def attack_monster(self):
if self.monsters:
monster = self.monsters[0]
self.combat(monster)
else:
print("There are no monsters to attack.")
def combat(self, monster):
print(f"\nCombat with {monster.name} begins!")
while self.player.is_alive() and monster.is_alive():
player_damage = self.player.attack()
monster.take_damage(player_damage)
print(f"You dealt {player_damage} damage to the {monster.name}.")
if monster.is_alive():
monster_damage = monster.attack()
self.player.take_damage(monster_damage)
print(f"The {monster.name} dealt {monster_damage} damage to you.")
if not monster.is_alive():
print(f"You have defeated the {monster.name}!")
self.monsters.remove(monster)
self.player.inventory.append(f"{monster.name} trophy")
def run_away(self):
if self.monsters:
print("You run away from the dungeon.")
self.game_over = True
else:
print("There is nothing to run away from.")
# Main function to start the game
if __name__ == "__main__":
player_name = input("Enter your character's name: ")
game = Game(player_name)
game.play()Editor is loading...
Leave a Comment