So I have been playing with classes, seeing that they are more efficient than globals, and I wrote this hopefully simple test program to see if I properly reverse engineered tetlaw's simple rpg. For the life of me I cannot figure out what is wrong with it. If an expert on classes here could tell me why that would be lovely.
import random
import time
class items:
def __init__(self):
self.nametext = ['health potion', 'dagger', '5 gold']
self.name = self.nametext[self._name()]
self.healthpotions = 0
self.daggers = 0
self.gold = 0
self.newitem = 0
def potgain(self):
if self.newitem == 1:
items.healthpotions = items.healthpotions + 1
self.newitem = 0
def daggain(self):
if self.newitem == 2:
items.daggers += 1
self.newitem = 0
def goldgain(self):
if self.newitem == 3:
items.gold += 5
self.newitem = 0
def _name(self):
return random.randrange(3)+1
def playerinv():
it = items()
print "Inventory:"
print "==================="
print "Gold: ",it.gold
print "Daggers: ",it.daggers
print "Healing Potions: ",it.healthpotions
invact()
def invact():
inv = input('>> ')
if inv == 1:
exit
if inv == 2:
randitem()
else:
print "Invalid. 1 for exit, 2 for an item."
playerinv()
def randitem():
global it
it = items()
if it._name == 1:
it.healthpotions += 1
elif it._name == 2:
it.daggers += 1
elif it._name == 3:
it.gold += 5
else:
print "Something has gone horribly wrong"
print "A faint magical glow illuminates the dark corridor..."
print "...and",it.nametext,"appears into your hands. It is"
print "dark once more..."
time.sleep(2)
playerinv()
def additems():
items.newitem = 1
playerinv()