Compiling Varibles (I think I don't really know what its called)

I’ve decided to code my own word adventure as a fun little side project. If you don’t know what that is its fine doesn’t really mater but I need to make this list of varibles shorter using something like “def” and don’t know how. If anyone could give some suggestions or some links to useful websites that would be great. :slight_smile: Heres the code list of varibles:

mag.BaseHealth = 100 + 'mag.BaseHealthPositive' - 'mag.BaseHealthNegative'
    mag.Health = 'mag.BaseHealth' + 'mag.BaseArmourBoost' + 'upg'
    mag.BaseHealthPositive = 25
    mag.BaseHealthNegative = 0
    mag.BaseArmourBoost = 20
    mag.BaseDamage = 25
    mag.Damage = 'mag.BaseDamage' + 'mag.BaseDamagePositive' - 'mag.BaseDamageNegative'
    mag.BaseDamagePositive = 0
    mag.BaseDamageNegative = 0
    #Knight
    kni.BaseHealth = 100 + 'kni.BaseHealthPositive' - 'kni.BaseHealthNegative'
    kni.Health = 'kni.BaseHealth' + 'kni.BaseArmourBoost' + 'upg'
    kni.BaseHealthPositive = 25
    kni.BaseHealthNegative = 0
    kni.BaseArmourBoost = 20
    kni.BaseDamage = 25
    kni.Damage = 'kni.BaseDamage' + 'kni.BaseDamagePositive' - 'kni.BaseDamageNegative'
    kni.BaseDamagePositive = 0
    kni.BaseDamageNegative = 0
    #Assassin
    asa.BaseHealth = 100 + 'asa.BaseHealthPositive' - 'asa.BaseClassHealthNegative'
    asa.Health = 'asa.BaseHealth' + 'asa.BaseArmourBoost' + 'upg'
    asa.BaseHealthPositive = 25
    asa.BaseHealthNegative = 0
    asa.BaseArmourBoost = 20
    asa.BaseDamage = 25
    kni.Damage = 'mag.BaseDamage' + 'mag.BaseDamagePositive' - 'mag.BaseDamageNegative'
    asa.BaseDamagePositive = 0
    asa.BaseDamageNegative = 0
    #Archer
    arc.BaseHealth = 100 + 'arc.BaseClassHealthPositive' - 'arc.BaseClassHealthNegative'
    arc.Health = 'arc.BaseHealth' + 'arc.BaseArmourBoost' + 'upg'
    arc.BaseHealthPositive = 25
    arc.BaseHealthNegative = 0
    arc.BaseArmourBoost = 20
    arc.BaseDamage = 25
    arc.BaseDamagePositive = 0
    arc.BaseDamageNegative = 0

And Just for reference and some contex this is the rest of the code so far:

import time
Connect = input("Connect to the Minigame Console y/n: ")
if Connect == 'y':
  a = """
     7777                7777 777777777 77        777777777 777777777 77777             77777 777777777
      7777              7777  77        77        77        77     77 77  777         777  77 77
       7777     77     7777   777777    77        77        77     77 77    777     777    77 777777
        7777   7777   7777    77        77        77        77     77 77      777 777      77 77
         7777777  7777777     777777777 777777777 777777777 777777777 77        777        77 777777777"""     
  print(a)
else:
  print(":( S0rRy Y0u C0uLdn't pl4y")
if Connect == 'y':
  Gender = input("Pick Your Gender M/F: ")
  if Gender == "M":
    Name = input("Pick Your Name(Be carefull you can't change it later): ") 
    print("Hello " + Name + " Nice to meet you!") 
    Begin = input("Would you like to begin your adventure? y/n: ")
    if Begin == "y":
      Begin = input("Do you want to begin your adventure? y/n: ")
if Begin == "y":
    print("Great Lets Begin")
    classOp = """
    Mage
    Knight
    Assassin
    Archer"""
    class = input("Firstly What Class Do You Want to play as?" + classOp: )
    #def player stats
    weapon = class + "Weapon"
    #def class stats
    #Mage
    mag.BaseHealth = 100 + 'mag.BaseHealthPositive' - 'mag.BaseHealthNegative'
    mag.Health = 'mag.BaseHealth' + 'mag.BaseArmourBoost' + 'upg'
    mag.BaseHealthPositive = 25
    mag.BaseHealthNegative = 0
    mag.BaseArmourBoost = 20
    mag.BaseDamage = 25
    mag.Damage = 'mag.BaseDamage' + 'mag.BaseDamagePositive' - 'mag.BaseDamageNegative'
    mag.BaseDamagePositive = 0
    mag.BaseDamageNegative = 0
    #Knight
    kni.BaseHealth = 100 + 'kni.BaseHealthPositive' - 'kni.BaseHealthNegative'
    kni.Health = 'kni.BaseHealth' + 'kni.BaseArmourBoost' + 'upg'
    kni.BaseHealthPositive = 25
    kni.BaseHealthNegative = 0
    kni.BaseArmourBoost = 20
    kni.BaseDamage = 25
    kni.Damage = 'kni.BaseDamage' + 'kni.BaseDamagePositive' - 'kni.BaseDamageNegative'
    kni.BaseDamagePositive = 0
    kni.BaseDamageNegative = 0
    #Assassin
    asa.BaseHealth = 100 + 'asa.BaseHealthPositive' - 'asa.BaseClassHealthNegative'
    asa.Health = 'asa.BaseHealth' + 'asa.BaseArmourBoost' + 'upg'
    asa.BaseHealthPositive = 25
    asa.BaseHealthNegative = 0
    asa.BaseArmourBoost = 20
    asa.BaseDamage = 25
    kni.Damage = 'mag.BaseDamage' + 'mag.BaseDamagePositive' - 'mag.BaseDamageNegative'
    asa.BaseDamagePositive = 0
    asa.BaseDamageNegative = 0
    #Archer
    arc.BaseHealth = 100 + 'arc.BaseClassHealthPositive' - 'arc.BaseClassHealthNegative'
    arc.Health = 'arc.BaseHealth' + 'arc.BaseArmourBoost' + 'upg'
    arc.BaseHealthPositive = 25
    arc.BaseHealthNegative = 0
    arc.BaseArmourBoost = 20
    arc.BaseDamage = 25
    arc.BaseDamagePositive = 0
    arc.BaseDamageNegative = 0
    print("You Enter The Town of Hostlevil")
    storLine1 = input(A Stranger Aproaches You... Do You 1 Walk Away or 2 Stay And Talk to Them? )
      if storLine1 == '1':
        print("You continue on your way avoiding the stranger.")
elif storLine1 == '2':
  storLine2 = input('The large muscly man approaches you, it seems he has bad intentions. Do you 1 stay and fight or 2 runaway:  ')
if storLine2 == '1'
  print("You Stand Your Ground, the stranger attacts first with his fist dealing 5 damage. Do you use your fist or attack with your " + weapon)
    
    else:
      print(":( Are You Sure?")

`

What you are coding is called a ‘text based adventure’ game; yes, I’m old enough to remember those.

I can’t say that I fully understand your code, but maybe how I coded a simple turn based (think, ‘Final Fantasy’, but without the graphics) fight game will help (or not).

The entire file is apx 250 lines, but it initializes thus:

# indices
name = 0
damage = 1
health, usage = 2, 2
skill = 3

creatures = {
    1: ['Golem', 6, 30, 0],
    2: ['Ogre', 8, 30, 0],
    3: ['Minotaur', 10, 30, 0]
}

players = {
    1: ['Ben', 0, 20, 0],
    2: ['Richard', 0, 20, 0],
    3: ['Rob', 0, 20, 0]
}

weapons = {
    1: ['a pointed stick', 2, 2],
    2: ['a short knife', 4, 3],
    3: ['a long knife', 5, 3],
    4: ['a short sward', 6, 3],
    5: ['a long sward', 12, 3],
    6: ['a spear', 12, 2],
    7: ['bow and arrows', 10, 3]
}

So, as the game progresses, the attributes (health, skill level, usage) are updated in a somewhat arbitrary, but also logical way.

Thank you for the help. Its fine you can’t understand my code. My python coding is always a mess. I think the sample code you’ve given me is a big help although after posting the question I found out that I could use def to put all of the different characters into classes and shorten the code alot. But I think your way is alot simpler.

You’re welcome.

I got some inspiration from here:

… and adapted the code, rather than a simple C&P.

I’ve now made some significant edits to the code hopefuly its easier to understand:

Name = input("Pick Your Name(Be carefull you can't change it later): ") 
print("Hello " + Name + " Nice to meet you!") 
Begin = input("Would you like to begin your adventure? y/n: ")
if Begin == "y":
  print("Great Lets Begin!")
  classOptions = """
  Mage
  Knight
  Assassin
  Archer"""
  Class = input("Firstly What Class Do You Want to play as?" + classOptions: )
  #def class stats
  def calculate_health(base_health_positive, base_health_negative):
    return 100 + base_health_positive - base_health_negative

def calculate_damage(base_damage_positive, base_damage_negative):
    return 25 + base_damage_positive - base_damage_negative

class Character:
    def __init__(self, base_health_positive, base_health_negative, base_armour_boost,
                 base_damage_positive, base_damage_negative):
        self.base_health_positive = base_health_positive
        self.base_health_negative = base_health_negative
        self.base_armour_boost = base_armour_boost
        self.base_damage_positive = base_damage_positive
        self.base_damage_negative = base_damage_negative

        self.base_health = calculate_health(self.base_health_positive, self.base_health_negative)
        self.health = self.base_health + self.base_armour_boost
        self.base_damage = calculate_damage(self.base_damage_positive, self.base_damage_negative)
        self.damage = self.base_damage

class Mag(Character):
    def __init__(self):
        super().__init__(base_health_positive=25, base_health_negative=0,
                         base_armour_boost=20, base_damage_positive=0,
                         base_damage_negative=0)

class Knight(Character):
    def __init__(self):
        super().__init__(base_health_positive=25, base_health_negative=0,
                         base_armour_boost=20, base_damage_positive=0,
                         base_damage_negative=0)

class Assassin(Character):
    def __init__(self):
        super().__init__(base_health_positive=25, base_health_negative=0,
                         base_armour_boost=20, base_damage_positive=0,
                         base_damage_negative=0)

class Archer(Character):
    def __init__(self):
        super().__init__(base_health_positive=25, base_health_negative=0,
                         base_armour_boost=20, base_damage_positive=0,
                         base_damage_negative=0)

    print("You Enter The Town of Hostlevil")
    storLine1 = input(A Stranger Aproaches You... Do You 1 Walk Away or 2 Stay And Talk to Them? )
      if storLine1 == '1':
        print("You continue on your way avoiding the stranger.")
elif storLine1 == '2':
  storLine2 = input('The large muscly man approaches you, it seems he has bad intentions. Do you 1 stay and fight or 2 runaway:  ')
if storLine2 == '1'
  print("You Stand Your Ground, the stranger attacts first with his fist dealing 5 damage. Do you use your fist or attack with your " + weapon)
  
    
    
    


Just to add: I wanted a more ‘realistic’ look to the text output, so I came up with this technique:

from sys import stdout

from time import sleep

def commentary(say):
    for char in say:
        stdout.write(char)
        stdout.flush()
        sleep(0.08)
    return


weapon = "sward"
commentary(f'''You stand your ground, the stranger attacks first with his fist dealing 5 damage.
Do you use your fist or attack with your {weapon}?''')

Thanks for the help. Honestly I mostly expected some one to tell me I was being an idiot and that the answer was realing obvious. :slightly_smiling_face:

Again, you’re welcome.

I know (and maybe others can also relate to this) that when I’m coding and thinking about the structure and the goal, often times I become ‘code blind’ and miss something that, with hindsight, seems very obvious, but it takes someone else who is detached from my thought process, to point to or hint at what I should have done, or should be doing: we’re all here to help each other and we all need help from time to time.

1 Like

You could have all of those classes representing types of characters, contain a self.stats with a value from a dataclass

@dataclass
class Stats:
    base_health_positive: int
    base_health_negative: int
    base_armout_boost: int
    base_damage_positive: int
    base_damage_negative: int

The initial/default values can be stored in dictionaries, which in turn could be in text (configuration) files

knight_stats = {
    "base_health_positive": 25,
    "base_health_negative" : 0,
    "base_armour_boost": 20,
    "base_damage_positive": 0,
    "base_damage_negative": 0,
}
Knight.stats = Stats(**knight_stats)

Do you mean like this

def calculate_health(base_health_positive, base_health_negative):
    return 100 + base_health_positive - base_health_negative

def calculate_weapon_damage(base_weapon_damage_positive, base_damage_positve, base_damage_negative):
    return 25 + base_damage_positive - base_damage_negative + 
class Weapon:
  def __init__
base_weapon_damage_positive
def calculate_damage(base_damage_positive, base_damage_negative):
    return 25 + base_damage_positive - base_damage_negative + weapon_damage

class Character:
    def __init__(self, base_health_positive, base_health_negative, base_armour_boost,
                 base_damage_positive, base_damage_negative):
        self.base_health_positive = base_health_positive
        self.base_health_negative = base_health_negative
        self.base_armour_boost = base_armour_boost
        self.base_damage_positive = base_damage_positive
        self.base_damage_negative = base_damage_negative

        self.base_health = calculate_health(self.base_health_positive, self.base_health_negative)
        self.health = self.base_health + self.base_armour_boost
        self.base_damage = calculate_damage(self.base_damage_positive, self.base_damage_negative)
        self.damage = self.base_damage

class Player(Character):
    def __init__(self):
        super().__init__(base_health_positive=25, base_health_negative=0,
                         base_armour_boost=20, base_damage_positive=10,
                         base_damage_negative=0)

class CommonEnemy(Character):
    def __init__(self):
        super().__init__(base_health_positive=0, base_health_negative=0,
                         base_armour_boost=20, base_damage_positive=0,
                         base_damage_negative=0)

class Miniboss(Character):
    def __init__(self):
        super().__init__(base_health_positive=25, base_health_negative=0,
                         base_armour_boost=20, base_damage_positive=0,
                         base_damage_negative=0)

class Boss(Character):
    def __init__(self):
        super().__init__(base_health_positive=25, base_health_negative=0,
                         base_armour_boost=20, base_damage_positive=0,
                         base_damage_negative=0)
player = Player()
StrangerMale1 = CommonEnemy()

`

Just another note, I’ve scraped the classes idea because it was to complicated.

N.B., please don’t forget to include your code/output in code fencing so it is formatted correctly for others to read and copy, as I’ve done for you in your posts above. As you may have figured out, you can do so with the </> button in the toolbar, or via typing triple backticks above and below the code in question, optionally with the language name (e.g. python) for syntax highlighting, like this:

```python
<YOUR CODE HERE>
```