CSDN博客

img zhjhe

补上前天文章的代码!

发表于2004/7/13 22:44:00  575人阅读

import random

class item:
    def __init__(self):
        self.attribute = {}
    def add(self, index, value):
        self.attribute[index] = value
    def printDesc(self):
        for key in self.attribute:
            print key,
            print self.attribute[key]

class itemFactory:
    def __init__(self):
        self.basic = {}
        self.advance = {}
    def addBasic(self, index, value):
        self.basic[index] = value
    def addAdvance(self, index, value,  odds):
        self.advance[index] = (value, odds)
    def makeAItem(self):
        temp = item()
        for key in self.basic.keys():
           value = random.randint(*self.basic[key])
           temp.add(key, value)
        for key in self.advance.keys():
            odds = random.randint(1, 100)
            if odds > self.advance[key][1]:
                value = random.randint(*self.advance[key][0])
                temp.add(key, value)
        return temp
       
def main():
    bladeFac = itemFactory()
    bladeFac.addBasic('damage', (10, 20))
    bladeFac.addAdvance('HP', (20, 30), 70)
    bladeFac.addAdvance('MP', (15, 25), 65)
    bladeFac.addAdvance('STR', (10, 15), 60)
    bladeFac.addAdvance('DEX', (15, 20), 55)
    bladeFac.addAdvance('MAG', (5, 10), 50)
   
    bladeOfFire = bladeFac.makeAItem()
               
    bladeOfFire.printDesc()
   
if __name__ == '__main__':
    main()
       

0 0

相关博文

我的热门文章

img
取 消
img