r/PythonLearning • • Apr 22 '26

Help Request Need Opinions

Can someone run this and give opinions on cleanliness and scalability? New to programming. Super simple fight simulator between Appa and Momo lol.

115 Upvotes

20 comments sorted by

View all comments

2

u/SuccessfulLeague7284 Apr 24 '26
UPDATED CODE: THANKS FOR THE FEEDBACK!!! I should be able to build much more efficiently on top of this...



class Creature:

    def __init__(self, name, hp, stamina, abilities):
        self.name = name
        self.hp = hp
        self.stamina = stamina
        master_ab_dict = {"tail whip": self.tail_whip , "roar" : self.roar , "scratch": self.scratch , "bite": self.bite, "growl": self.growl }
        self.ab_dict = {}
        for move in abilities:
            if move in master_ab_dict:
                self.ab_dict[move] = master_ab_dict[move]


    def tail_whip(self , enemy):
            enemy.hp -= 2
            self.stamina -= 1
            print(f"{enemy.name} hp: {enemy.hp}" )
            print(f"{self.name} stamina: {self.stamina}")

    def roar(self, enemy):
            enemy.stamina -= 2
            self.stamina -= 1
            print(f"{enemy.name} stamina: {enemy.stamina}")
            print(f"{self.name} stamina: {self.stamina}")

    def scratch(self, enemy):
        enemy.hp -= 1
        self.stamina -= 1
        print(f"{enemy.name} hp: {enemy.hp}")
        print(f"{self.name} stamina: {self.stamina}")

    def bite(self, enemy):
        enemy.hp -= 3
        self.stamina -=2
        print(f"{enemy.name} hp: {enemy.hp}")
        print(f"{self.name} stamina: {self.stamina}")

    def growl(self, enemy):
        enemy.stamina -= 5
        print(f"{enemy.name} stamina: {enemy.stamina}")


appa = Creature("Appa", 25, 30, ["roar", "tail whip"])
momo = Creature("Momo", 10, 40, ["scratch", "roar"])
pb_dog = Creature("Polar Bear Dog" , 18, 40, ["bite" , "growl"] )

characters = {"appa": appa , "momo": momo, "polar bear dog": pb_dog}

def choose_character():
    while True:
        player = input("choose a character: ").lower().strip()
        if player in characters:
            player = characters.get(player)
            return player
        else:
            print("choose a valid character....")

player = choose_character() #player is chosen one time for the entire script and is defined here

def fight_flow(all_enemies):
    while True:

        target = None
        while True:
            enemy = input("choose your opponent: ").lower().strip()
            if enemy == player.name.lower().strip():
                print("can't hit yourself...")
                continue
            elif enemy in characters:
                target = characters[enemy]
            else:
                print("choose a valid opponent...")
                continue
            if target.hp > 0:
                break
            else:
                print("enemy already defeated; choose another enemy...")
                continue

        action = None
        while True:
            att = input("choose your ability: ").lower().strip()
            if att in player.ab_dict:
                action = player.ab_dict[att]
                break
            else:
                print(f"{player.name} Doesn't know that ability...")

        action(target)
        if target.hp > 0:
            continue
        else:
            print(f"{target.name} has fallen!!!")

        if all(enemy.hp <= 0 for enemy in all_enemies): #checks to see if all enemies in the fight are dead
            print("FIGHT WON!!!")
            return
        else:
            continue







print()
all_enemies = [momo , appa] #change all_enemies list for each fight encounter to control the story
fight_flow(all_enemies)

#NEXT CREATE A STAMINA CHECK SO THAT A PLAYER WITH NO STAMINA MUST WAIT FOR STAMINA REGENERATION
#CREATE A DECISION TREE BASED AI ENEMY SYSTEM SO THAT ENEMIES FIGHT BACK
#CREATE A PLAYER DEATH AND DEFEAT CONDITION
#CREATE A WAY TO EXCLUDE THE PLAYER FROM BEING IN THE all_enemies LIST FOR ANY GIVEN FIGHT
#ADD time.sleep FOR PACING

2

u/PureWasian Apr 24 '26

Just saw your reply to my original comment and this update, very excited for you!!

I'll share some edits to this just to give you some more ideas on refactoring/code organization, but definitely go with what makes the most sense to you

1

u/SuccessfulLeague7284 Apr 24 '26

Thanks so much!!!

1

u/PureWasian Apr 24 '26 edited Apr 24 '26

Here is what I came up with. At a high level, the main learning point I think you're getting a little mixed up on is the difference between a Class and an Object.

A Class would define what "momo" (or other creature) is, but you need to spawn in a "momo" as an Object with a living, breathing health bar/stamina value/etc. Or you can even have multiple "momo" Objects in existence all at once!

So, I'd keep your characters as a read-only glossary of how to create (instantiate) new characters when you startup your code. And then all_enemies or enemy or player should reference the actually "spawned" objects rather than modifying characters values directly.

The other refactoring changes I did:

  • I moved Abilities functions into a separate class so they act like reusable functions, and then mapped names to those functions to master_ab_dict as a registry of everything available for Creatures to use. Kinda helps compartmentalize it more for taking two creature objects and does the calculating/updating stuff in more of a vacuum (so you can split it into separate files easier)

  • two small helper functions for printing hp/stamina values because, again, minimize copy/pasting when you can

  • "guard clauses" around using an ability -- it returns False and warns the player if they do not have enough stamina for an ability. Otherwise it returns True if successful. This lets the combat loop retry the action until a valid ability is chosen (setting successful_action to True)

  • choose_character() and spawn_creature() to actually instantiate the Objects of each character Class as you need them (see explanations in first few paragraphs)

  • is_fight_over() helper function to handle all the logic for checking win/loss. Similarly, input_valid_enemy() and input_valid_ability() helper functions to keep fight_flow() shorter and more high-level understandable.

So, even with these changes, two things I discovered but didn't bother to solve while refactoring:

  • you cannot target an enemy "momo" if you are "momo". And similarly, if you fight two "momo"s at the same time, it would break the input_valid_enemy() logic (and similar for what you have in your existing code)

  • with the added stamina-checking guard clauses, there is no error checking for how to complete the combat if you are out of stamina. So you'd be stuck there :)

Anyways, it was a fun exercise for me so thank you for making the post! Definitely give the code below a read and please ask away if any is unclear. (And again, just offering some fresh perspective - no need to change much if you prefer yours more, though I would def. suggest having creatures be read-only and making all of the changes related to that)

Hopefully you can kinda start to see where the code can be split into separate files that coordinate together with the way I set it up, and how helper functions can help keep things more organized if you were to continue to flesh out the rest of it.

1

u/PureWasian Apr 24 '26 edited Apr 24 '26

The entry point of the program is near the bottom (player = choose_character()), which is where execution starts. I kept the main runtime flow grouped together there so it’s easy to see how everything ties together.

 # can keep this dictionary in JSON file, database table, etc.
 characters = {
     "appa": {
         "name": "Appa",
         "hp": 25,
         "stamina": 30,
         "abilities": ["roar", "tail whip"]
     },
     "momo": {
         "name": "Momo",
         "hp": 10,
         "stamina": 40,
         "abilities": ["scratch", "roar"]
     },
     "polar bear dog": {
         "name": "Polar Bear Dog",
         "hp": 18,
         "stamina": 40,
         "abilities": ["bite", "growl"]
     }
 }
 
 def print_hp(creature):
     print(f"{creature.name} hp: {creature.hp}")
 
 def print_stamina(creature):
     print(f"{creature.name} stamina: {creature.stamina}")
 
 class Abilities:
     def check_stamina(source, stamina_cost):
         if source.stamina >= stamina_cost:
             return True
         else:
             print(f"{source.name} does not have enough stamina!")
             return False
 
     def tail_whip(source, enemy):
         stamina_cost = 1
         hp_drain = 2
         if Abilities.check_stamina(source, stamina_cost):
             enemy.hp -= hp_drain
             source.stamina -= stamina_cost
             print_hp(enemy)
             print_stamina(source)
             return True
         else:
             return False
 
     def roar(source, enemy):
         stamina_cost = 1
         stamina_drain = 2
         if Abilities.check_stamina(source, stamina_cost):
             enemy.stamina -= stamina_drain
             source.stamina -= stamina_cost
             print_stamina(enemy)
             print_stamina(source)
             return True
         else:
             return False
 
     def scratch(source, enemy):
         stamina_cost = 1
         hp_drain = 1
         if Abilities.check_stamina(source, stamina_cost):
             enemy.hp -= hp_drain
             source.stamina -= stamina_cost
             print_hp(enemy)
             print_stamina(source)
             return True
         else:
             return False
 
     def bite(source, enemy):
         stamina_cost = 3
         hp_drain = 2
         if Abilities.check_stamina(source, stamina_cost):
             enemy.hp -= hp_drain
             source.stamina -= stamina_cost
             print_hp(enemy)
             print_stamina(source)
             return True
         else:
             return False
 
     def growl(source, enemy):
         enemy.stamina -= 5
         print_stamina(enemy)
         return True
 
 class Creature:
     def __init__(self, name, hp, stamina, abilities):
         self.name = name
         self.hp = hp
         self.stamina = stamina
         self.ab_dict = {}
         for move in abilities:
             if move in master_ab_dict:
                 self.ab_dict[move] = master_ab_dict[move]
 
 # linkage from named key to each ability function
 master_ab_dict = {
     "tail whip": Abilities.tail_whip,
     "roar" : Abilities.roar,
     "scratch": Abilities.scratch,
     "bite": Abilities.bite,
     "growl": Abilities.growl
 }
 
 def choose_character():
     valid_characters = ', '.join(list(characters.keys()))
 
     user_input = input(f"choose a character: ({valid_characters}): ")
     while user_input.lower().strip() not in characters:
         user_input = input(f"choose a valid character... ({valid_characters})")
 
     character = characters.get(user_input)
     return Creature(
         character["name"],
         character["hp"],
         character["stamina"],
         character["abilities"]
     )
 
 def spawn_creature(creature_key):
     if creature_key in characters.keys():
         creature = characters[creature_key]
         return Creature(
             creature["name"],
             creature["hp"],
             creature["stamina"],
             creature["abilities"]
         )
     else:
         return None
 
 def is_fight_over(player, all_enemies):
     # player dying has precedence over enemy dying (note if/elif logic order)
     if player.hp <= 0:
         print("FIGHT LOST...")
         return True
     elif all(enemy.hp <= 0 for enemy in all_enemies):
         print("FIGHT WON!!!")
         return True
     return False
 
 def input_valid_enemy(player, all_enemies):
     enemy_map = {enemy.name.lower().strip(): enemy for enemy in all_enemies}
     options = ', '.join(enemy.name.lower() for enemy in all_enemies)
     while True:
         user_input = input(f"choose your opponent ({options}): ").lower().strip()
         
         if user_input == player.name.lower().strip():
             print("can't hit yourself...")
         elif user_input in enemy_map:
             target = enemy_map[user_input]
             if target.hp > 0:
                 return target
             else:
                 print("enemy already defeated; choose another enemy...")
         else:
             print("choose a valid opponent...")
 
 def input_valid_ability(player):
     options = ', '.join(list(player.ab_dict.keys()))
     while True:
         user_input = input(f"choose your ability ({options}): ")
         if user_input.lower().strip() in player.ab_dict:
             return player.ab_dict[user_input]
         else:
             print(f"{player.name} Doesn't know that ability...")
     
 
 def fight_flow(player, all_enemies):
     while not is_fight_over(player, all_enemies):
         target = input_valid_enemy(player, all_enemies)

         successful_action = False
         while not successful_action:
            action = input_valid_ability(player)
            successful_action = action(player, target)

         if target.hp > 0:
             continue
         else:
             print(f"{target.name} has fallen!!!")
 
 player = choose_character()
 creature1 = spawn_creature("momo")
 creature2 = spawn_creature("appa")
 all_enemies = [creature1, creature2]
 fight_flow(player, all_enemies)
 
 #NEXT CREATE A STAMINA CHECK SO THAT A PLAYER WITH NO STAMINA MUST WAIT FOR STAMINA REGENERATION
 #CREATE A DECISION TREE BASED AI ENEMY SYSTEM SO THAT ENEMIES FIGHT BACK
 #CREATE A PLAYER DEATH AND DEFEAT CONDITION
 #CREATE A WAY TO EXCLUDE THE PLAYER FROM BEING IN THE all_enemies LIST FOR ANY GIVEN FIGHT
 #ADD time.sleep FOR PACING