python バケモン

import random

class Bakemon:
    def __init__(self, name, hp, attack):
        self.name = name
        self.hp = hp
        self.attack = attack

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

    def take_damage(self, damage):
        self.hp -= damage
        if self.hp < 0:
            self.hp = 0

    def attack_opponent(self, opponent):
        damage = random.randint(1, self.attack)
        opponent.take_damage(damage)
        return damage

def create_bakemon():
    bakemon_list = [
        Bakemon("Bakachu", 50, 10),
        Bakemon("Charabak", 60, 12),
        Bakemon("Bakasaur", 55, 11),
        Bakemon("Squirtlemon", 50, 10)
    ]
    return bakemon_list

def choose_bakemon(bakemon_list):
    print("Choose your Bakemon:")
    for idx, bakemon in enumerate(bakemon_list):
        print(f"{idx + 1}. {bakemon.name} (HP: {bakemon.hp}, Attack: {bakemon.attack})")
    choice = int(input("Enter the number of your choice: ")) - 1
    return bakemon_list[choice]

def battle(player_bakemon, enemy_bakemon):
    print(f"A wild {enemy_bakemon.name} appeared!")
    while player_bakemon.is_alive() and enemy_bakemon.is_alive():
        print(f"\n{player_bakemon.name} (HP: {player_bakemon.hp}) vs {enemy_bakemon.name} (HP: {enemy_bakemon.hp})")
        action = input("Do you want to attack (a) or run (r)? ").lower()
        if action == 'a':
            damage = player_bakemon.attack_opponent(enemy_bakemon)
            print(f"{player_bakemon.name} dealt {damage} damage to {enemy_bakemon.name}!")
            if enemy_bakemon.is_alive():
                damage = enemy_bakemon.attack_opponent(player_bakemon)
                print(f"{enemy_bakemon.name} dealt {damage} damage to {player_bakemon.name}!")
            else:
                print(f"{enemy_bakemon.name} is defeated!")
                break
        elif action == 'r':
            print("You ran away!")
            break
        else:
            print("Invalid action. Please choose again.")
    
    if not player_bakemon.is_alive():
        print(f"{player_bakemon.name} is defeated! Game over.")
        return False
    return True

def main():
    print("Welcome to the Bakemon game!")
    bakemon_list = create_bakemon()
    player_bakemon = choose_bakemon(bakemon_list)
    
    while True:
        enemy_bakemon = random.choice(bakemon_list)
        if enemy_bakemon == player_bakemon:
            continue
        if not battle(player_bakemon, enemy_bakemon):
            break
        play_again = input("Do you want to battle again? (y/n): ").lower()
        if play_again != 'y':
            print("Thanks for playing! Goodbye.")
            break

if __name__ == "__main__":
    main()

投稿者: chosuke

趣味はゲームやアニメや漫画などです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です