Alright, so I'm trying to make a text rpg for proof of concept purposes, and I've hit a wall.
My problem, which may be deeper than I hope, lies within class instance referencing. I have a chest class where it fills the chest with a random amount of items. This works so far. Then there is the option to print out the contents of the chest. This works, kind of. It returns exactly what I told it to, unfortunately. Whenever I manually print the contents of the chest, it gives me memory locations for each instantiation. I have *no* idea how to fix this. If you need more of my code, please ask.
class Chest():
def __init__(self):
self.contents = []
self.maxnum = random.randrange(1,6) + 3
self.currnum = 0
self.itemtype = 0
itemindex = 0
self.fillChest()
def fillChest(self):
self.currnum = 0
while self.currnum < self.maxnum:
self.itemtype = random.randrange(1,4)
if self.itemtype == 1:
self.contents.append(HealthPotion())
self.currnum += 1
elif self.itemtype == 2:
self.contents.append(Armor(True))
self.currnum += 1
elif self.itemtype == 3:
self.contents.append(Sword(True))
self.currnum += 1
else:
print "Error"
break
def printChest(self):
x = self.contents.count(HealthPotion())
y = self.contents.count(Armor(True))
z = self.contents.count(Sword(True))
print "Chest contains:"
print "="*20
if x > 0:
print str(x) + " health potions"
if y > 0:
print str(y) + " set(s) of " + Armor.material
if z > 0:
print str(z) + Sword.material + " sword(s)"
print "="*20
^chest code
from playerclass02 import Player
import random
class Item(object):
def __init__(self):
self.name = "Null"
self.weight= 1
self.value = 10
def use(self):
print "Null use action."
class HealthPotion(Item):
def __init__(self):
Item.__init__(self)
self.name = "Health Potion"
self.weight = 5
self.value = 15
self.healamt = 10
self.description = "Heals 10 points of health"
def use(self, player):
if (player.health < player.maxhealth) and (player.health > 0):
print "%s is using %s. Description of object: %s" % (player.name, self.name, self.description)
player.health += self.healamt
elif player.health >= player.maxhealth:
print "%s is already at max health!" % (player.name)
else:
print "%s is dead, and cannot use the %s" % (player.name, self.name)
class Armor(Item):
def __init__(self, randinst):
Item.__init__(self)
matmod = 1
self.name = "Armor"
self.weight = 20
self.value = 50
self.defense = matmod * 5
self.material = "$default$"
self.description = "A sturdy suit of %s armor that protects for %s points of damage" % (self.material, self.defense)
self.worn = False
self.randinst = False
if self.randinst == True:
randarm()
def use(self, player):
if self.worn == False:
self.worn == True
player.armorval += self.defense
print "%s dons a suit of % armor. Description: %s" % (player.name, self.type)
else:
self.worn == False
player.armorval -= self.defense
def randarm(self):
random.randint(1,3) == a
if a == 1:
self.material = "iron"
matmod = 2
elif a == 2:
self.material = "steel"
matmod = 4
elif a == 3:
self.material = "obsidian"
matmod = 6
class Sword(Item):
def __init__(self, randinst):
Item.__init__(self)
matmod = 1
self.material = "$default$"
self.randinst = False
self.worn = False
self.attack = matmod * 5
if self.randinst == True:
randsword()
def randsword(self):
random.randint(1,3) == a
if a == 1:
self.material = "iron"
matmod = 2
elif a == 2:
self.material = "steel"
matmod = 4
elif a == 3:
self.material = "obsidian"
matmod = 6
^item code
PS. I don't know if the entire thing works, I've only got so far testing it before I ran into my little problem. If you have ANY questions please ask.