This week, I’ve spent more time working on a Python RPG game library then I’ve spent on SBW1, so don’t expect to see a huge chunk of content added to this weeks test build update. Below is a bit of what I’ve been working on in Python, this class is a generic character class for an RPG. Also, i’ve attached a screen shot of the classes behavior when called from a python command line!
class Char(object):
def __init__(self, name):
self.name = name
self.hpMax = 5
self.hp = self.hpMax
self.st = 1
self.vt = 1
self.armorDef = 1
self.weaponAtt = 1
self.weaponName = “Fist”
self.lvl = 1
self.exp = 0
self.exp_cap = 5
def __str__(self):
return (“Char “,name)
def attack(self, enemy):
temp_att = self.st + self.weaponAtt
print(self.name + ” attacks the ” + enemy.name + ” with ” + self.weaponName )
enemy.hit(self, temp_att)
def hit(self, enemy, attack_damage):
temp_damage = attack_damage – self.armorDef
self.hp -= temp_damage
if self.hp > 0:
print(self.name + ” takes ” + str(temp_damage) + ” damage.”)
else:
print(self.name + ” is defeated!”)
if self.lvl >= enemy.lvl:
temp_exp = 2**(self.lvl-enemy.lvl)
else:
temp_exp = 1
print(enemy.name + ” gains ” + str(temp_exp) + ” exp!”)
enemy.exp
temp_exp
if enemy.exp >= enemy.exp_cap:
enemy.lvlUp()
def lvlUp(self):
self.lvl += 1
self.hpMax += self.vit
statChoice = “”
while not (statChoice == “s” or statChoice == “v”):
statChoice = input(“Upgrade Strength or Vitality”)
statChoice = statChoice.lower()
statChoice = statChoice[0]
if statChoice == “s”:
self.st += 1
print(self.name + “s strength is now ” + str(self.st))
elif statChoice == “v”:
self.vt += 1
print(self.name + “s vitality is now ” + str(self.vt))
else:
statChoicce = “”
print(“That wasn’t a valid choice, lets try again.”)
self.hp = self.hpMax