Python Forum

Full Version: New guy working on some basic battle mechanics. Little help?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please understand that I'm extremely new to Python and object oriented programming so I'm sure this code looks like a mess. I'm working on a basic RPG style battle system and I'm reaching the limits of my knowledge. I made a character class and worked out the basics mechanics of how battle works. You'll notice i have a SPD (speed) described in the class. I want this number to determine who goes first, but I have no idea how to apply this. I have other goals, but ill start small with that one. Any advice?

class Character:

    def __init__(self, name, HP, ATK, DEF, SPD):
        self.name = name
        self.HP = HP
        self.ATK = ATK
        self.DEF = DEF
        self.SPD = SPD
from character import Character
import random
import time


character1 = Character("Adam", 5000, 100, 0, 0)
character2 = Character("Buddy", 5000, 100, 0, 1)
round = 0

### Battle Mechanics ###
while character1.HP >= 0 and character2.HP >= 0:
    winner = ""
    round += 1
    dice1 = random.randint(1, 50)
    dice2 = random.randint(1, 50)
    swing1 = character1.ATK - character2.DEF + dice1
    swing2 = character2.ATK - character1.DEF + dice2

    crit1 = False
    crit2 = False
    if dice2 >= 47:
        swing2 = swing2 * 2
        crit2 = True
    if dice1 >= 47:
        swing1 = swing1 * 2
        crit1 = True
    fumble1 = False
    fumble2 = False
    if dice2 <= 3:
        swing2 = swing2 / 2
        fumble2 = True
    if dice1 <= 3:
        swing1 = swing1 / 2
        fumble1 = True


    ### Print ###
    print("\nRound " + str(round) + ":")
    time.sleep(.25)


    if character1.HP <= 0:
        winner = character2.name
    else:
        print(character1.name + " hit " + character2.name + " for " + str(swing1) + " damage.")
        if crit1:
            print("Critical hit!")
        if fumble1:
            print(character1.name + " has fumbled!")
    time.sleep(.25)



    if character2.HP <= 0:
        winner = character1.name
    else:
        print(character2.name + " hit " + character1.name + " for " + str(swing2) + " damage.")
        if crit2:
            print("Critical hit!")
        if fumble2:
            print(character2.name + " has fumbled!")
    time.sleep(.25)





    ### Apply Damage ###

    if character1.HP > 0 and character2.HP >0:
        character2.HP -= (swing1)
        character1.HP -= (swing2)



### Outcome Print ###
print("\n\n\n")
if character1.HP > character2.HP:
    print(character1.name + " has killed " + character2.name)
else:
    print(character2.name + " has killed " + character1.name)


print("Rounds: " + str(round))
print(character1.name + "'s HP: " + str(character1.HP))
print(character2.name + "'s HP: " + str(character2.HP))
Also, any additional comments on my coding would be appreciated. I'm still learning the basics.
You can simplify the loop by going through it once for each attacker. With two characters, you could determine which is faster, and assign them to attacker, and assign the other to defender. With more characters, you could make a list and sort it by SPD (combatants.sort(key = lambda character: character.SPD)). Then at the end, you either swap the attacker and defender, or move on to the next attacker in the list.

You need to update the character's HP, obviously. It's not clear how you are determining damage done, but then you can just subtract that from the HP: defender.HP -= damage.
(Mar-16-2019, 03:46 PM)ichabod801 Wrote: [ -> ]You can simplify the loop by going through it once for each attacker. With two characters, you could determine which is faster, and assign them to attacker, and assign the other to defender. With more characters, you could make a list and sort it by SPD (combatants.sort(key = lambda character: character.SPD)). Then at the end, you either swap the attacker and defender, or move on to the next attacker in the list.

You need to update the character's HP, obviously. It's not clear how you are determining damage done, but then you can just subtract that from the HP: defender.HP -= damage.

ok assigning them to attacker and defender using an if-else seems pretty simple actually. i think ill try to make that work.