By Armv

2015-11-05 05:43:08 8 Comments

I just learned about classes today and wanted to find a way to implement them that interested me. I whipped up this little battle simulator and would like any feedback and critiques as well as any ideas or similar projects I could pursue.

import random
import time
import decimal

rnd = 0

class Unit: 
    def __init__(self, n, hp, s, d, a, mp): = n 
        self.maxHP = hp 
        self.curHP = hp 
        self.strength = s 
        self.defence = d 
        self.agility = a = mp

    def describe(self):
        print ("HP: "+ str(self.curHP) + " STRENGTH:"+ str(self.strength) + " DEFENCE:" + str(self.defence) + " AGILITY:" + str(self.agility) + " MP:" + str(

def battle( a, b ): 
    turn = 1 

    if a.agility > b.agility: 
        attacker = a 
        defender = b 
        attacker = b 
        defender = a 

    doBattle = True 
    while (doBattle):
        print ("\n ---  Turn " , turn , ": " , , " ---" )
        print ( , ": " , attacker.curHP )
        print ( , ": " , defender.curHP , "\n") 

        if defender.agility > attacker.agility and random.randint(1,4) == 4: 
            print ( , " missed." )
            if random.randint(1,5) != random.randint(1,6):
                multiplier_1 = random.randint(70,150)/100
                multiplier_2 = random.randint(70,150)/100
                attackDamage = round(decimal.Decimal(attacker.strength*multiplier_1),1) - round(decimal.Decimal(defender.defence*multiplier_2),1) 
                if attackDamage < 0: 
                    attackDamage = 0 
                defender.curHP = defender.curHP - attackDamage 
                print ( , " did " , attackDamage , " damage to " , , "." )
                attackDamage = round(decimal.Decimal(attacker.strength*2),1) - round(decimal.Decimal(defender.defence/2),2)
                if attackDamage < 0: 
                    attackDamage = 0 
                defender.curHP = defender.curHP - attackDamage 
                print ("CRITICAL HIT!!!!!" + , " did " , attackDamage , " damage to " , , "." )

        if defender.curHP <= 0 and == 0: 
            doBattle = False 
            print ("\n\n" , , " won the battle!" )
        elif defender.curHP <= 0 and >= 0:
   -= 1
            defender.curHP += random.randint(4,16)
            if defender.curHP > 0:
                print("You try to use your MP to heal!")
                print("It worked! HP: " + str(defender.curHP))
                print("You try to use your MP to heal!")
                print("No effect... you die.")

            temp = defender 
            defender = attacker 
            attacker = temp 
            turn = turn + 1 

name = input("Your name: ") 

character = Unit(name, random.randint(13,30), random.randint(2,5), random.randint(1,3), random.randint(1,5), random.randint(1,5) ) 

while input("Fight? [y/n]: ") == "y":
    rnd += 1
    print("Enemy Info:")
    enemy = Unit("Monster", random.randint(10,25), random.randint(2,6), random.randint(1,3), random.randint(1,5), random.randint(0,1)) 
    character.maxHP = character.curHP
    character.curHP += 3
    powerup = random.randint(1,3)
    if powerup == 1 and rnd >= 2:
        character.strength += random.randint(1,2)
        print("STR UP! Strength is now " + str(character.strength))
    elif powerup == 2 and rnd >= 2:
        character.defence += random.randint(1,2)
        print("DEF UP! Defense is now " + str(character.defence))
    elif powerup == 3 and rnd >= 2:
        character.agility += random.randint(1,2)
        print("AGILITY UP! Agility is now " + str(character.agility))
    if rnd >= 2:
        print("Character Info:")
    battle( character, enemy )


@301_Moved_Permanently 2015-11-05 13:11:46


Since you only use the sleep function from the time module, you should emphasize it by declaring from time import sleep and change your calls to only sleep. I find it cleaner even though it's not a strong rule of thumb, taste varies and both forms are acceptable.

I’d recommend getting rid of the decimal module. You have no need of strict floating point computation and since you are rounding (thus converting to floats) anyway, you loose it's interest right after using it. Better round the final result of the computation and compute intermediate results using good-old floats.

String representation

What do you do when you want to output the content of a dictionary? You print it. What about lists? You print them. Integers, sets, tuples? print them all.

Same goes for your Units. You should print(character) instead of character.describe(). How to do that without getting outputs like <__main__.Unit object at 0x02EBFF10>? By implementing the __str__ special method.

Object-Oriented Programming

Your functions does a lot of things and nearly all of them are directly related to your Units. You should both split them into smaller functions that (for now) do little but have meaning (and meaningful names) and make those function Unit's methods. You will be able to more easily extend them latter if you decide to add more mechanisms to your battles.

Check if a Unit is alive, heal it, check whether of two Units is the swifter are all actions that can reasonably have their own function. For now the code will be simple but if you plan on adding a loot system with boost items, potions or even curses, you’ll have an easier time managing the whole logic because you will only have to include things in these tiny functions.

Similarly, since these functions are directly related to Units and perform actions on them, they should be implemented as methods in your class.


Using a flag to end a while loop on certain condition feels not quite right. You should either:

  • loop forever and break on such conditions;
  • use the condition as the condition of the while itself.

If the condition is complex, a function call might even make things more readable. In battle the while condition should be “neither a nor b is dead”.

For your main loop, you should also try to sanitize your input. To me, 'y', 'Y', ' y', ' Yes ', '' are all valid values to continue. A common idiom is to use input(...).strip() and even input(...).strip().lower() if you want to check into a set of predefined values.

General improvements

  • Using functions/methods can also be a way to avoid repeating the same lines of code. You should also try to avoid it in if...else. For instance when computing the result of an attack:

    attackDamage = round(decimal.Decimal(attacker.strength*multiplier_1),1) - round(decimal.Decimal(defender.defence*multiplier_2),1) 
    if attackDamage < 0: 
        attackDamage = 0 
    defender.curHP = defender.curHP - attackDamage 
    print ( , " did " , attackDamage , " damage to " , , "." )


    attackDamage = round(decimal.Decimal(attacker.strength*2),1) - round(decimal.Decimal(defender.defence/2),2)
    if attackDamage < 0: 
        attackDamage = 0 
    defender.curHP = defender.curHP - attackDamage 
    print ("CRITICAL HIT!!!!!" + , " did " , attackDamage , " damage to " , , "." )

    are pretty similar. You can avoid it by setting only the multipliers in the test and performing the other operations “out” of the if.

  • Python allow you to unpack iterables into several variables at once. See here and here for details. Combined with implicit tuples it allows to simply write:

    attacker, defender = defender, attacker

    if you want to swap the two variables.

  • Similarly, in some places, using the ternary operator x if condition else y will make your code shorter and cleaner.

  • The builtin function max will also help you make the code more readable.

  • Spacing is important for readability and should be consistent: no space before an opening parenthesis in a function call, one space after a coma… You should also try to limit your line length to 80 characters. Read PEP 8 for a full list of recommendations and some working examples.

    Also print already uses a space as separator between arguments. You don't need to add them manually. Or, if you want to explicitly position them, you can override print default separator with the sep keyword:

    print('Hello', 'world!', sep='-') # outputs 'Hello-world!'


  • Unit's maxHP is set but never used. It sounds to me like you forgot to implement what you intended it for.

  • A battle will continue if the defender died and failed to use it’s mp to heal itself.

Proposed Improvements

I used getattr and setattr to dynamically update the attribute represented by the string chosen by random.choice. If you’re not used to it and don't quite get it, your approach is fine. Just make sure to not repeat yourself when computing the increment for the attribute.

import random
from time import sleep

class Unit: 
    def __init__(self, n, hp, s, d, a, mp): = n
        self.maxHP = hp
        self.curHP = hp
        self.strength = s
        self.defence = d
        self.agility = a = mp

    def __str__(self):
        descr = "[{}] HP: {}, STRENGTH: {}, DEFENSE: {}, AGILITY: {}, MP: {}"
        return descr.format(, self.curHP, self.strength,
                            self.defence, self.agility,

    def is_dead(self):
        return self.curHP <= 0

    def perform_attack(self, ennemy):
        if random.randint(1,5) != random.randint(1,6):
            strength_multiplier = random.randint(70, 150) / 100
            defence_multiplier = random.randint(70, 150) / 100
            strength_multiplier = 2
            defence_multiplier = 0.5
            print("CRITICAL HIT!!!!")
        attackDamage = max(self.strength * strength_multiplier -
                           ennemy.defence * defence_multiplier, 0)
        ennemy.curHP = round(ennemy.curHP - attackDamage, 1)
        print(, "did", attackDamage, "damage to",, ".")

    def heal(self):
        self.maxHP = self.curHP
        self.curHP += 3

    def heal_with_mp(self):
        if > 0:
   -= 1
            print(, "try to use an MP to heal!")
            self.curHP += random.randint(4, 16)
            if self.is_dead():
                print("No effect…",, "died")
                print("It worked! HP:", self.curHP)

    def swifter(self, ennemy):
        return self.agility > ennemy.agility

    def battle(self, ennemy):
        # Resolve first attacker based on agility
        # we attack first if there is a draw
        attacker, defender = ((ennemy, self)
                              if ennemy.swifter(self)
                              else (self, ennemy))

        turn = 0
        while not (self.is_dead() or ennemy.is_dead()):
            turn += 1
            print("---  Turn", turn, ":",, "---" )
            print(, ":", attacker.curHP)
            print(, ":", defender.curHP)

            # Allow less agile defenders to avoid the attack
            # with a lower probability chance
            miss_factor = 4 if defender.swifter(attacker) else 20
            if random.randint(1,miss_factor) == miss_factor: 
                print(, "missed.")

            if defender.is_dead():

            attacker, defender = defender, attacker

        winner = if self.is_dead() else
        print(winner, "won the battle!")

    def encounter(self, ennemy):
        if not self.is_dead():

    def power_up(self):
        trait = random.choice(('strength', 'defence', 'agility'))
        new_value = getattr(self, trait) + random.randint(1, 2)
        setattr(self, trait, new_value)
        print(trait.upper(), "UP!", trait.title(), "is now", new_value)

def journey():
    name = input("Your name: ") 

    character = Unit(name,
                     random.randint(13, 30),
                     random.randint(2, 5),
                     random.randint(1, 3),
                     random.randint(1, 5),
                     random.randint(1, 5))

    while not character.is_dead():
        enemy = Unit("Monster",
                     random.randint(10, 25),
                     random.randint(2, 6),
                     random.randint(1, 3),
                     random.randint(1, 5),
                     random.randint(0, 1))
        if input("Fight? [y/n]: ").strip().lower() not in {'y', 'yes', ''}:

if __name__ == "__main__":

Going further

The first step forward to improve your script could be to use inheritance to provide several presets of monsters: create several class that inherit from Unit and use super to fix default values. You can still have a GenericMonster class whose call to super().__init__ will still use random values. Try to make your monsters unique with a combination of two high traits and two low ones (or one high and 3 medium). You will then be able to build a random monster with:

EnemyType = random.choice((GenericMonster, Zombie, Blob, ReptilianWarrior,...))
enemy = EnemyType()

You can also allow your monster to take the number of rounds as parameter and be stronger and stronger as the brawl goes on.

An other interesting change would be to allow the user to input a space separated sequence of integer to be used as an initializer of his character traits. Build a list of constraints (maximum 40 points, life between 15 and 30, strength between 2 and 10, etc) and check them either in the constructor or before.

With several types of monsters, you could rewrite your power-up system to gain traits based on the type of enemy you encountered (a strong enemy will build up your defense, an agile one your agility…). Or you could also let the user choose.

You can then build a text or ascii-art based labyrinth traversal so that refusing to fight a monster will still allow you to choose an other direction and find an other one (hopefully easier) to fight.

You can then improve it to add loots, inventory, curses, potions, shops…

Last step is to turn it into an MMORPG :)

@brian_o 2015-11-05 16:32:32

This is a great answer and great advice. Don't be daunted by the proposed changes. One of the nice things about prototyping in Python is the ability to mix object-oriented and procedural code; you don't have to implement all these changes at once! Using your code as the starting point, try implementing Mathias's swifter function, then use that to determine attacker and defender. You'll be slowly transitioning behavior to the Unit class instead of the battle function. Most important: Keep running and testing the program between changes.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Java RPG battle simulator

2 Answered Questions

[SOLVED] Basic Battle Simulator GUI RPG Game

1 Answered Questions

[SOLVED] Pokemon battle simulator

1 Answered Questions

[SOLVED] Exurbb (automated turn based) Battle Simulator Revised

1 Answered Questions

[SOLVED] Exurbb (automated turn based) Battle Simulator

2 Answered Questions

[SOLVED] Turn-based battle simulator

2 Answered Questions

[SOLVED] Random RPG Battle System in Lua

1 Answered Questions

[SOLVED] Beginner code for a text-based battle simulator

3 Answered Questions

[SOLVED] Risk battle simulator v2

3 Answered Questions

[SOLVED] Risk battle simulator v1

Sponsored Content